codeparrot/xlcost-text-to-code huggingface dataset:
Program to check if tank will overflow , underflow or filled in given time | C ++ program to check if Tank will overflow or not in given time ; function to calculate the volume of tank ; function to print overflow / filled / underflow accordingly ; driver function ; radius of the tank ; height of the tank ; rate of flow of water ; time given ; calculate the required time ; printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; float volume ( int radius , int height ) { return ( ( 22 / 7 ) * radius * radius * height ) ; } void check_and_print ( float required_time , float given_time ) { if ( required_time < given_time ) cout << " Overflow " ; else if ( required_time > given_time ) cout << " Underflow " ; else cout << " Filled " ; } int main ( ) { int radius = 5 , height = 10 , rate_of_flow = 10 ; float given_time = 70.0 ; float required_time = volume ( radius , height ) / rate_of_flow ; check_and_print ( required_time , given_time ) ; return 0 ; }
Program to find third side of triangle using law of cosines | CPP program to find third side of triangle using law of cosines ; Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver program to check the above function ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float cal_cos ( float n ) { float accuracy = 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= fabs ( cosval - cosx ) ) ; return cosx ; } float third_side ( int a , int b , float c ) { float angle = cal_cos ( c ) ; return sqrt ( ( a * a ) + ( b * b ) - 2 * a * b * angle ) ; } int main ( ) { float c = 49 ; int a = 5 , b = 8 ; cout << third_side ( a , b , c ) ; return 0 ; }
Check whether given circle resides in boundary maintained by two other circles | CPP program to check whether circle with given co - ordinates reside within the boundary of outer circle and inner circle ; function to check if given circle fit in boundary or not ; Distance from the center ; Checking the corners of circle ; driver program ; Radius of outer circle and inner circle respectively ; Co - ordinates and radius of the circle to be checked
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fitOrNotFit ( int R , int r , int x , int y , int rad ) { double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ; if ( val + rad <= R && val - rad >= R - r ) cout << " Fits STRNEWLINE " ; else cout << " Doesn ' t ▁ Fit STRNEWLINE " ; } int main ( ) { int R = 8 , r = 4 ; int x = 5 , y = 3 , rad = 3 ; fitOrNotFit ( R , r , x , y , rad ) ; return 0 ; }
Number of Triangles that can be formed given a set of lines in Euclidean Plane | C ++ program to find the number of triangles that can be formed using a set of lines in Euclidean Plane ; double variables can ' t ▁ be ▁ checked ▁ precisely ▁ using ▁ ' == ' this function returns true if the double variables are equal ; This function returns the number of triangles for a given set of lines ; slope array stores the slope of lines ; slope array is sorted so that all lines with same slope come together ; After sorting slopes , count different slopes . k is index in count [ ] . ; int this_count = 1 ; Count of current slope ; calculating sum1 ( Sum of all slopes ) sum1 = m1 + m2 + ... ; calculating sum2 . sum2 = m1 * m2 + m2 * m3 + ... ; int temp [ n ] ; Needed for sum3 ; calculating sum3 which gives the final answer m1 * m2 * m3 + m2 * m3 * m4 + ... ; Driver code ; lines are stored as arrays of a , b and c for ' ax + by = c ' ; n is the number of lines
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define EPSILON  numeric_limits<double>::epsilon() NEW_LINE bool compareDoubles ( double A , double B ) { double diff = A - B ; return ( diff < EPSILON ) && ( - diff < EPSILON ) ; } int numberOfTringles ( int a [ ] , int b [ ] , int c [ ] , int n ) { double slope [ n ] ; for ( int i = 0 ; i < n ; i ++ ) slope [ i ] = ( a [ i ] * 1.0 ) / b [ i ] ; sort ( slope , slope + n ) ; int count [ n ] , k = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( compareDoubles ( slope [ i ] , slope [ i - 1 ] ) ) this_count ++ ; else { count [ k ++ ] = this_count ; this_count = 1 ; } } count [ k ++ ] = this_count ; int sum1 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum1 += count [ i ] ; int sum2 = 0 ; for ( int i = 0 ; i < k ; i ++ ) { temp [ i ] = count [ i ] * ( sum1 - count [ i ] ) ; sum2 += temp [ i ] ; } sum2 /= 2 ; int sum3 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum3 += count [ i ] * ( sum2 - temp [ i ] ) ; sum3 /= 3 ; return sum3 ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 4 , 5 , 5 } ; int c [ ] = { 5 , 7 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " The ▁ number ▁ of ▁ triangles ▁ that " " ▁ can ▁ be ▁ formed ▁ are : ▁ " << numberOfTringles ( a , b , c , n ) ; return 0 ; }
Program to find line passing through 2 Points | C ++ Implementation to find the line passing through two points ; This pair is used to store the X and Y coordinate of a point respectively ; Function to find the line given two points ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define pdd  pair<double, double> NEW_LINE void lineFromPoints ( pdd P , pdd Q ) { double a = Q . second - P . second ; double b = P . first - Q . first ; double c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { cout << " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ " " is : ▁ " << a << " x ▁ - ▁ " << b << " y ▁ = ▁ " << c << endl ; } else { cout << " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ " " is : ▁ " << a << " x ▁ + ▁ " << b << " y ▁ = ▁ " << c << endl ; } } int main ( ) { pdd P = make_pair ( 3 , 2 ) ; pdd Q = make_pair ( 2 , 6 ) ; lineFromPoints ( P , Q ) ; return 0 ; }
Regular polygon using only 1 s in a binary numbered circle | C ++ program to find whether a regular polygon is possible in circle with 1 s as vertices ; method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPolygonWithMidpoints ( int arr [ ] , int N , int midpoints ) { for ( int j = 0 ; j < midpoints ; j ++ ) { int val = 1 ; for ( int k = j ; k < N ; k += midpoints ) { val &= arr [ k ] ; } if ( val && N / midpoints > 2 ) { cout << " Polygon ▁ possible ▁ with ▁ side ▁ length ▁ " << << ( N / midpoints ) << endl ; return true ; } } return false ; } void isPolygonPossible ( int arr [ ] , int N ) { int limit = sqrt ( N ) ; for ( int i = 1 ; i <= limit ; i ++ ) { if ( N % i == 0 ) { if ( checkPolygonWithMidpoints ( arr , N , i ) || checkPolygonWithMidpoints ( arr , N , ( N / i ) ) ) return ; } } cout << " Not ▁ possiblen " ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isPolygonPossible ( arr , N ) ; return 0 ; }
Minimum lines to cover all points | C ++ program to get minimum lines to cover all the points ; Utility method to get gcd of a and b ; method returns reduced form of dy / dx as a pair ; get sign of result ; method returns minimum number of lines to cover all points where all lines goes through ( xO , yO ) ; set to store slope as a pair ; loop over all points once ; get x and y co - ordinate of current point ; if this slope is not there in set , increase ans by 1 and insert in set ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } pair < int , int > getReducedForm ( int dy , int dx ) { int g = gcd ( abs ( dy ) , abs ( dx ) ) ; bool sign = ( dy < 0 ) ^ ( dx < 0 ) ; if ( sign ) return make_pair ( - abs ( dy ) / g , abs ( dx ) / g ) ; else return make_pair ( abs ( dy ) / g , abs ( dx ) / g ) ; } int minLinesToCoverPoints ( int points [ ] [ 2 ] , int N , int xO , int yO ) { set < pair < int , int > > st ; pair < int , int > temp ; int minLines = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int curX = points [ i ] [ 0 ] ; int curY = points [ i ] [ 1 ] ; temp = getReducedForm ( curY - yO , curX - xO ) ; if ( st . find ( temp ) == st . end ( ) ) { st . insert ( temp ) ; minLines ++ ; } } return minLines ; } int main ( ) { int xO , yO ; xO = 1 ; yO = 0 ; int points [ ] [ 2 ] = { { -1 , 3 } , { 4 , 3 } , { 2 , 1 } , { -1 , -2 } , { 3 , -3 } } ; int N = sizeof ( points ) / sizeof ( points [ 0 ] ) ; cout << minLinesToCoverPoints ( points , N , xO , yO ) ; return 0 ; }
Maximum height when coins are arranged in a triangle | C ++ program to find maximum height of arranged coin triangle ; Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver code to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; float squareRoot ( float n ) { float x = n ; float y = 1 ; float e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } int findMaximumHeight ( int N ) { int n = 1 + 8 * N ; int maxH = ( -1 + squareRoot ( n ) ) / 2 ; return maxH ; } int main ( ) { int N = 12 ; cout << findMaximumHeight ( N ) << endl ; return 0 ; }
Number of Integral Points between Two Points | C ++ code to find the number of integral points lying on the line joining the two given points ; Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver program to test above
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; class Point { public : int x , y ; Point ( int a = 0 , int b = 0 ) : x ( a ) , y ( b ) { } } ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int getCount ( Point p , Point q ) { if ( p . x == q . x ) return abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return abs ( p . x - q . x ) - 1 ; return gcd ( abs ( p . x - q . x ) , abs ( p . y - q . y ) ) - 1 ; } int main ( ) { Point p ( 1 , 9 ) ; Point q ( 8 , 16 ) ; cout << " The ▁ number ▁ of ▁ integral ▁ points ▁ between ▁ " << " ( " << p . x << " , ▁ " << p . y << " ) ▁ and ▁ ( " << q . x << " , ▁ " << q . y << " ) ▁ is ▁ " << getCount ( p , q ) ; return 0 ; }
How to check if given four points form a square | A C ++ program to check if four given points form a square or not . ; Structure of a point in 2D space ; A utility function to find square of distance from point ' p ' to point ' q ' ; This function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; int d2 = distSq ( p1 , p2 ) ; from p1 to p2 int d3 = distSq ( p1 , p3 ) ; from p1 to p3 int d4 = distSq ( p1 , p4 ) ; from p1 to p4 ; If lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must met to form a square . 1 ) Square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) Square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; The below two cases are similar to above case ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; struct Point { int x , y ; } ; int distSq ( Point p , Point q ) { return ( p . x - q . x ) * ( p . x - q . x ) + ( p . y - q . y ) * ( p . y - q . y ) ; } bool isSquare ( Point p1 , Point p2 , Point p3 , Point p4 ) { if ( d2 == 0 d3 == 0 d4 == 0 ) return false ; if ( d2 == d3 && 2 * d2 == d4 && 2 * distSq ( p2 , p4 ) == distSq ( p2 , p3 ) ) { return true ; } if ( d3 == d4 && 2 * d3 == d2 && 2 * distSq ( p3 , p2 ) == distSq ( p3 , p4 ) ) { return true ; } if ( d2 == d4 && 2 * d2 == d3 && 2 * distSq ( p2 , p3 ) == distSq ( p2 , p4 ) ) { return true ; } return false ; } int main ( ) { Point p1 = { 20 , 10 } , p2 = { 10 , 20 } , p3 = { 20 , 20 } , p4 = { 10 , 10 } ; isSquare ( p1 , p2 , p3 , p4 ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Count triplets such that product of two numbers added with third number is N | C ++ program for the above approach ; Function to find the divisors of the number ( N - i ) ; Stores the resultant count of divisors of ( N - i ) ; Iterate over range [ 1 , sqrt ( N ) ] ; Return the total divisors ; Function to find the number of triplets such that A * B - C = N ; Loop to fix the value of C ; Adding the number of divisors in count ; Return count of triplets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDivisors ( int n ) { int divisors = 0 ; int i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) { divisors ++ ; } } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) { divisors ++ ; } } return divisors ; } int possibleTriplets ( int N ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { count += countDivisors ( N - i ) ; } return count ; } int main ( ) { int N = 10 ; cout << possibleTriplets ( N ) ; return 0 ; }
Maximize count of planes that can be stopped per second with help of given initial position and speed | C ++ program for the above approach ; Function to find maximum number of planes that can be stopped from landing ; Stores the times needed for landing for each plane ; Iterate over the arrays ; Stores the time needed for landing of current plane ; Update the value of t ; Append the t in set St ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPlanes ( int A [ ] , int B [ ] , int N ) { set < int > St ; for ( int i = 0 ; i < N ; i ++ ) { int t = ( A [ i ] % B [ i ] > 0 ) ? 1 : 0 ; t += ( A [ i ] / B [ i ] ) + t ; St . insert ( t ) ; } return St . size ( ) ; } int main ( ) { int A [ ] = { 1 , 3 , 5 , 4 , 8 } ; int B [ ] = { 1 , 2 , 2 , 1 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maxPlanes ( A , B , N ) ; return 0 ; }
Find the player who will win by choosing a number in range [ 1 , K ] with sum total N | C ++ program for above approach ; Function to predict the winner ; Driver Code ; Given Input ; Function call
#include <iostream> NEW_LINE using namespace std ; void predictTheWinner ( int K , int N ) { if ( N % ( K + 1 ) == 0 ) cout << " Bob " ; else cout << " Alice " ; } int main ( ) { int K = 7 , N = 50 ; predictTheWinner ( K , N ) ; return 0 ; }
Maximize the rightmost element of an array in k operations in Linear Time | C ++ program for above approach ; Function to calculate maximum value of Rightmost element ; Initializing ans to store Maximum valued rightmost element ; Calculating maximum value of Rightmost element ; Printing rightmost element ; Driver Code ; Given Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; void maxRightmostElement ( int N , int k , int arr [ ] ) { int ans = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int d = min ( arr [ i ] / 2 , k / ( N - 1 - i ) ) ; k -= d * ( N - 1 - i ) ; ans += d * 2 ; } cout << ans << endl ; } int main ( ) { int N = 4 , k = 5 , arr [ ] = { 3 , 8 , 1 , 4 } ; maxRightmostElement ( N , k , arr ) ; return 0 ; }
Minimize the maximum element in constructed Array with sum divisible by K | C ++ program for the above approach . ; Function to find smallest maximum number in an array whose sum is divisible by K . ; Minimum possible sum possible for an array of size N such that its sum is divisible by K ; If sum is not divisible by N ; If sum is divisible by N ; Driver code .
#include <iostream> NEW_LINE using namespace std ; int smallestMaximum ( int N , int K ) { int sum = ( ( N + K - 1 ) / K ) * K ; if ( sum % N != 0 ) return ( sum / N ) + 1 ; else return sum / N ; } int main ( ) { int N = 4 ; int K = 3 ; cout << smallestMaximum ( N , K ) << endl ; return 0 ; }
Check if it is possible to construct an Array of size N having sum as S and XOR value as X | C ++ program for the above approach ; Function to find if any sequence is possible or not . ; Since , S is greater than equal to X , and either both are odd or even There always exists a sequence ; Only one case possible is S == X or NOT ; ; Considering the above conditions true , check if XOR of S ^ ( S - X ) is X or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findIfPossible ( int N , int S , int X ) { if ( S >= X and S % 2 == X % 2 ) { if ( N >= 3 ) { return " Yes " ; } if ( N == 1 ) { if ( S == X ) { return " Yes " ; } else { return " No " ; } } if ( N == 2 ) { int C = ( S - X ) / 2 ; int A = C ; int B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { return " Yes " ; } else { return " No " ; } } } else { return " No " ; } } int main ( ) { int N = 3 , S = 10 , X = 4 ; cout << findIfPossible ( N , S , X ) ; return 0 ; }
Check whether each Array element can be reduced to minimum element by replacing it with remainder with some X | C ++ program for the above approach ; Function to check if every integer in the array can be reduced to the minimum array element ; Stores the minimum array element ; Find the minimum element ; Traverse the array arr [ ] ; Stores the maximum value in the range ; Check whether mini lies in the range or not ; Otherwise , return Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isPossible ( int arr [ ] , int n ) { int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) mini = min ( mini , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == mini ) continue ; int Max = ( arr [ i ] + 1 ) / 2 - 1 ; if ( mini < 0 mini > Max ) return " No " ; } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 1 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << isPossible ( arr , N ) ; return 0 ; }
Maximum number which can divide all array element after one replacement | C ++ Implementation for the above approach ; Function to return gcd of two numbers ; If one of numbers is 0 then gcd is other number ; If both are equal then that value is gcd ; One is greater ; Function to return minimum sum ; Initialize min_sum with large value ; Initialize variable gcd ; Storing value of arr [ i ] in c ; Update maxGcd if gcd is greater than maxGcd ; returning the maximum divisor of all elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcdOfTwoNos ( int num1 , int num2 ) { if ( num1 == 0 ) return num2 ; if ( num2 == 0 ) return num1 ; if ( num1 == num2 ) return num1 ; if ( num1 > num2 ) return gcdOfTwoNos ( num1 - num2 , num2 ) ; return gcdOfTwoNos ( num1 , num2 - num1 ) ; } int Min_sum ( int arr [ ] , int N ) { int min_sum = 1000000 , maxGcd = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int gcd ; if ( i == 0 ) gcd = arr [ 1 ] ; else { gcd = arr [ i - 1 ] ; } for ( int j = 0 ; j < N ; j ++ ) { if ( j != i ) gcd = gcdOfTwoNos ( gcd , arr [ j ] ) ; } int c = arr [ i ] ; if ( gcd > maxGcd ) maxGcd = gcd ; } return maxGcd ; } int main ( ) { int arr [ ] = { 16 , 5 , 10 , 25 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << Min_sum ( arr , N ) ; return 0 ; }
Count of distinct N | C ++ Program for the above approach ; Function to find the count of distinct odd integers with N digits using the given digits in the array arr [ ] ; Stores the factorial of a number ; Calculate the factorial of all numbers from 1 to N ; Stores the frequency of each digit ; Stores the final answer ; Loop to iterate over all values of Nth digit i and 1 st digit j ; If digit i does not exist in the given array move to next i ; Fixing i as Nth digit ; Stores the answer of a specific value of i and j ; If digit j does not exist move to the next j ; Fixing j as 1 st digit ; Calculate number of ways to arrange remaining N - 2 digits ; Including j back into the set of digits ; Including i back into the set of the digits ; Return Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddIntegers ( int arr [ ] , int N ) { int Fact [ N ] = { } ; Fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { Fact [ i ] = i * Fact [ i - 1 ] ; } int freq [ 10 ] = { } ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } int ans = 0 ; for ( int i = 1 ; i <= 9 ; i += 2 ) { if ( ! freq [ i ] ) continue ; freq [ i ] -- ; for ( int j = 1 ; j <= 9 ; j ++ ) { int cur_ans = 0 ; if ( freq [ j ] == 0 ) { continue ; } freq [ j ] -- ; cur_ans = Fact [ N - 2 ] ; for ( int k = 0 ; k <= 9 ; k ++ ) { cur_ans = cur_ans / Fact [ freq [ k ] ] ; } ans += cur_ans ; freq [ j ] ++ ; } freq [ i ] ++ ; } return ans ; } int main ( ) { int A [ ] = { 2 , 3 , 4 , 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( int ) ; cout << countOddIntegers ( A , N ) ; return 0 ; }
Count pairs in an array having sum of elements with their respective sum of digits equal | C ++ program for the above approach ; Function to find the sum of digits of the number N ; Stores the sum of digits ; If the number N is greater than 0 ; Return the sum ; Function to find the count of pairs such that arr [ i ] + sumOfDigits ( arr [ i ] ) is equal to ( arr [ j ] + sumOfDigits ( arr [ j ] ) ; Stores the frequency of value of arr [ i ] + sumOfDigits ( arr [ i ] ) ; Traverse the given array ; Find the value ; Increment the frequency ; Stores the total count of pairs ; Traverse the map mp ; Update the count of pairs ; Return the total count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigits ( int N ) { int sum = 0 ; while ( N ) { sum += ( N % 10 ) ; N = N / 10 ; } return sum ; } int CountPair ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] + sumOfDigits ( arr [ i ] ) ; mp [ val ] ++ ; } int count = 0 ; for ( auto x : mp ) { int val = x . first ; int times = x . second ; count += ( ( times * ( times - 1 ) ) / 2 ) ; } return count ; } int main ( ) { int arr [ ] = { 105 , 96 , 20 , 2 , 87 , 96 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountPair ( arr , N ) ; return 0 ; }