C++ Articles

Page 50 of 597

Disarium Number with examples in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

A number whose sum of its digits powered with its respective position equals to the number itself is called a disarium number.The noOfDigits(int num) function takes the number and return the number of digits by constantly dividing the number by 10 while there is only ones place left. On each iteration the digits variable is incremented to keep the digits track and is returned once the while loop ends.int noOfDigits(int num){    int digits = 0;    int temp = num;    while (temp){       temp= temp/10;       digits++;    }    return digits; }Next, isDisarium(int ...

Read More

Count pairs from two BSTs whose sum is equal to a given value x in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 331 Views

We are given two binary search trees as input and a variable x. The goal is to find pairs of nodes from each tree such that the sum of value of nodes is equal to x. Take node 1 from BST_1 and node 2 from BST_2 and add data part of both. If sum=x. Increment count.Let us understand with examples.Input Output − Count of pairs from two BSTs whose sum is equal to a given value x are − 1Explanation − The pair is (8, 6)Input Output −Count of pairs from two BSTs whose sum is equal to a given value x ...

Read More

Count of subsequences having maximum distinct elements in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 425 Views

We are given an array arr[] containing integers only. The goal is to find the number of subsequences of arr[] such that they have maximum number distinct elements. If the array is [ 4, 1, 2, 3, 4 ] then two subsequences will be [ 4, 1, 2, 3 ] and [ 1, 2, 3, 4 ].Let us understand with examplesInput − arr[]= { 1, 3, 5, 4, 2, 3, 1 }Output − Count of subsequences having maximum distinct elements are − 4Explanation − The maximum distinct elements are 1, 2, 3, 4 and 5. Count is 5. Subsequences will ...

Read More

Determine the number of squares of unit area that a line will pass through in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 199 Views

The objective is to determine the number of squares a line will pass through given two endpoints (x1, y1) and (x2, y2).To find the number of squares through which our line pass we need to find : difference between the x points (dx) = x2-x1, difference between the y points (dy) = y2-y1, adding the dx and dy and subtracting by their gcd (result) = dx + dy – gcd(dx, dy).The unitSquares(int x1, int y1, int x2, int y2) function takes four values x1, y1 and x2, y2. The absolute difference between the x2 and x1 and the absolute difference ...

Read More

Count of unique pairs (arr[i], arr[j]) such that i < j in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 924 Views

We are given an array containing integer elements. The goal is to find unique pairs of elements of array such that pairs of type (arr[i],arr[j]) have indexes such that iLet us understand with examplesInput − arr[] = {1,2,3};Output − Count of unique pairs (arr[i], arr[j]) such that i < j are − 3Explanation − As all elements are unique. Pairs would be −(1,2) - ( arr[0],arr[1] ) 0

Read More

Count of values of x <= n for which (n XOR x) = (n – x) in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 437 Views

We are given a number n as input. The goal is to find values x such that condition (n xor x)=(nx) holds. Also x lies in range [0,n].Let us understand with examplesInput − n=10Output − Count of values of x

Read More

Diagonal of a Regular Hexagon in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 212 Views

The regular hexagons are comprised of six equilateral triangles so the diagonal of a regular hexagon would be 2*side.ExampleLet us see the following implementation to get the regular Heptagon diagonal from its side −#include using namespace std; int main(){    float side = 12;    if (side < 0)       return -1;    float diagonal = 2*side;    cout

Read More

Count of subarrays whose maximum element is greater than k in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 392 Views

We are given an array arr[] containing integer elements and a variable k . The goal is to find the count of subarrays of arr[] that have greatest/maximum element more that k. If the array is [1, 2, 3] and k is 1. Then possible subarrays are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]. The subarrays with maximum element > 1 are [2], [3], [1, 2], [2, 3], [1, 2, 3]. So the count is 5.Let us understand with examplesInput − arr[] = {1, 2, 5, 3 } k=3Output − Count of subarrays whose maximum element is ...

Read More

Check if a string can become empty by recursively deleting a given sub-string in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 253 Views

Suppose, we are given two strings, str1 and str2. str2 is a substring of str1, and we can delete str2 from str1. It is possible, that the string str2 appears multiple times in str1. Our goal here is to find out if str1 becomes a null string if we keep removing str2 from str1 multiple times. If it is possible we return 1, otherwise 0.So, if the input is like str1 = "CCCPPPPPP", str2 = "CPP"; then the output will be true.To solve this, we will follow these steps −while size of str1 > 0, do −index := return the ...

Read More

Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 331 Views

We are given a string str[] containing ‘a’, ‘b’ and ‘c’ only. The goal is to find the substrings of str[] such that all the three characters are not part of that substring. For any string str, substrings could be “a”, “b”, “c”, “abb”, “bba”, “bc”, “ca”, “ccc” but not “abc”, “bcca” , “cab” as these have ‘a’, ‘b’ and ‘c’, all three.Let us understand with examples.Input − str[] = “aabc”Output − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 8Explanation − Substrings will be : ...

Read More
Showing 491–500 of 5,962 articles
« Prev 1 48 49 50 51 52 597 Next »
Advertisements