Found 7347 Articles for C++

Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++

sudhir sharma
Updated on 14-Aug-2020 13:36:24

589 Views

In this problem, we are given a number n. Our task is to create a program to find the sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+...+n).Lets example to understand the problem,Inputn = 4Output 20Explanation −(1) + (1+2) + (1+2+3) + (1+2+3+4) = 20A simple solution to the problem will be creating the series by using two loops.AlgorithmInitialize sum = 0 Step 1: Loop for i -> 1 to n i.e i = 1 to i 1 to i i.e. i = 1 to i

Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++

sudhir sharma
Updated on 14-Aug-2020 13:33:43

818 Views

Here, we are given an integer n. It defines the number of terms of the series 1/1 + ( (1+2)/(1*2) ) + ( (1+2+3)/(1*2*3) ) + … + upto n terms.Our task is to create a program that will find the sum of series 1/1 + (1+2)/(1*2) + (1+2+3)/(1*2*3) + … upto n terms.Let’s take an example to understand the problem, Inputn = 3Output3.5Explanation −(1/1) + (1+2)/(1*2) + (1+2+3)/(1*2*3) = 1 + 1.5 + 1 = 3.5A simple solution to this problem is by looping from 1 to n. Then, add the values of the sum of i divided by ... Read More

Sum of the series 0.7, 0.77, 0.777 … upto n terms in C++

sudhir sharma
Updated on 14-Aug-2020 13:31:55

158 Views

In this problem, we are given n terms of a number. The series is 0.7, 0.77, 0.777…. Our task is to create a program to find the sim of the series 0.7, 0.77, 0.777 … upto n terms.Let’s take an example to understand the problem, Input  4Output  Explanation −0.7 + 0.77 + 0.777 + 0.7777 = 3.0247To solve this problem, we will derive the formula for sum of series. Let’s find the general formula for it, sum = 0.7 + 0.77 + 0.777 + ... upto n terms sum = 7 (0.1 + 0.11 + 0.111 + … upto n ... Read More

Sum of the products of all possible Subsets in C++

sudhir sharma
Updated on 14-Aug-2020 13:24:44

524 Views

In this problem, we are given an array arr[] of N numbers. Our task is to create a program that will find the sum of the products of all possible subsets.Here, we will find all subsets and then find the product of all elements for each subset. Then add all the values to calculate the sum.Let’s take an example to understand the problem, Inputarr[] = {4, 5, 6}Output209Explanation −All subsets of arr[] are: {4}, {5}, {6}, {4, 5}, {5, 6}, {4, 6}, {4, 5, 6} Sum of product = (4) + (5) + (6) + (4*5) + (5*6) + (4*6) ... Read More

Queries to find whether a number has exactly four distinct factors or not in C++

Ayush Gupta
Updated on 09-Oct-2020 08:53:01

304 Views

In this problem, we are given a Q number of queries, each having a number N. Our task is to create a program to solve the Queries to find whether a number has exactly four distinct factors or not in C++.Problem DescriptionTo solve each query, we need to find whether the number N has exactly four distinct factors. If it has print YES, else No.Let’s take an example to understand the problem, Input: Q = 3, 4, 6, 15 Output: NOYES YESExplanationFor query 1: Factors of 4 are 1, 2, 4For query 2: Factors of 6 are 1, 2, 3, 6For ... Read More

Queries to find the last non-repeating character in the sub-string of a given string in C++

Ayush Gupta
Updated on 09-Oct-2020 08:55:40

130 Views

In this problem, we are given string str, and Q queries, each consisting of two integers. Our task is to create the program to solve Queries to find the last non-repeating character in the sub-string of a given string in C++.Problem DescriptionIn each query, we have two integers L and R. To solve the queries, we will take a substring starting from index L to index R. And find the last character which is non-repeating in the sub-string.Let’s take an example to understand the problem, Input: str = “Tutorialspoint” Q = 2query = {{4, 8}, {2, 6}}Output: -1 , -1ExplanationsubStr[4...8] ... Read More

Queries to find maximum product pair in range with updates in C++

Ayush Gupta
Updated on 09-Oct-2020 08:58:39

304 Views

In this problem, we are given an array arr[] and Q queries. Each Query can be one of 2 types, 1st to find the maximum pair product in a given range [ Start - End ]. 2nd to update the ith index element with value. Our task is to create a program to solve Queries to find maximum product pair in range with updates in C++.Let’s take an example to understand the problem, Input:arr = {4, 2, 6, 9, 1}Q = 3Q1 = [1, 1, 4]Q2 = [2, 2, 3]Q3 = [1, 0, 2]Output: 54, 12ExplanationFor query 1, type 1: range ... Read More

Queries to find distance between two nodes of a Binary tree – O(logn) method in C++

Ayush Gupta
Updated on 09-Oct-2020 09:03:23

109 Views

In this problem, we are given a binary tree and we are given Q queries. Our task is to create a program to solve Queries to find distance between two nodes of a Binary tree – O(logn) method in C++.Problem DescriptionIn each query, we are given two nodes of the binary tree and we need to find the number distance between two nodes i.e. the number of edges to be traversed to reach one node from another node.Let’s take an example to understand the problem, Input: Binary TreeQueries = 3Q1 -> [2, 6]Q2 -> [4, 1]Q3 -> [5, 3]Output:3, 2, ... Read More

Queries to count the number of unordered co-prime pairs from 1 to N in C++

Ayush Gupta
Updated on 09-Oct-2020 09:04:59

193 Views

In this problem, we are given Q queries each contains a number N. Our task is to create a program to solve Queries to count the number of unordered coprime pairs from 1 to N in C++.Co-prime also known as relatively prime or mutually prime are the pair of numbers that have only one factor i.e. 1.Let’s take an example to understand the problem, Input: Q = 2, queries = [5, 6] Output: 10ExplanationThe pairs are : (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (4, 5)Solution ApproachThe most promising solution ... Read More

Queries to check whether a given digit is present in the given Range in C++

Ayush Gupta
Updated on 09-Oct-2020 09:09:14

265 Views

In this problem, we have given an array arr[] and some queries each consisting of three values, L and R, and val. Our task is to create a program to solve Queries to check whether a given digit is present in the given Range in C++.Problem Description−To solve each query, we need to check if the given element val is present in the given Range between L and R or not.Let’s take an example to understand the problem, Input:arr[] = {4, 8, 1, 7, 2, 9, 3, 5, 1}Q = 3query = {{1, 4, 3}, {0, 2, 1}, {4, 7, ... Read More

Advertisements