Count Ways to Reach the Nth Stair Using Steps 1, 2, or 3 in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:40:34

616 Views

We are given a total number of steps in a staircase that is n. A person can reach the next floor by skipping 1, 2 or 3 steps at a time. The goal is to find the number of ways in which the next floor can be reached by doing so.We will use the recursive method by keeping in mind that to reach any i’th step, a person has to jump from i-1th step ( skip 1 step) , i-2th step (skip 2 steps ) or i-3th step ( skip 3 steps ).Let’s understand with examples.Input N=3 stepsOutput Count of ways to ... Read More

Count Triplet Pairs (A, B, C) in 2D Space in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:38:48

348 Views

We are given an input of N points on a 2-D space. The goal is to find the count of triplets of points from the input such that one point is the mid-point on the line between the other two. i.e if triplet is (A, B, C) then B is midpoint of A and C ( or any other combination of A, B, C ).We will do this by inserting all points as pairs into a vector. Then add all pairs from this vector in set. By taking two points from the set check if the sum of (x, ... Read More

Count Numbers with All 1s Together in Binary Representation in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:37:01

516 Views

We are given a positive integer N. The goal is to count the numbers less than or equal to N that have all 1’s in their binary representation. For example 1 is 1, 3 is 11, 7 is 111, 15 is 1111... so on.If we see the numbers then all of them are 2i-1. Where i start from 1. To check such numbers less than n. We’ll compare if 2i-1

Count Numbers Representable as Sum of Same Parity Primes in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:35:38

218 Views

We are given an array Arr[] of positive integers of size N. The goal is to count the number of elements in that array which can be represented as sum of parity primes, that is they can be shown as a sum of the same prime number. Ex; 4= 2+2, 6=3+3 or 2+2+2The sum of any two odd or even prime numbers will always be even. And except 0 and 2 all even numbers can be represented as sum of same primes.Let’s understand with examples.Input Arr[] = { 2, 5, 10, 15, 20, 25 }Output Number which satisfy condition : 3Explanation Numbers as ... Read More

Count Number of Trailing Zeros in Product of Array in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:33:04

421 Views

We are given an array Arr[] of positive integers of size N. The goal is to count the number of trailing zeroes present in the product of all elements of the array.We will do this by counting the factors of each number. We will count 2 and 5 as factors of each number as the product of 2 and 5 is 10 which gives 1 trailing 0. In the end whichever count is smaller gives the count of trailing zeroes in the product. If we have 4 2’s and 6 5’s then there will be 4 trailing zeroes in the ... Read More

Count Number of Ways to Divide a Number in Parts in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:31:15

479 Views

We are given a positive number N. The goal is to count the number of ways in which the number N can be divided into 3 parts. The parts may or may not be equal. N lies in range [1, 5000].We will do this by using three for loops for 3 parts of the number. Check at the innermost loop that the sum of all three is equal to N. If true, then increment the count of ways.Let’s understand with examples.Input − N=5Output − Number of ways to divide N in 3 parts: 2Explanation − 5 can be shown as ... Read More

Count Numbers Whose XOR with N is Equal to OR with N in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:30:18

146 Views

We are a number N. The goal is to find numbers between 0 and N whose OR with N is equal to XOR with N.We will do this by traversing no. from i=0 to i

Count Numbers Whose Difference with N Equals XOR with N in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:28:03

150 Views

We are a number N. The goal is to find numbers between 0 and N whose difference with N is equal to XOR with N.We will do this by traversing no. from i=0 to i

Count Numbers Whose Sum with X Equals XOR with X in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:26:37

220 Views

We are a number X. The goal is to find numbers between 0 and X whose sum with X is equal to XOR with X.We will do this by traversing no. from i=0 to i

Count Pieces of the Circle After N Cuts in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:25:05

145 Views

We are given an integer N which represents the number of cuts applied on a 2D-circle. Each circle divides the circle in two halves. Goal is to find the pieces of the circle after N cuts.Number of pieces= 2 * no. of cutsLet’s understand with examples.Input − N=1Output − Pieces of circle: 2Explanation −Input − N=3Output − Pieces of circle: 6Explanation −Approach used in the below program is as followsWe take N for a number of cuts.Take pieces=1*N.Print the result..Example#include using namespace std; int main(){    int N=2;    Int pieces=2*N;    cout

Advertisements