Found 7197 Articles for C++

Valid Triangle Number in C++

Arnab Chakraborty
Updated on 04-May-2020 13:14:50

602 Views

Suppose we have an array consists of non-negative integers, our task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n – 1 down to 0right := i – 1, left ... Read More

Array Nesting in C++

Arnab Chakraborty
Updated on 04-May-2020 13:12:17

540 Views

Suppose we have a zero-indexed array A of length N that contains all integers from 0 to N-1. We have to find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below. Now consider the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element occurs in S. So if the array is like A = [5, 4, 0, 3, 1, 6, ... Read More

Next Greater Element III in C++

Arnab Chakraborty
Updated on 04-May-2020 13:09:59

257 Views

Suppose we have a positive 32-bit integer n, we need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If we have no such positive 32-bit integer number, then return -1.So if the number is 213, then the result will be 231.To solve this, we will follow these steps −s := n as string, sz := size of s, ok := falsefor i in range sz – 2 to 0if s[i] < s[i + 1], then ok := true and break the loopif of is ... Read More

Optimal Division in C++

Arnab Chakraborty
Updated on 04-May-2020 13:06:53

321 Views

Suppose we have a list of positive integers; the adjacent integers will perform the float division. So for example, [2, 3, 4] -> 2 / 3 / 4. Now, we can add any number of parenthesis at any position to change the priority of these operations. We should find out how to add parenthesis to get the maximum result, we have to find the corresponding expression in string format. Our expression should NOT contain redundant parenthesis. So if the input is like [1000, 100, 10, 2], then the result will be “1000/(100/10/2)”.To solve this, we will follow these steps −n ... Read More

Minimum Time Difference in C++

Arnab Chakraborty
Updated on 04-May-2020 13:02:31

464 Views

Suppose we have a list of 24-hour clock time points in "Hour:Minutes" format, we have to find the minimum minutes difference between any two time points in the list. So if the input is like [“12:30”, ”15:17”], so this will return 167.To solve this, we will follow these steps −Define an array called ok of size 24*60 + 1, and initially all are false.n := size of tpfor i in range 0 to n – 1hr := the hour part from the timemin := minute part of the stringtime := hr * 60 + minif ok[time] is true, then return ... Read More

Complex Number Multiplication in C++

Arnab Chakraborty
Updated on 04-May-2020 12:58:28

2K+ Views

Suppose we have two strings that are representing complex numbers, we have to parse them and perform complex number multiplication, then return result as a string.So if the input is like “1+-1i” and “1+-1i”, then the result will be “0+-2i”.To solve this, we will follow these steps −aa := a pair of real and imaginary of first complex numberbb := a pair of real and imaginary of second complex numberx := aa.real * bb.real – aa.img*bb.imgy := aa.real * bb.img + aa.img*bb.realreturn the string as “x+yi”Let us see the following implementation to get better understanding −Example Live Demo#include using namespace ... Read More

Program to find N-th term of series 2, 12, 28, 50, 77, 112, 152, 198, …in C++

Ayush Gupta
Updated on 03-Oct-2020 09:39:55

96 Views

In this problem, we are given a number N. Our task is to create a program to find N-th term of series 2, 12, 28, 50, 77, 112, 152, 198, …in C++.Problem Description − To find the N-th term of the series.2, 12, 28, 50, 77, 112, 152, 198, ...N termsWe will find a general for the series.Let’s take an example to understand the problem, Input − N = 6Output − 112Solution Approach:Here, the series is increasing in parabolic form, so the general term will be a quadratic equation.So, the general formula for the series isTN = 3*(N*N) + N ... Read More

Program to find N-th term of series 0, 11, 28, 51, 79, 115, 156, 203, …In C++

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

120 Views

In this problem, we are given a number N. Our task is to create a program to find N-th term of series 0, 11, 28, 51, 79, 115, 156, 203, … In C++.Problem Description − To find the Nth terms of the series −0, 11, 28, 51, 79, 115, 156, 203, … N-th term.Let’s take an example to understand the problem,Input − N = 5Output − 79Solution Approach:The general formula for nth term of the series is −Tn = 3*(N*N) + 2*N - 5Program to illustrate the working of our solution,#include using namespace std; int findNTerm(int N) { int nthTerm = ( (3*N*N) + 2*N - 5); return nthTerm; } int main() { int N = 9; cout

Program to find N-th term of series 1, 3, 12, 60, 360...in C++

Ayush Gupta
Updated on 03-Oct-2020 09:41:34

176 Views

In this problem, we are given a number N. Our task is to create a Program to find N-th term of series 1, 3, 12, 60, 360...in C++.Given Series1, 3, 12, 60, 360, 2520 … N TermsLet’s take an example to understand the problem, Input − N = 6Output − 2520Solution Approach:The general term formula for this one is a bit tricky. So, the increase in the series values is huge. So, there can be a few possibilities factorial or exponentials. So, we will first consider factorial, and on observing we can see the growth half as half of the ... Read More

Program to find N-th term of series 1, 6, 17, 34, 56, 86, 121, 162, …in C++

Ayush Gupta
Updated on 04-May-2020 12:28:24

137 Views

In this tutorial, we will be discussing a program to find N-th term of series 1, 6, 17, 34, 56, 86, 121, 162, …For this, we will be provided with a number. Our task is to find the term for the given series at that particular position.Example Live Demo#include #include using namespace std; //calculating nth term of given series int nthTerm(int n) {    return 3 * pow(n, 2) - 4 * n + 2; } int main() {    int N = 4;    cout

Advertisements