Found 7347 Articles for C++

Find sum of the series 1-2+3-4+5-6+7....in C++

sudhir sharma
Updated on 27-Jan-2022 08:45:24

1K+ Views

In this problem, we are given an integer value N. Our task is to find Sum of Series 1 - 2 + 3 - 4 + 5 - 6 + 7 upto n terms.The series is 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10...Let's take an example to understand the problem, Input : N = 4 Output : -2Explanation −1 - 2 + 3 - 4 = -2 Solution ApproachA simple approach to solve the problem is finding the general term of the series and then finding the sum ... Read More

Find sum of the series ?3 + ?12 +.... upto N terms in C++

sudhir sharma
Updated on 27-Jan-2022 08:43:32

96 Views

In this problem, we are given an integer value N. Our task is to find Sum of Series ?3 + ?12 + ... upto n terms.The series is $\sqrt3 + \sqrt12 + \sqrt27 + \sqrt48 + ...$I.e. It is a series of square roots.Let's take an example to understand the problem, Input : N = 3 Output : 10.3922Explanation −$\sqrt3 + \sqrt12 + \sqrt27 = 1.7320 + 3.4641 + 5.1961 = 10.3922$Solution ApproachA simple approach to solve the problem is finding the general term of the series and then finding the sum till n terms. And calculating the sum using ... Read More

Find sum of Series with n-th term as n^2 - (n-1)^2 in C++

sudhir sharma
Updated on 27-Jan-2022 08:38:47

304 Views

In this problem, we are given an integer value N. Our task is to find Sum of Series n^2 - (n-1)^2 upto n terms.Let's take an example to understand the problem, Input : N = 3 Output : 6Explanation −[12 - (0)2] + [22 - (1)2] + [32 - (2)2] = 1 - 0 + 4 - 1 + 9 - 2 = 9Solution ApproachA simple approach to solve the problem is finding the general term of the series and then finding the sum till n terms. And calculating the sum using formula will reduce the time to O(1). Also, ... Read More

Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++

sudhir sharma
Updated on 27-Jan-2022 08:31:46

688 Views

In this problem, we are given an integer value N. Our task is to find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms.Let's take an example to understand the problem, Input : N = 3 Output : 6Explanation −12 - 22 + 32 = 1 - 4 + 9 = 6Solution ApproachA simple approach to solve the problem is using loops. We will loop from 1 to n with iterator i.If i is odd, add (i2) to the sum.If i is even, subtract (i2) to the sum. At last, return the sum of series ... Read More

Find Sum of pair from two arrays with maximum sum in C++

sudhir sharma
Updated on 27-Jan-2022 08:24:46

201 Views

In this problem, we are given two arrays, positive and distinct. Our task is to find the sum of pairs from two arrays with maximum sum.We will find the pair with the maximum sum with one element from each array.Let's take an example to understand the problem, Input : arr1[] = {3, 7, 5}, arr2[] = {8, 2, 4} Output : 15Explanation −Pairs is (7, 8) = 7 + 8 = 15 Solution ApproachA simple approach to solve the problem is using loops. We will use a nested loop and find the sum of all pairs and return the pair ... Read More

Find sum of even and odd nodes in a linked list in C++

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

674 Views

In this problem, we are given a linked list. Our task is to find the sum of even and odd nodes in a linked list.Let's take an example to understand the problem, Input : linked list : 3 -> 2 -> 5 -> 7 -> 1 -> 9 Output : evenSum = 2 ; oddSum = 25Explanation −evenSum = 2 oddSum = 3 + 5 + 7 + 1 + 9 = 25Solution ApproachA simple approach to solve the problem is traversing the linked list and checking for even or odd values and adding them to their respective sum value.AlgorithmStep ... Read More

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

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

811 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

566 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

303 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

316 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

Advertisements