Found 7197 Articles for C++

Count of lines required to write the given String in C++

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

486 Views

We are given a string Str of alphabets and an array widths[]containing width of all English alphabets. The goal is to find the number of lines required to print this string on a page which has a width of 10 characters. Also print remaining characters.We will traverse the string check width of the current character and add it, if this sum >=10 increment number of lines.Let’s understand with examples.Input Str = "ababababab" widths[] = {2, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1};Output Count of ... Read More

Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:44:50

193 Views

We are given two arrays containing prime and non prime numbers. The goal is to find the count of distinct sums of pairs of prime numbers in each array.We will do this by making a pair of two primes from each array, take their sum and add them to set sums. In the end the size of the set is the number of distinct sums of primes.Let’s understand with examples.Input Arr1[] = { 1, 2, 3 } Arr2[] = { 2, 3, 4}Output Distinct Sums of primes :3Explanation Prime pairs (2, 2), (2, 3), (3, 2), (3, 3). Unique sums are 4, 5, ... Read More

Count ways to spell a number with repeated digits in C++

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

212 Views

We are given a number containing many repeated digits as a string. The goal is to find the number of ways to spell it. For example 112233 can be spelled as double one, double two double three or one one two two three three.We will do this by checking continuous numbers. If the number is “13” there is only one way to spell it as “one three” (20). If digits are “113” ways “double one three”, “one one three” (21). So, the approach is to count the one continuous digit in string and multiply 2(count-1) with the previous result.Let’s understand ... Read More

Count ways to reach the nth stair using step 1, 2 or 3 in C++

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

566 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) of points in 2-D space that satisfy the given condition in C++

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

324 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 have all 1s together in binary representation in C++

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

479 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

194 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

381 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

449 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

124 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

Advertisements