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
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
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
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
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
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
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
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
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
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