C++ Articles - Page 292 of 719

Count words in a given string in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:50:51

4K+ Views

We are given with a sentence or string containing words that can contain spaces, new line characters and tab characters in between. The task is to calculate the total number of words in a string and print the result.Input − string str = “welcome to tutorials point\t”Output − Count of words in a string are − 4Explanation − There are four words in a string i.e. welcome, to, tutorials, point and the rest are spaces(“ ”), next line character() and tab character(\t) present between the words.Input − string str = “honesty\t is the best policy”Output − Count of words in ... Read More

Counting frequencies of array elements in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:48:21

16K+ Views

We are given an array of integer elements which contains duplicate values and the task is to calculate the frequencies of the distinct elements present in an array and print the result.Input − int arr[] = {1, 1, 2, 3, 4, 1, 2, 3}Output −frequency of 1 is: 3 frequency of 2 is: 2 frequency of 3 is: 2 Frequency of 4 is: 1Input − int arr[] = {2, 3, 4, 1, 5}Output −frequency of 1 is: 1 frequency of 2 is: 1 frequency of 3 is: 1 Frequency of 4 is: 1 Frequency of 5 is: 1Approach used in ... Read More

Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:43:35

327 Views

We are given two numbers L and R that define a range [L, R]. The goal is to find all numbers between L and R that are even, and sum of whose digits is divisible by 3.We will do this by calculating the sum of digits of all even numbers between L and R and increment count if that sum%3==0.Let us understand with examples.Input − L=10, R=20Output − Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3: 2Explanation − Numbers between 10 and 20 that are even. 10, 12, 14, 16, ... Read More

Count number of substrings with numeric value greater than X in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:40:51

282 Views

We are given a string of numbers 0 to 9. The string represents a decimal number. The goal is to find all substrings that represent a decimal number which is greater than number X. Condition is that substring should not start with 0. I.e in “2021”, “02”, “021”. “0” will not be included.We will do this by checking the first value of all substrings, if it is greater than 0 then start making substrings from that index by converting them into integers using stoi(). If substring>X increment count.Let us understand with examples.Input − str=”123” X=12Output − Count of number of ... Read More

Counting even decimal value substrings in a binary string in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:37:31

237 Views

We are given with a string of 0’s and 1’s only. The string represents a binary number read from left to right. i.e. 001 is 4 and not 1. The goal is to find all substrings that represent an even decimal number.We will do this by checking the first value of all substrings, if it is 0 then number is even if 1 then number will be odd. Increment count by length-i as all substrings with this sbstr[0]=’0’ will be even in decimal.Let us understand with examples.Input − str=”101”Output − Count of even decimal value substrings in a binary string ... Read More

Count of quadruplets with given Sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:35:16

243 Views

We are given with four arrays. Goal is to find quadruplets of elements from the four arrays that have sum equal to a given Sum value. The elements selected should be such that all 4 elements belong to different arrays.We will do this by traversing all arrays using for loop and check if A[i]+B[j]+C[k]+D[l]==sum. If yes increment count.Let us understand with examples −Input −A[]={ 1, 3, 1}, B[]={ 2, 4, 5 } , C[]={ 1, 1, 2 } , D[]= { 4, 4, 0} Sum=5Output − Count of quadruplets with given sum are − 2Explanation −2 quadrutplets are: (A[0], B[0], ... Read More

Maximize number of nodes which are not part of any edge in a Graph in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:31:49

129 Views

We are given a graph containing nodes and edges. The goal is to find the maximum number of possible nodes that are connected to any edge of the graph. We know that no. of nodes will always be less than or equal to the number of edges in a complete graph.We will do this by trying to make a complete graph where the number of nodes is n then there will be n(n-1)/2 edges.edge=n(n-1)/2 (here n for nodes )2*edge=n(n-1). Once n(n-1)> no. of edges then we have extra nodes. So iterate from i=1 to i=n.Till i(i-1)>2*edge. Return n-i as result.Let ... Read More

Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:29:59

251 Views

We are given an array of integers. The goal is to count numbers such that the absolute difference between the sum of all elements and that element is greater than variable k.We will do this by getting the sum of elements of the array. Now for each element arr[i], check if −sum-2(arr[i])>k, as sum already includes arr[i] so subtract it twice. If true increment count.Let’s understand with examples.Input − arr[]= { 1, 2, 3, 0, 3, 2, 0, 1 }, k=10Output − Count of elements: 2Explanation − Sum of elements is 1212-1-1=10, 12-2-2=8, 12-3-3=6, 12-0-0=12.Only 12 > 10, so for ... Read More

Count of numbers which can be made power of 2 by given operation in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:27:46

370 Views

We are given an array of positive integers. The goal is to find the count of numbers that can be made power of two by adding 1 to them utmost once.We will check using log2(i) that number is power of two or can become power of two by adding 1 to it. If yes increment count.Let’s understand with examples.Input − arr[]= {1, 3, 2, 5, 6 }, Output − Count of numbers that can become power of 2: 3Explanation − 1+1=2 → 21 , 3+1=4 → 22 , 2=21 others will become 5+1=6, 6+1=7Input − arr[]= {2, 4, 8, 16 ... Read More

Count of only repeated element in a sorted array of consecutive elements in C++

Sunidhi Bansal
Updated on 31-Aug-2020 08:21:18

5K+ Views

We are given an array of consecutive numbers of length n. The array has only one number which is repeated more than once. The goal is to get the number of times that element is repeated in the array. Or we can say find the length of a repeated element in the array.We will traverse the array from i=0 to i

Advertisements