Found 7347 Articles for C++

Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++

sudhir sharma
Updated on 27-Jan-2021 04:59:42

464 Views

In this problem, we are given a number n. Our task is to create a program to find the sum of series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)). Let’s take an example to understand the problem,  Input:   n = 5Output:  55So, according to the question suppose a user gives us a number ‘n’ and we have to add series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1)).Let's understand the meaning of this series better first.We take n=1, then the series becomes 1.We take n=2, then the series becomes 1+ (1+3) because the ... Read More

Sum over Subsets - Dynamic Programming in C++

sudhir sharma
Updated on 27-Jan-2021 04:59:09

220 Views

In this problem, we are given an array arr[] of size 2n. Our task is to create a program to find the sum over subset using dynamic programming to solve it.We need to calculate function, F(x) = Σ Ai such that x&i == i for all x. i.e. i is a bitwise subset of x.Let’s take an example to understand the problem,  Input: A[] = {5, 7, 1, 9} , n = 2Output: 5 12 6 22Explanation: For n = 2 , there are 4 values of x. They are 0, 1, 2, 3.Now, calculating values of the function:F(0) = A0 ... Read More

Sum of XOR of sum of all pairs in an array in C++

sudhir sharma
Updated on 27-Jan-2021 04:57:57

1K+ Views

In this problem, we are given an array arr[] of size n. Our task is to create a program to find the sum of XOR of sum of all pairs in an array.Let’s see an example to understand the problem,  Input: arr[5, 7, 9]Output: 22Explanation: (5+5) ^ (5+7) ^ (5+9) ^ (7+5) ^ (7+7) ^ (7+9) ^ (9+5) ^ (9+7) ^ (9+9) = 22A simple solution to the problem is by using a nested loop. And creating all possible pairs from the array. And calculate the XOR of the sum of each pair.Algorithm: Initialise XorSum = 0Step 1: iterate from 0 to n. Follow:Step 1.1: iterate ... Read More

Print all possible ways to convert one string into another string in C++

sudhir sharma
Updated on 25-Jan-2021 05:29:05

171 Views

In this problem, we are given two strings str1 and str2. Our task is to create a program to Print all possible ways to convert one string into another string. Problem Description: Here, we need to find all possible ways using which we can convert str1 to str2. While converting, we can perform any of the three operations, Insert RemoveReplaceLet’s take an example to understand the problem,  Input: str1 = “kfeod” str2 = “kfcadq”OutputWay1:Insert q, after d. Replace c by e. Replace o by a.Solution ApproachWe will find the minimum number of edits first and then create a DP matrix. Then check if the character ... Read More

Print BST keys in given Range - O(1) Space in C++

sudhir sharma
Updated on 25-Jan-2021 05:28:31

82 Views

In this problem, we are given two values k1 and k2 (k1 < k2), and the root of the binary search tree. Our task is to create a program to Print BST keys in given Range. Problem Description: We will print all the keys of the tree from n1 to n2 in increasing order.Let’s take an example to understand the problem,  Input: k1 = 4, k2 = 12Output: 6, 7, 9Solution ApproachSimple we can solve the problem using inorder traversal but the space complexity there is 0(n) but the need of the hour is to solve in O(1) complexity. So, for this, we will use a ... Read More

Print cells with same rectangular sums in a matrix in C++

sudhir sharma
Updated on 25-Jan-2021 05:28:12

75 Views

In this problem, we are given a matrix mat of size mXn of integer values. Our task is to create a program to Print cells with same rectangular sums in a matrix.Problem description: We will be finding a cell in the matrix in such a way that the sum of sub-matrices that are starting and ending with the cell is equal to the sum of all the remaining elements. For a cell the sum of matrix (a, b) sub-matrix mat[0][0] to mat[a][b] and mat[a][b] to mat[m][n] is equal to the sum of all remaining elements.Let’s take an example to understand the problem, ... Read More

Find maximum in an array without using Relational Operators in C++

sudhir sharma
Updated on 25-Jan-2021 05:24:40

178 Views

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to find maximum in an array without using Relational Operators. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 7 , 8, 2}Output: 8Solution ApproachSince we need to compare values without using logical operators. For this we need to perform repeated subtraction, the number which will last longer will be the larger one.We will decrement all values by one till they become zero. We will start with the first two values of the array and find the greatest ... Read More

Find maximum average subarray of k length in C++

sudhir sharma
Updated on 25-Jan-2021 05:25:01

99 Views

In this problem, we are given an array arr[] of size n consisting of positive and negative values and an integer k. Our task is to find the maximum average subarray of k length. Let’s take an example to understand the problem,  Input: arr[] = {4, -1, 5, 6, -2, 4} k = 3Output: 10Explanation: The subarray of size 3 with max sum is -1, 5, 6 = 10Solution ApproachA solution to the problem is done by using an auxiliary array to store cumulative sum till the current index in the array.To find the sum of subarrays, we need to compute the difference between the indices ... Read More

Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

sudhir sharma
Updated on 25-Jan-2021 05:22:01

65 Views

In this problem, we are given two values x and y. Our task is to find maximum among x^(y^2) or y^(x^2) where x and y are given. Let’s take an example to understand the problem,  Input: x = 4, y = 3Output: 3^(4^2)Explanation: x^(y^2) = 4^(3^2) = 4^9 = 262144y^(x^2) = 3^(4^2) = 3^16 = 43046721Solution approachOne approach can be to calculate both values and then print the maximum of both. But this method does not work when the values are large.A simple and easy approach is using natural log (ln) which will be the solution easier.ln(x^(y^2)) = (y^2) * ln(x)ln(y^(x^2)) = (x^2) * ... Read More

Find maximum among all right nodes in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:35

48 Views

In this problem, we are given a Binary Tree. Our task is to find maximum among all right nodes in Binary Tree. Problem Description: Here, we need to find the maximum value amongst all right child nodes of the binary Tree.Let’s take an example to understand the problem,  Input: Output: 9Explanation: All right nodes are: {2, 8, 9}. Maximum of them is 9.Solution ApproachTo solve the problem, we need to traverse the tree and check if its right child exists. If it exists, compare with maxRight element and replace if it is greater.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More

Advertisements