Found 7197 Articles for C++

Count numbers whose difference with N is equal to XOR with N in C++

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

133 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

196 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

125 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

205 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

8K+ 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

Count strings that end with the given pattern in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:19:13

184 Views

We are given an array of strings str[] and a pattern string pat. The goal is to find the string elements of str[] that have pattern pat at the end.We will traverse each string of str and compare last characters with pat. If they match incrementLet’s understand with examples.Input str[]={ “kittens”, “hens”, “deers”, “dogs” } pat=”ens”Output Strings that end with given pattern: 2Explanation Strings “kitt-ens” and “h-ens” end with “ens”.Input str[]={ “tickets”, “wickets”, “bats”, “cricket” } pat=”et”Output Strings that end with given pattern: 1Explanation Strings “wick-et” ends with “et”.Approach used in the below program is as followsWe string array str[] and a pattern string pat.N is ... Read More

Count square and non-square numbers before n in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:17:02

976 Views

We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their cubes is N.Naive ApproachTraverse all numbers from 1 to N and check if it is a perfect square. If floor(sqrt(i))==ceil(sqrt(i)).Then the number is a perfect square.Efficient ApproachPerfect squares below N can be found using formula: floor(sqrt(N)).Let’s understand with examples.Input N=20Output Count of square numbers: 4 Count of non-square numbers: 16Explanation Square numbers are 1, 4, 9 and 16. Rest all are non-squares and less than 20.Input N=40Output Count of square numbers: 6 Count of non-square numbers: 34Explanation Square numbers are 1, 4, 9, 16, ... Read More

Count numbers which can be constructed using two numbers in C++

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

294 Views

We are provided three numbers X, Y and N ( to define range [1, N] ). The goal is to find all the numbers in the range [1, N] that can be constructed using X and Y only any number of times..For example if X=2 and Y=3. Number 6 can be constructed using 2 thrice ( 2+2+2 ) or 3 twice ( 3+3 ). Similarly 7 can be constructed using 2 twice and 3 once ( 2+2+3 ).We will do this by subtracting X or Y from each number from 1 to N. If the final number reduces to 0 ... Read More

Count pairs from two arrays having sum equal to K in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:13:20

318 Views

We are given two arrays Arr1[] and Arr2[] and a number K. The goal is to find unique pairs of elements of both arrays such that their sum is K. Pairs will be of form ( Arr1[i], Arr2[j] ) where Arr1[i]+Arr2[j]==K.We will traverse using two loops for i and j. If sum (Arr1[i]+Arr2[j])==K. And the pair doesn’t exist in unordered_map. Add it to the map and increment count.Let’s understand with examples.Input Arr1[]={ 1, 3, 2, 4, 3, 2 }; Arr2[]={ 0, 2, 1, 2, 3 }; K=4Output Number of pairs with sum K : 4Explanation Pairs will be ( Arr1[0], Arr2[4] ) → ... Read More

Count pairs (i,j) such that (i+j) is divisible by both A and B in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:11:27

213 Views

We are given variables N, M, A and B. The goal is to find ordered pairs of positive numbers( i, j ) such that their sum is divisible by both A and B. And 1

Advertisements