Found 7347 Articles for C++

Maximum Sum of Two Non-Overlapping Subarrays in C++

Arnab Chakraborty
Updated on 13-Nov-2020 11:45:44

168 Views

Suppose we have an array A of integers; we have to find the maximum sum of elements in two non−overlapping subarrays. The lengths of these subarrays are L and M.So more precisely we can say that, we have to find the largest V for whichV = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either −0 = 0, decrease i by 1, decrease j by 1, do −rightL[i + 1] := max of temp and x where x is 0 if i + 2 >= n otherwise x = rightL[i + 2]temp ... Read More

Program to find trailing zeros in factorial of n in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 09:25:30

134 Views

Suppose we have a number n, we have to find the number of trailing zeros of n!.So, if the input is like n = 20, then the output will be 4, as 20! = 2432902008176640000To solve this, we will follow these stepsset count := 0for i := 5, (n/i) > 1, update i := i * 5, docount := count + (n /i)return countLet us see the following implementation to get better understandingExample Live Demo#include #include #define MAX 20 using namespace std; int countTrailingZeros(int n) {    int count = 0;    for (int i = 5; n / i >= 1; i *= 5)       count += n / i;    return count; } main() {    int n = 20;    cout

Program to find cost to remove consecutive duplicate characters with costs in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 09:07:01

401 Views

Suppose we have a string with lowercase letters and we also have a list of non-negative values called costs, the string and the list have the same length. We can delete character s[i] for cost costs[i], and then both s[i] and costs[i] is removed. We have to find the minimum cost to delete all consecutively repeating characters.So, if the input is like s = "xxyyx" nums = [2, 3, 10, 4, 6], then the output will be 6, as we can delete s[0] and s[3] for a total cost of 2 + 4 = 6.To solve this, we will follow ... Read More

Program to find minimum number of groups in communication towers in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:33:18

144 Views

Suppose we have a 2D binary matrix where 1 represents a communication tower, and 0 represents an empty cell. The towers can communicate in the following ways: 1. If tower A, and tower B are either on the same row or column, they can communicate with each other. 2. If tower A can talk with tower B and B can talk with C, then A can talk to C (transitive property). We have to find the total number of tower groups there are (here a group is a list of towers that can talk with each other).So, if the input ... Read More

Program to find angle between hour and minute hands of a clock in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:26:28

2K+ Views

Suppose we have two values hours and minutes. We have to find a smaller angle formed between the hour and the minute hand.So, if the input is like hour = 12 minutes = 45, then the output will be 112.5To solve this, we will follow these steps:if h = 12, then set h := 0if m = 60, then set m := 0hAngle := 0.5 * (60h) + mmAngle := 6mret := |hAngle - mAngle|return minimum of ret and (360 – ret)Let us see the following implementation to get better understanding:Example Live Demo#include using namespace std; class Solution { public: ... Read More

Program to find maximum number enemies will be killed to place a bomb in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:03:05

339 Views

Suppose we have a 2D matrix of three different values, 2s, 1s, and 0s, where a 2 represents an enemy, 1 represents a wall and 0 represents an empty cell. We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as we can place the bomb at the green box to kill maximum 3 enemies.ret := 0n ... Read More

Program to convert binary search tree to a singly linked list in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 07:46:56

497 Views

Suppose we have a binary tree; we have to convert this into a singly linked list (in place).So, if the input is likethen the output will beTo solve this, we will follow these steps:ser prev := nullDefine a recursive function solve(), that will take root as input.if root is null, then returnsolve(right of root)solve(left of root)right of root := prev, left of root := nullprev := rootLet us see the following implementation to get better understanding:Example Live Demo#include using namespace std; class TreeNode{    public:    int val;    TreeNode *left, *right;    TreeNode(int data){       val = ... Read More

Count the pairs of vowels in the given string in C++

Sunidhi Bansal
Updated on 02-Nov-2020 06:20:11

287 Views

We are given with a string of characters and the task is to calculate the count of pairs having both the elements as vowels. As we know there are five vowels in English alphabet i.e. a, i, e, o, u and other characters are known as Consonants.Input − string str = "tutorials point”Output − Count the pairs of vowels in the given string are: 2Explanation − From the given string we can form pairs as (t, u), (u, t), (t, o), (o, r), (r, i), (i, a), (a, l), (l, s), (s, p), (p, o), (o, i), (i, n) and ... Read More

Count the number of elements in an array which are divisible by k in C++

Sunidhi Bansal
Updated on 02-Nov-2020 06:18:19

1K+ Views

We are given with an array of positive integer numbers and an integer variable k. The task is to calculate the count of the number of elements in an array which is divisible by the given value k.Input − int arr[] = {4, 2, 6, 1, 3, 8, 10, 9}, k = 2Output − Count the number of elements in an array which are divisible by 2 are − 5Explanation − we will divide the elements in an array by a value k and check whether the reminder is 0 or not. So, 4 is divisible by 2, 2 is ... Read More

Count of character pairs at same distance as in English alphabets in C++

Sunidhi Bansal
Updated on 02-Nov-2020 06:16:00

102 Views

We are given a string of characters and the task is to calculate the count of character pairs having pairs at the same distance as we have in english alphabets.Input − string str = ‘Tutorials Point’Output − Count of character pairs at same distance as in English alphabets are: 5Explanation − The character pairs with same distance as in english alphabets are (u, t), (u, r), (t, r), (i, o) and (s, n). So in total there are 5 pairs.Input − string str = ‘Learning is the best habit’Output − Count of character pairs at same distance as in English ... Read More

Advertisements