Server Side Programming Articles - Page 1643 of 2650

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 14-Aug-2020 13:50:53

530 Views

In this problem, we are given an integer n. Our task is to create a program to find the sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)).From this series, we can observe that ith term of the series is the sum of first i odd numbers.Let’s take an example to understand the problem, Inputn = 3Output 14Explanation −(1) + (1+3) + (1+3+5) = 14A simple solution to this problem is using a nested loop and then add all odd numbers to a sum variable. Then return the sum.ExampleProgram to illustrate the working of our solution, ... Read More

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

Ravi Ranjan
Updated on 16-Jul-2025 18:15:22

870 Views

In this article, we are given a number n. Our task is to write a program to calculate the sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+...+n). This series can be represented mathematically as: $$ \displaystyle\sum\limits_{k=1}^n \displaystyle\sum\limits_{j=1}^k j $$ The above series is also known as tetrahedral number or triangular pyramidal number. A tetrahedral number is the number of points required to form a pyramid with a triangular base. Below is an example of the tetrahedral number series up to n. Scenario Consider the following example ... Read More

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

Ravi Ranjan
Updated on 15-Jul-2025 18:29:51

1K+ Views

In this article, we are given an integer n. It defines the number of terms in the series: 1/1 + ( (1+2)/(1*2) ) + ( (1+2+3)/(1*2*3) ) + … + up to n terms. Our task is to write a program to calculate the sum of series 1/1 + (1+2)/(1*2) + (1+2+3)/(1*2*3) + … up to n terms. The above series can be represented as: $$ \sum_{k=1}^{n} \frac{\sum_{j=1}^{k} j}{k!} $$ Scenario The following example calculates the sum for the given series up to 4 terms: Input: n ... 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

249 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

767 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

420 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

192 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

405 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

192 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

302 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

Advertisements