Found 7347 Articles for C++

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

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

115 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

741 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

174 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

211 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

134 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

Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++

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

243 Views

We are given a number N. The goal is to find ordered pairs of positive numbers such that the sum of their squares is N.We will do this by finding solutions to the equation a2+ b2 = N. Where a is not more than square root of N and b can be calculated as square root of (N-a2).Let’s understand with examples.Input N=100Output Count of pairs of (a, b) where a^3+b^3=N: 2Explanation Pairs will be (6, 8) and (8, 6). 62+82=36+64=100Input N=11Output Count of pairs of (a, b) where a^3+b^3=N: 0Explanation No such pairs possible.Approach used in the below program is as followsWe take integer N.Function squareSum(int ... Read More

Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++

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

280 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.We will do this by finding solutions to the equation a3 + b3 = N. Where a is not more than cube root of N and b can be calculated as cube root of (N-a3).Let’s understand with examples.Input N=35Output Count of pairs of (a, b) where a^3+b^3=N: 2Explanation Pairs will be (2, 3) and (3, 2). 23+33=8+27=35Input N=100Output Count of pairs of (a, b) where a^3+b^3=N: 0Explanation No such pairs possible.Approach used in the below program is as followsWe take integer N.Function ... Read More

Count ordered pairs with product less than N in C++

Sunidhi Bansal
Updated on 31-Oct-2020 05:06:07

80 Views

We are given a number N. The goal is to find ordered pairs of positive numbers such that their product is less than N.We will do this by starting from i=1 to i

Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++

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

193 Views

We are given two numbers S and K. The goal is to find ordered pairs of positive numbers such that their sum is S and XOR is K.We will do this by starting from i=1 to i

Count of smaller or equal elements in the sorted array in C++

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

977 Views

We are given an array of integers. The goal is to find the count of elements of an array which are less than or equal to the given value K.Input Arr[]= { 1, 2, 3, 14, 50, 69, 90 } K=12Output Numbers smaller or equal to K: 3Explanation Numbers 1, 2, 3 is smaller or equal to 12.Input Arr[]= { 12, 13, 13, 13, 14, 50, 54, 100 } K=14Output Numbers smaller or equal to K: 5Explanation Numbers 12, 13, 14 are smaller or equal to 14.Naive ApproachApproach used in the below program is as followsWe take the integer array Arr[] and K.Function smallorEqual(int arr[], int ... Read More

Advertisements