Found 7197 Articles for C++

Find sum of divisors of all the divisors of a natural number in C++

sudhir sharma
Updated on 27-Jan-2022 08:11:18

1K+ Views

In this problem, we are given a natural number N. Our task is to find the sum of divisors of all the divisors of a natural number.Let's take an example to understand the problem, Input : N = 12 Output : 55Explanation −The divisors of 12 are 1, 2, 3, 4, 6, 12 Sum of divisors = (1) + (1 + 2) + (1 + 3) + (1 + 2 + 4) + (1 + 2 + 3 + 6) + (1 + 2 + 3 + 4 + 6 + 12) = 1 + 3 + 4 + 7 ... Read More

Find Sum of all unique subarray sum for a given array in C++

sudhir sharma
Updated on 25-Jan-2022 14:17:49

731 Views

In this problem, we are given an array arr[] consisting of n integer values. Our task is to find the sum of all unique subarray sum for a given array. Subarray sum is the sum of elements of the given subarray.Let's take an example to understand the problem, Input : arr[] = {1, 2, 4} Output : 23Explanation −All subarrays of the given array are : (1), (2), (4), (1, 2), (2, 4), (1, 2, 4) Sum of subarrays = 1 + 2 + 4 + (1+2) + (2+4) + (1+2+4) = 23Solution ApproachA solution to the problem is by ... Read More

Find sum of all right leaves in a given Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2022 14:01:09

425 Views

In this problem, we are given a binary tree. Our task is to find the sum of all left right in a given Binary Tree.Let's take an example to understand the problem, Input :Output : 8Explanation −All leaf nodes of the tree are : 1, 8 Sum = 1 + 8 = 9Solution ApproachA simple solution to the problem is traversing the tree from root to leaf. If a node is a left leaf node, add it to sum. When the whole tree is traversed. Print Sum.ExampleProgram to illustrate the working of our solution#include using namespace std; struct Node{ ... Read More

Find sum of all left leaves in a given Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2022 13:26:08

396 Views

In this problem, we are given a binary tree. Our task is to find the sum of all left leaves in a given Binary Tree.Let's take an example to understand the problem, Input : Output : 11Explanation −All leaf nodes of the tree are : 2, 9 Sum = 2 + 9 = 11Solution ApproachA simple solution to the problem is traversing the tree from root to leaf. If a node is a left leaf node, add it to sum. When the whole tree is traversed. Print Sum.ExampleProgram to illustrate the working of our solution#include using namespace std; struct Node{ ... Read More

Find subarray with given sum - (Handles Negative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 13:06:14

590 Views

In this problem, we are given an array arr[] consisting of N integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, -1, 4, 6, -9, 5} sum = 14 Output : subarray = {5, -1, 4, 6}Explanation −Subarray sum = 5 - 1 + 4 + 6 = 14Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find ... Read More

Find subarray with given sum - (Nonnegative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 12:52:30

412 Views

In this problem, we are given an array arr[] consisting of N positive integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, 1, 4, 6, 9, 5} sum = 11 Output : subarray = {1, 4, 6}Explanation −Subarray sum = 1 + 4 + 6 = 11Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find the sum ... Read More

Find sub-string with given power in C++

sudhir sharma
Updated on 25-Jan-2022 12:06:06

259 Views

In this problem, we are given a string str and an integer pow. Our task is to find a sub-string with given power.We need to return the substring whose power is equal to pow.Power of string is the sum of powers of its characters.Power of character : a -> 1, b -> 2, c -> 3, ...Let's take an example to understand the problem, Input : string = "programming" power = 49 Output : 'pro'Explanation −Power of matrix : pro, power(p) = 16 power(p) = 18 power(p) = 15 Total = 16 + 18 + 15 = 49Solution ApproachA simple ... Read More

Find sub-matrix with the given sum in C++

sudhir sharma
Updated on 25-Jan-2022 11:16:54

1K+ Views

In this problem, we are given a 2D matrix of size N*N and two variables sum and size. Our task is to find a sub-matrix with the given sum.We need to find a sub-matrix of size*size with element sum equal to sum.Let's take an example to understand the problem, Input : mat[][] = {    {1, 5, 7, 9}    {2, 4, 6, 8}    {1, 2, 5, 6}    {3, 6, 9, 3} } sum = 22 Size = 2 Output : YESExplanation −The submatrix of size k with sum 22 is {5, 7} {4, 6}Solution ApproachA simple solution ... Read More

Find start and ending index of an element in an unsorted array in C++

sudhir sharma
Updated on 25-Jan-2022 08:48:09

933 Views

In this problem, we are given an array aar[] of n integer values which are not sorted and an integer val. Our task is to find the start and ending index of an element in an unsorted array.For the occurrence of the element in the array, we will return, "Starting index and ending index " if it is found in the array twice or more."Single index " if it is found in the array once."Element not present " if it is not present in the array.Let's take an example to understand the problem, Example 1Input : arr[] = {2, 1, ... Read More

Find Square Root under Modulo p (Shanks Tonelli algorithm) in C++

sudhir sharma
Updated on 25-Jan-2022 08:28:01

580 Views

In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p.Let's take an example to understand the problem, Input : n = 4, p = 11 Output : 9Solution ApproachHere, we will be using Tonelli-Shanks Algorithm.Tonelli-Shanks Algorithm is used in modular arithmetic to solve for a value x in congruence of the form x2 = n (mod p).The algorithm to find square root modulo using shank's Tonelli Algorithm −Step 1 − Find the value of $(n^{((p-1)/2)})(mod\:p)$, if its value is p -1, then modular square root is ... Read More

Advertisements