Found 7197 Articles for C++

Count palindrome words in a sentence in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:30:31

2K+ Views

We are given a string containing an English sentence. The goal is to find the number of words in the string that are palindromes. Palindrome words are those that when read from start or end have the same alphabet sequence. If the sentence is “Madam speaks good Malayalam”, then count of palindrome words is 2. (Madam and Malayalam)Note − Words can contain both upper and lowercase alphabets.Let us understand with examples.Input − str = "My Mom and Anna left at Noon";Output − Count of palindrome words in a sentence are − 3Explanation − Palindrome words in above sentence are − ... Read More

Count paths with distance equal to Manhattan distance in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:28:09

459 Views

We are given variables x1, x2, y1, y2 representing two points on a 2D coordinate system as (x1, y1) and (x2, y2). The goal is to find all the paths that will have distance equal to the Manhattan distance between these two points.Manhattan DistanceManhattan Distance between two points (x1, y1) and (x2, y2) is −MD= |x1 – x2| + |y1 – y2|Let’s take A= |x1 – x2| and B= |y1 – y2|All paths with Manhattan distance equal to MD will have edges count as (A+B). A horizontal edge and B vertical edges. So possible combination of (A+B) edges divided into ... Read More

Count passing car pairs in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:26:13

334 Views

We are given an array of length N containing 0’s and 1’s only. The value 1 represents a car going towards west direction and value 0 represents a car going towards east direction.We count passing cars as 1 if a pair of car A and car B is such that 0

Count pairs of natural numbers with GCD equal to given number in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:25:00

1K+ Views

We gave three input variables as ‘start’, ‘end’ and ‘number’. The goal is to find pairs of numbers between start and end that have GCD value equal to ‘number’. For example GCD(A, B)=number and both A, B are in range [start, end].Let us understand with examples.Input − start=5 end=20 number=8Output − Count of pairs of natural numbers with GCD equal to given number are − 3Explanation − Pairs between 5 to 20 such that GCD is 8 are − (8, 8), (8, 16), (16, 8)Input − start=5 end=20 number=7Output − Count of pairs of natural numbers with GCD equal to ... Read More

Count pairs with average present in the same array in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:20:18

305 Views

We are given an array of integers such that each element of the array is in the range [- 1000, 1000]. The goal is to find pairs of elements of the array such that their average is also present in that array. If array is arr[]= [1, 2, 3, 4]. Then pairs would be (1, 3) and (2, 4) as the average of 1, 3 is 2 and the average of 2, 4 is 3 and both 2 and 3 are present in the array. Count would be 2.Let us understand with examples.Input − arr[]= { -1, 2, 5, -3, ... Read More

Count pairs of numbers from 1 to N with Product divisible by their Sum in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:18:23

253 Views

We are given a number N. The goal is to find the pairs of numbers from 1 to N such that the product of pairs is equal to the sum of pairs.Let us understand with examples.Input − N=11Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 1Explanation − Numbers 3 and 6 have product 18 and their sum 9 fully divides 18.Input − N=30Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 12Explanation − Pairs are − (3, 6), (4, ... Read More

Count the number of carry operations required to add two numbers in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:16:24

2K+ Views

We are given two numbers num_1 and num_2. The goal is to count the number of carry operations required if the numbers are added. If numbers are 123 and 157 then carry operations will be 1. (7+3=10, 1+2+5=8, 1+1=2 ).Let us understand with examplesInput − num_1=432 num_2=638Output − Count of number of carry operations required to add two numbers are − 2Explanation − From right to left adding digits and counting carry −(2+9=10, carry 1 ) count=1, (1+3+3=7, carry 0 ) count=1, (4+6=10, carry 1 ) count=2Input − num_1=9999 num_2=111Output − Count of number of carry operations required to add ... Read More

Program to count how many numbers should be appended to create all numbers from 1 to k in C++

Arnab Chakraborty
Updated on 02-Dec-2020 05:34:56

92 Views

Suppose we have a list of numbers called nums and another value k. We have to find the minimum number of numbers that we need to insert into nums such that we can make any number from [1, k] using some subset in nums.So, if the input is like nums = [3, 5], k = 6, then the output will be 2, as we have to insert 1, 2, so we can make : 1 = [1], 2 = [2], 3 = [3], 4 = [1, 3], 5 = [5], 6 = [1, 5].To solve this, we will follow these ... Read More

Count positive integers with 0 as a digit and maximum ‘d' digits in C++

Sunidhi Bansal
Updated on 01-Dec-2020 13:06:56

134 Views

We are given a number d which represents the number of digits. The goal is to find the count of positive integers with 0 as a digit and have maximum d digits. Count all 1 digit, 2 digit, 3 digit….d digit positive numbers containing at least one 0.We will first find numbers the count of numbers that have d digits with at least one 0. Let’s say d=3. To make a 3-digit number with at least one 0, possible ways are −Here d1 can have 1 to 9 : 9 ways d2 can have 0-9 : 10 ways d3 can ... Read More

Count the number of rhombi possible inside a rectangle of given size in C++

Sunidhi Bansal
Updated on 01-Dec-2020 13:04:39

215 Views

We are given a rectangle with dimensions as height X width. The rectangle is represented on a 2D coordinate system with the left-lower corner at point (0, 0). So the goal is to count the number of rhombi possible inside this rectangle such that all these conditions are met −The rhombus has an area more than 0.The diagonals of the rhombus are parallel to the x and y axis.The rhombus has integer coordinates for all corners.Let us understand with examplesInput − length=3 width=3Output − Count of number of rhombi possible inside a rectangle of given size are: 4Explanation − Below ... Read More

Advertisements