C++ Articles

Page 215 of 597

Continuous Subarray Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have a list of non-negative numbers and a target integer k, we have to write a function to check whether the array has a continuous subarray of size at least 2 that sums up to a multiple of k, sums up to n*k where n is also an integer. So if the input is like [23, 2, 4, 6, 7], and k = 6, then the result will be True, as [2, 4] is a continuous subarray of size 2 and sums up to 6.To solve this, we will follow these steps −Make a map m, set m[0] ...

Read More

Contiguous Array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have a binary array, we have to find the maximum length of a contiguous subarray with equal number of 0 and 1. So if the input is like [0, 1, 0], then the output will be 2 as [0, 1] or [1, 0] is the largest contiguous array with equal number of 0s and 1s.To solve this, we will follow these steps −ret := 0, n := size of nums, sum := 0make a map m, set m[0] := - 1for i in range 0 to size of nums – 1sum := sum + 1 when nums[i] is ...

Read More

Random Pick with Weight in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have an array w of positive integers, were w[i] describes the weight of index i, we have to define a function pickIndex() which randomly picks an index in proportion to its weight.So if the input is like [1, 3], call pickIndex() five times, then the answer may come as − 0, 1, 1, 1, 0.To solve this, we will follow these steps −Define an array v, Through the initializer, initialize asn := w[0]for i in range 1 to size of ww[i] := w[i] + w[i – 1]n := w[i]v = wThe pickIndex() will work as follows −Take a ...

Read More

Program to find N-th term of series 3, 5, 33, 35, 53... in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 203 Views

In this tutorial, we will be discussing a program to find N-th term of series 3, 5, 33, 35, 53…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#include using namespace std; //finding the nth term in the series int printNthElement(int n){    int arr[n + 1];    arr[1] = 3;    arr[2] = 5;    for (int i = 3; i

Read More

Program to find HCF iteratively in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 300 Views

In this tutorial, we will be discussing a program to find HCF iteratively.For this, we will be provided with two numbers. Our task is to calculate the HCF of the given numbers using an iterative function.Example#include using namespace std; int get_HCF(int a, int b){    while (a != b){       if (a > b)          a = a - b;       else          b = b - a;    }    return a; } int main(){    int a = 60, b = 96;    cout

Read More

Program to find last digit of n'th Fibonnaci Number in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 656 Views

In this problem, we are given a number N. Our task is to create a Program to find last digit of Nth Fibonacci number in C++.Problem DescriptionWe need to find the last digit (i.e. LSB ) of the Nth Fibonacci number.Let’s take an example to understand the problem, Input: N = 120 Output: 1Solution ApproachA simple solution will be using the direct Fibonacci formula to find the Nth term. But this method will not be feasible when N is a large number. So to overcome this thing, we will use the property of the Fibonacci Series that the last digit ...

Read More

Program to find last two digits of 2^n in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 706 Views

In this problem, we are given a number N. Our task is to create a Program to find last two digits of 2^n in C++.Problem DescriptionTo find the last two digits. We will use only the product of the last two digits. And leave other things to make the calculation small.Let’s take an example to understand the problem, Input: N = 12 Output: 96Explanation2^12 = 4096Solution ApproachTo solve the problem, a direct approach could be finding the value of 2^N and then finding the remainder when it is divided by 100.Example#include using namespace std; int findLastDigit(int N){    int powerVal ...

Read More

Program to find LCM of two Fibonnaci Numbers in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 259 Views

In this problem, we are given two numbers N and M. Our task is to create a program to find LCM of two Fibonacci Numbers in C++.Problem Description − We will find the Nth and Mth Fibonacci number. And then we will find the LCM of their two numbers and return the result.Fibonacci Numbers0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377....Let’s take an example to understand the problem,  Input: N = 4, B = 9Output:Explanation4th Fibonacci number is 29th Fibonacci number is 21LCM is 42.Solution ApproachTo solve the problem, we need to find ...

Read More

Program to find Length of Bridge using Speed and Length of Train in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 292 Views

In this problem, we are given the length (L) and speed (S) of the train along with the time taken by it to pass the bridge. Our task is to create a program to find Length of Bridge using Speed and Length of Train in C++.Problem DescriptionWe need to find the length of the bride using the speed of the train, the time taken by it to cross the bridge, And the length of the train.Let’s take an example to understand the problem, Input: L = 310, S = 45m/sec , time = 12 secOutput: 230 mSolution ApproachThe whole train ...

Read More

Program to find N-th term of series 1, 2, 11, 12, 21... in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 221 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, 2, 11, 12, 21… in C++.Problem DescriptionTo find the Nth term of the series −1, 2, 11, 12, 21, 22, 111, 112, .... NtermsWe will find the general term of the series.Let’s take an example to understand the problem, InputN = 8Output112Solution ApproachTo derive the general term, we need to closely observe the series. In this series, we can see that there are only 1’s and 2’s in the value. And every term is an alternation of ...

Read More
Showing 2141–2150 of 5,962 articles
« Prev 1 213 214 215 216 217 597 Next »
Advertisements