Found 7347 Articles for C++

Find next greater number with same set of digits in C++

Naveen Singh
Updated on 13-Mar-2021 12:34:42

1K+ Views

In this problem, we are given an element N. We need to find the next greater number with the same set of digits. We need to find the smallest number with the same digit which is greater than N.Let’s take an example to understand the problem, InputN = "92534"Output92543Solution ApproachA simple solution to the problem to find the next greater element is by the following approach −Traverse the number from least significant bit to most significant bit. And stop when the current element is smaller than the last element.After this search for the smallest element in the remaining array. And ... Read More

Find n’th number in a number system with only 3 and 4 in C++

Naveen Singh
Updated on 13-Mar-2021 12:33:05

1K+ Views

In this problem, we are given an element N. we need to find the N’th number in a number system with only 3 and 4.The number system consists of elements 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, …Let’s take an example to understand the problem, InputN = 6Output44ExplanationThe numbers of the number system are − 3, 4, 33, 34, 43, 44...Solution ApproachThe number system is similar to the binary number system but the number 0 is replaced by 3 and number 1 is replaced by 4.Let’s say this as sBinary.So, number Nth number is (n-1)’s Sbinary conversion.With ... Read More

Find N’th item in a set formed by sum of two arrays in C++

Naveen Singh
Updated on 13-Mar-2021 12:30:53

98 Views

In this problem, we are given two sorted arrays arr1[] and arr2[] of size m and an element N. Our task is to Find Nth item in a set formed by the sum of two arrays.Code description − Here, we will create a set that consists of the sum of element one of arr1 and one of arr2 i.e. sum = arr1[i] + arr2[j], where are i , j < m. For N, we need to find the value of the Nth element of the set.Let’s take an example to understand the problem, Inputarr1[] = {3, 1, 5} , arr2[] = ... Read More

Find n-variables from n sum equations with one missing in C++

Naveen Singh
Updated on 13-Mar-2021 12:27:57

47 Views

In this problem, we are given an array sum[] consisting of a sum of (n-1) variables given as, Sum[1] = x2 + x3 + x4 + … xn Sum[2] = x1 + x3 + x4 + … xn . . Sum[i] = x2 + x3 + x4 + … x(i-1) + x(i+1) + … + xn . . Sum[n] = x1 + x2 + x3 + … x(n-1) Our task is to find the value of x1, x2, ... xn.Let’s take an example to understand the problem, Inputsum[] = {6, 6, 6, 6, 6, 6, 6}Outputx1 = 1, x2 = ... Read More

C++ program to find n-th term of series 3, 9, 21, 41, 71…

Naveen Singh
Updated on 13-Mar-2021 12:26:14

200 Views

In this problem, we are given an integer N. The task is to find the n-th term in series 3, 9, 21, 41, 71...Let’s take an example to understand the problem, InputN = 7Output169ExplanationThe series is 3, 9, 21, 41, 71, 169...Solution ApproachA simple solution to the problem is by finding the general term of the series. The general term can be found by observing the series a bit. It is, $$T(N) = \sum n^{2} + \sum n + 1$$We can directly use the formula for the sum of square of first n natural numbers, first n natural number and ... Read More

C++ program to find n-th term of series 2, 10, 30, 68, 130 …

Naveen Singh
Updated on 13-Mar-2021 12:24:30

3K+ Views

In this problem, we are given an integer N. The task is to find the n-th term in series 2, 10, 30, 68, 130...Let’s take an example to understand the problem, InputN = 7Output350ExplanationThe series is 2, 10, 30, 68, 130, 222, 350...Solution ApproachA simple solution to the problem is by finding the general term of the series. Here, the Nth term of the series is N^3 + N. This is found by subtracting the current element with the current index.For i, i = 1, T(1) = 2 = 1 + 1 = 1^3 + 1 i = 2, T(1) ... Read More

C++ program to find n-th term of series 1, 3, 6, 10, 15, 21…

Naveen Singh
Updated on 13-Mar-2021 12:22:37

501 Views

In this problem, we are given an integer N. The task is to find the n-th term in series 1, 3, 6, 10, 15, 21, 28....Let’s take an example to understand the problem, InputN = 7Output28ExplanationThe series is 1, 3, 6, 10, 15, 21, 28...Solution ApproachA simple solution to the problem is by finding the general term of the series. On observing the series we can see that the ith number of the series is the sum of (i-1)th term and i.This type of number is called triangular number.To solve the problem, we will loop till n, and for each ... Read More

C++ program to find n-th term of series 1, 4, 27, 16, 125, 36, 343...

Naveen Singh
Updated on 13-Mar-2021 12:20:32

419 Views

In this problem, we are given an integer N. The task is to find the n-th term in series 1, 4, 27, 16, 125, 36, 343....Let’s take an example to understand the problem, InputN = 7Output343ExplanationThe series is 1, 4, 27, 16, 125, 36, 343…Solution ApproachA simple solution to the problem is by finding the general term of the series. This series comprises two different series one at odd terms and one at even terms. If the current element index is even, the element is square of its index. And if the current element index is odd, the element is ... Read More

C++ program to find n-th term in the series 9, 33, 73,129 …

Naveen Singh
Updated on 13-Mar-2021 12:19:06

284 Views

In this problem, we are given an integer N. The task is to find n-th term in series 9, 33, 73, 129....Let’s take an example to understand the problem, InputN = 4Output129ExplanationThe series upto nth term is 9, 33, 73, 129...Solution ApproachThe solution to the problem lies in finding the nth term of the series. We will find it mathematically and then apply the general term formula to our program.First let’s subtract the series by shifting it by one.Sum = 9 + 33 + 73 + … + t(n-1) + t(n) - Sum = 9 + 33 + 73 + ... Read More

C++ program to find n-th term in the series 7, 15, 32, …

Naveen Singh
Updated on 13-Mar-2021 12:17:07

130 Views

In this problem, we are given an integer N. The task is to find n-th term in series 7, 15, 32....Let’s take an example to understand the problem, InputN = 6Output281ExplanationThe series upto nth term is 7, 15, 32, 67, 138, 281Solution ApproachThe solution to the problem lies in decoding the series. You can see the series is a mix of series.The subtracting the values, T(2) - T(1) = 15 - 7 = 8 T(3) - T(2) = 32 - 15 = 17 So, T(2) = 2*T(1) + 1 T(3) = 2*T(2) + 2 T(n) = 2*T(n-1) + ... Read More

Advertisements