Found 7347 Articles for C++

Count numbers have all 1s together in binary representation in C++

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

391 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 which can be represented as sum of same parity primes in C++

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

111 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

263 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

333 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

77 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 is equal to XOR with N in C++

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

101 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 is equal to XOR with x in C++

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

105 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

91 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

Count quadruples from four sorted arrays whose sum is equal to a given value x in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:23:14

118 Views

We are given four arrays A[], B[], C[] and D[]. The goal is to find all quadruples of elements of these arrays such that A[i]+B[j]+C[k]+D[l] =x. All four arrays have the same number of elements N.We will do this by traversing each array once and compare if A[i]+B[j]+C[j]+D[l]==x. If true increment count.Let’s understand with examples.Input A[]={ 1, 2, 3}; B[]={ 2, 3, 2}; C[]={ 4, 3, 1}; D[]={ 3, 1, 1 }; X=12Output Count of Quadruples: 4Explanation Quadruples such as ( A[i] B[j] C[k] D[l] ) are: (2 3 4 3) , (3 2 4 3), (3 3 3 3), (3 2 4 ... Read More

Count Primes in Ranges in C++

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

7K+ Views

We are given range variables START and END. The goal is to find the count of prime numbers in the range [START, END].We will check if number i in range is prime by checking if any number other than 1 fully divides it and is between 1 and i/2. If it is prime. Increment count.Let’s understand with examples.Input Start=1 End=20Output Primes in Ranges : 8Explanation Primes between 1 and 20 are: 2, 3, 5, 7, 11, 13, 17, 19.Input Start=100 End=200Output Primes in Ranges : 21Explanation Primes between 100 and 200 are: 101 103 107 109 113 127 131 137 139 149 151 157 163 167 ... Read More

Advertisements