codeparrot/xlcost-text-to-code huggingface dataset:
Probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | C ++ implementation of the approach ; Function to return the probability of getting the maximum pair sum when a random pair is chosen from the given array ; Initialize the maximum sum , its count and the count of total pairs ; For every single pair ; Get the sum of the current pair ; If the sum is equal to the current maximum sum so far ; Increment its count ; If the sum is greater than the current maximum ; Update the current maximum and re - initialize the count to 1 ; Find the required probability ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float findProb ( int arr [ ] , int n ) { long maxSum = INT_MIN , maxCount = 0 , totalPairs = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( sum == maxSum ) { maxCount ++ ; } else if ( sum > maxSum ) { maxSum = sum ; maxCount = 1 ; } totalPairs ++ ; } } float prob = ( float ) maxCount / ( float ) totalPairs ; return prob ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findProb ( arr , n ) ; return 0 ; }
Maximum count of common divisors of A and B such that all are co | C ++ implementation of the approach ; Function to return the count of common factors of a and b such that all the elements are co - prime to one another ; GCD of a and b ; Include 1 initially ; Find all the prime factors of the gcd ; If gcd is prime ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCommonFactors ( int a , int b ) { int gcd = __gcd ( a , b ) ; int ans = 1 ; for ( int i = 2 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { ans ++ ; while ( gcd % i == 0 ) gcd /= i ; } } if ( gcd != 1 ) ans ++ ; return ans ; } int main ( ) { int a = 12 , b = 18 ; cout << maxCommonFactors ( a , b ) ; return 0 ; }
Find the day number in the current year for the given date | C ++ implementation of the approach ; Function to return the day number of the year for the given date ; Extract the year , month and the day from the date string ; If current year is a leap year and the date given is after the 28 th of February then it must include the 29 th February ; Add the days in the previous months ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int days [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int dayOfYear ( string date ) { int year = stoi ( date . substr ( 0 , 4 ) ) ; int month = stoi ( date . substr ( 5 , 2 ) ) ; int day = stoi ( date . substr ( 8 ) ) ; if ( month > 2 && year % 4 == 0 && ( year % 100 != 0 year % 400 == 0 ) ) { ++ day ; } while ( month -- > 0 ) { day = day + days [ month - 1 ] ; } return day ; } int main ( ) { string date = "2019-01-09" ; cout << dayOfYear ( date ) ; return 0 ; }
Find the number of cells in the table contains X | CPP program to find number of cells in the table contains X ; Function to find number of cells in the table contains X ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Cells ( int n , int x ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( x % i == 0 && x / i <= n ) ans ++ ; return ans ; } int main ( ) { int n = 6 , x = 12 ; cout << Cells ( n , x ) ; return 0 ; }
Smallest power of 4 greater than or equal to N | C ++ implementation of above approach ; Function to return the smallest power of 4 greater than or equal to n ; If n is itself is a power of 4 then return n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextPowerOfFour ( int n ) { int x = floor ( sqrt ( sqrt ( n ) ) ) ; if ( pow ( x , 4 ) == n ) return n ; else { x = x + 1 ; return pow ( x , 4 ) ; } } int main ( ) { int n = 122 ; cout << nextPowerOfFour ( n ) ; return 0 ; }
Minimum operations required to convert X to Y by multiplying X with the given co | C ++ implementation of the approach ; Function to return the minimum operations required ; Not possible ; To store the greatest power of p that divides d ; While divible by p ; To store the greatest power of q that divides d ; While divible by q ; If d > 1 ; Since , d = p ^ a * q ^ b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int x , int y , int p , int q ) { if ( y % x != 0 ) return -1 ; int d = y / x ; int a = 0 ; while ( d % p == 0 ) { d /= p ; a ++ ; } int b = 0 ; while ( d % q == 0 ) { d /= q ; b ++ ; } if ( d != 1 ) return -1 ; return ( a + b ) ; } int main ( ) { int x = 12 , y = 2592 , p = 2 , q = 3 ; cout << minOperations ( x , y , p , q ) ; return 0 ; }
Number of Quadruples with GCD equal to K | C ++ implementation of the approach ; Function to calculate NC4 ; Base case to calculate NC4 ; Function to return the count of required quadruples using Inclusion Exclusion ; Effective N ; Iterate over 2 to M ; Number of divisors of i till M ; Count stores the number of prime divisors occurring exactly once ; To prevent repetition of prime divisors ; If repetition of prime divisors present ignore this number ; If prime divisor count is odd subtract it from answer else add ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nCr ( int n ) { if ( n < 4 ) return 0 ; int answer = n * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ; answer /= 24 ; return answer ; } int countQuadruples ( int N , int K ) { int M = N / K ; int answer = nCr ( M ) ; for ( int i = 2 ; i < M ; i ++ ) { int j = i ; int temp2 = M / i ; int count = 0 ; int check = 0 ; int temp = j ; while ( j % 2 == 0 ) { count ++ ; j /= 2 ; if ( count >= 2 ) break ; } if ( count >= 2 ) { check = 1 ; } for ( int k = 3 ; k <= sqrt ( temp ) ; k += 2 ) { int cnt = 0 ; while ( j % k == 0 ) { cnt ++ ; j /= k ; if ( cnt >= 2 ) break ; } if ( cnt >= 2 ) { check = 1 ; break ; } else if ( cnt == 1 ) count ++ ; } if ( j > 2 ) { count ++ ; } if ( check ) continue ; else { if ( count % 2 == 1 ) { answer -= nCr ( temp2 ) ; } else { answer += nCr ( temp2 ) ; } } } return answer ; } int main ( ) { int N = 10 , K = 2 ; cout << countQuadruples ( N , K ) ; return 0 ; }
Find the number which when added to the given ratio a : b , the ratio changes to c : d | C ++ implementation of the approach ; Function to return the required number X ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getX ( int a , int b , int c , int d ) { int X = ( b * c - a * d ) / ( d - c ) ; return X ; } int main ( ) { int a = 2 , b = 3 , c = 4 , d = 5 ; cout << getX ( a , b , c , d ) ; return 0 ; }
Number of ways to arrange a word such that no vowels occur together | C ++ code for above approach ; Function to check if a character is vowel or consonent ; Function to calculate factorial of a number ; Calculating no of ways for arranging vowels ; Iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; calculating no of ways to arrange the given word such that all vowels come together ; calculate no of ways to arrange vowels ; to store denominator of fraction ; count of consonents ; calculate the number of ways to arrange the word such that all vowels come together ; To calculate total number of permutations ; To store length of the given word ; denominator of fraction ; return total number of permutations of the given word ; Function to calculate number of permutations such that no vowels come together ; to store frequency of character ; count frequency of all characters ; calculate total number of permutations ; calculate total number of permutations such that all vowels come together ; substrat vwl_tgthr from total to get the result ; return the result ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } ll fact ( ll n ) { if ( n < 2 ) return 1 ; return n * fact ( n - 1 ) ; } ll only_vowels ( map < char , int > & freq ) { ll denom = 1 ; ll cnt_vwl = 0 ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) { if ( isVowel ( itr -> first ) ) { denom *= fact ( itr -> second ) ; cnt_vwl += itr -> second ; } } return fact ( cnt_vwl ) / denom ; } ll all_vowels_together ( map < char , int > & freq ) { ll vow = only_vowels ( freq ) ; ll denom = 1 ; ll cnt_cnst = 0 ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) { if ( ! isVowel ( itr -> first ) ) { denom *= fact ( itr -> second ) ; cnt_cnst += itr -> second ; } } ll ans = fact ( cnt_cnst + 1 ) / denom ; return ( ans * vow ) ; } ll total_permutations ( map < char , int > & freq ) { ll cnt = 0 ; ll denom = 1 ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) { denom *= fact ( itr -> second ) ; cnt += itr -> second ; } return fact ( cnt ) / denom ; } ll no_vowels_together ( string & word ) { map < char , int > freq ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) { char ch = tolower ( word [ i ] ) ; freq [ ch ] ++ ; } ll total = total_permutations ( freq ) ; ll vwl_tgthr = all_vowels_together ( freq ) ; ll res = total - vwl_tgthr ; return res ; } int main ( ) { string word = " allahabad " ; ll ans = no_vowels_together ( word ) ; cout << ans << endl ; word = " geeksforgeeks " ; ans = no_vowels_together ( word ) ; cout << ans << endl ; word = " abcd " ; ans = no_vowels_together ( word ) ; cout << ans << endl ; return 0 ; }
Program to find the number of men initially | C ++ implementation of above approach . ; Function to return the number of men initially ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfMen ( int D , int m , int d ) { int Men = ( m * ( D - d ) ) / d ; return Men ; } int main ( ) { int D = 5 , m = 4 , d = 4 ; cout << numberOfMen ( D , m , d ) ; return 0 ; }
Area of triangle formed by the axes of co | C ++ program area of triangle formed by the axes of co - ordinates and a given straight line ; Function to find area ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double area ( double a , double b , double c ) { double d = fabs ( ( c * c ) / ( 2 * a * b ) ) ; return d ; } int main ( ) { double a = -2 , b = 4 , c = 3 ; cout << area ( a , b , c ) ; return 0 ; }
Sum of two numbers where one number is represented as array of digits | C ++ implementation of the approach ; Function to return the vector containing the answer ; Vector v is to store each digits sum and vector ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > addToArrayForm ( vector < int > & A , int K ) { vector < int > v , ans ; int rem = 0 ; int i = 0 ; for ( i = A . size ( ) - 1 ; i >= 0 ; i -- ) { int my = A [ i ] + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . push_back ( my % 10 ) ; } else { v . push_back ( my ) ; rem = 0 ; } K = K / 10 ; } while ( K > 0 ) { int my = K % 10 + rem ; v . push_back ( my % 10 ) ; if ( my / 10 > 0 ) rem = 1 ; else rem = 0 ; K = K / 10 ; } if ( rem > 0 ) v . push_back ( rem ) ; for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) ans . push_back ( v [ i ] ) ; return ans ; } int main ( ) { vector < int > A { 2 , 7 , 4 } ; int K = 181 ; vector < int > ans = addToArrayForm ( A , K ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] ; return 0 ; }
Compute maximum of the function efficiently over all sub | C ++ implementation of the above approach ; Function to return maximum sum of a sub - array ; Function to return maximum value of function F ; Compute arrays B [ ] and C [ ] ; Find maximum sum sub - array of both of the arrays and take maximum among them ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  100005 NEW_LINE using namespace std ; int kadaneAlgorithm ( const int * ar , int n ) { int sum = 0 , maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; if ( sum < 0 ) sum = 0 ; maxSum = max ( maxSum , sum ) ; } return maxSum ; } int maxFunction ( const int * arr , int n ) { int b [ MAX ] , c [ MAX ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i & 1 ) { b [ i ] = abs ( arr [ i + 1 ] - arr [ i ] ) ; c [ i ] = - b [ i ] ; } else { c [ i ] = abs ( arr [ i + 1 ] - arr [ i ] ) ; b [ i ] = - c [ i ] ; } } int ans = kadaneAlgorithm ( b , n - 1 ) ; ans = max ( ans , kadaneAlgorithm ( c , n - 1 ) ) ; return ans ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxFunction ( arr , n ) ; return 0 ; }
Third last digit in 5 ^ N for given N | C ++ implementation of the above approach ; Function to find the element ; if n < 3 ; If n is even return 6 If n is odd return 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findThirdDigit ( int n ) { if ( n < 3 ) return 0 ; return n & 1 ? 1 : 6 ; } int main ( ) { int n = 7 ; cout << findThirdDigit ( n ) ; return 0 ; }
Probability of A winning the match when individual probabilities of hitting the target given | C ++ implementation of the approach ; Function to return the probability of A winning ; p and q store the values of fractions a / b and c / d ; To store the winning probability of A ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double getProbability ( int a , int b , int c , int d ) { double p = ( double ) a / ( double ) b ; double q = ( double ) c / ( double ) d ; double ans = p * ( 1 / ( 1 - ( 1 - q ) * ( 1 - p ) ) ) ; return ans ; } int main ( ) { int a = 1 , b = 2 , c = 10 , d = 11 ; cout << getProbability ( a , b , c , d ) ; return 0 ; }
Largest palindromic number in an array | C ++ implementation of above approach ; Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; If a palindrome larger than the currentMax is found ; Return the largest palindromic number from the array ; Driver program ; print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } int largestPalindrome ( int A [ ] , int n ) { int currentMax = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] > currentMax && isPalindrome ( A [ i ] ) ) currentMax = A [ i ] ; } return currentMax ; } int main ( ) { int A [ ] = { 1 , 232 , 54545 , 999991 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << largestPalindrome ( A , n ) ; return 0 ; }
Reduce the array to a single element with the given operation | C ++ implementation of the approach ; Function to return the final element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long getFinalElement ( long n ) { long finalNum ; for ( finalNum = 2 ; finalNum * 2 <= n ; finalNum *= 2 ) ; return finalNum ; } int main ( ) { int N = 12 ; cout << getFinalElement ( N ) ; return 0 ; }
Sum of elements in an array having prime frequency | C ++ program to find sum of elements in an array having prime frequency ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the sum of elements in an array having prime frequency ; Map is used to store element frequencies ; Traverse the map using iterators ; Count the number of elements having prime frequencies ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } int sumOfElements ( int arr [ ] , int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , n + 1 ) ; int i , j ; unordered_map < int , int > m ; for ( i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; int sum = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) { if ( prime [ it -> second ] ) { sum += ( it -> first ) ; } } return sum ; } int main ( ) { int arr [ ] = { 5 , 4 , 6 , 5 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumOfElements ( arr , n ) ; return 0 ; }
Sum of all odd length palindromic numbers within the range [ L , R ] | C ++ program to find the sum of all odd length palindromic numbers within the given range ; Function that returns true if the given number is a palindrome ; Here we are generating a new number ( reverse_num ) * by reversing the digits of original input number ; If the original input number ( num ) is equal to * to its reverse ( reverse_num ) then its palindrome * else it is not . ; Function that returns true if the given number is of odd length ; Function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( int num ) { int reverse_num = 0 , remainder , temp ; temp = num ; while ( temp != 0 ) { remainder = temp % 10 ; reverse_num = reverse_num * 10 + remainder ; temp /= 10 ; } if ( reverse_num == num ) { return true ; } return false ; } bool isOddLength ( int num ) { int count = 0 ; while ( num > 0 ) { num /= 10 ; count ++ ; } if ( count % 2 != 0 ) { return true ; } return false ; } long sumOfAllPalindrome ( int L , int R ) { long sum = 0 ; if ( L <= R ) for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) && isOddLength ( i ) ) { sum += i ; } } return sum ; } int main ( ) { int L = 110 , R = 1130 ; cout << " ▁ " << sumOfAllPalindrome ( L , R ) << endl ; }
Number of ways to arrange a word such that all vowels occur together | C ++ program to calculate the no . of ways to arrange the word having vowels together ; Factorial of a number ; calculating ways for arranging consonants ; Ignore vowels ; calculating ways for arranging vowels ; Function to count total no . of ways ; Count vowels and consonant ; total no . of ways ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll fact ( int n ) { ll f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f = f * i ; return f ; } ll waysOfConsonants ( int size1 , int freq [ ] ) { ll ans = fact ( size1 ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) continue ; else ans = ans / fact ( freq [ i ] ) ; } return ans ; } ll waysOfVowels ( int size2 , int freq [ ] ) { return fact ( size2 ) / ( fact ( freq [ 0 ] ) * fact ( freq [ 4 ] ) * fact ( freq [ 8 ] ) * fact ( freq [ 14 ] ) * fact ( freq [ 20 ] ) ) ; } ll countWays ( string str ) { int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; int vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] != ' a ' && str [ i ] != ' e ' && str [ i ] != ' i ' && str [ i ] != ' o ' && str [ i ] != ' u ' ) consonant ++ ; else vowel ++ ; } return waysOfConsonants ( consonant + 1 , freq ) * waysOfVowels ( vowel , freq ) ; } int main ( ) { string str = " geeksforgeeks " ; cout << countWays ( str ) << endl ; return 0 ; }
Sum of Fibonacci Numbers with alternate negatives | C ++ Program to find alternate sum of Fibonacci numbers ; Computes value of first fibonacci numbers and stores their alternate sum ; Initialize result ; Add remaining terms ; For even terms ; For odd terms ; Return the alternating sum ; Driver program to test above function ; Get n ; Find the alternating sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateAlternateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ n + 1 ] ; fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ; int sum = pow ( fibo [ 0 ] , 2 ) + pow ( fibo [ 1 ] , 2 ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum -= fibo [ i ] ; else sum += fibo [ i ] ; } return sum ; } int main ( ) { int n = 8 ; cout << " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " << n << " ▁ terms : ▁ " << calculateAlternateSum ( n ) << endl ; return 0 ; }
Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ... | CPP Program to find Nth term ; Function that will return nth term ; Driver Code ; Get n ; Get the value ; Get n ; Get the value
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getValue ( int n ) { int i = 0 , k = 1 ; while ( i < n ) { i = i + k ; k = k * 2 ; } return k / 2 ; } int main ( void ) { int n = 9 ; cout << getValue ( n ) << endl ; n = 1025 ; cout << getValue ( n ) << endl ; }
Construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | CPP implementation of above approach ; Function that traverses digits in a number and modifies frequency count array ; Array to keep count of digits ; Traversing through x ^ 1 to x ^ n ; For power function , both its parameters are to be in double ; calling countDigits function on x ^ i ; Printing count of digits 0 - 9 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDigits ( double val , long arr [ ] ) { while ( ( long ) val > 0 ) { long digit = ( long ) val % 10 ; arr [ ( int ) digit ] ++ ; val = ( long ) val / 10 ; } return ; } void countFrequency ( int x , int n ) { long freq_count [ 10 ] = { 0 } ; for ( int i = 1 ; i <= n ; i ++ ) { double val = pow ( ( double ) x , ( double ) i ) ; countDigits ( val , freq_count ) ; } for ( int i = 0 ; i <= 9 ; i ++ ) { cout << freq_count [ i ] << " ▁ " ; } } int main ( ) { int x = 15 , n = 3 ; countFrequency ( x , n ) ; }
Number of values of b such that a = b + ( a ^ b ) | C ++ program to find the number of values of b such that a = b + ( a ^ b ) ; function to return the number of solutions ; check for every possible value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSolutions ( int a ) { int count = 0 ; for ( int i = 0 ; i <= a ; i ++ ) { if ( a == ( i + ( a ^ i ) ) ) count ++ ; } return count ; } int main ( ) { int a = 3 ; cout << countSolutions ( a ) ; }
Number of values of b such that a = b + ( a ^ b ) | C ++ program to find the number of values of b such that a = b + ( a ^ b ) ; function to return the number of solutions ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSolutions ( int a ) { int count = __builtin_popcount ( a ) ; count = pow ( 2 , count ) ; return count ; } int main ( ) { int a = 3 ; cout << countSolutions ( a ) ; }
Sum of Area of all possible square inside a rectangle | CPP program to calculate the sum of area of all possible squares that comes inside the rectangle ; Function to calculate the sum of area of all possible squares that comes inside the rectangle ; Square with max size possible ; calculate total square of a given size ; calculate area of squares of a particular size ; total area ; increment size ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateAreaSum ( int l , int b ) { int size = 1 ; int maxSize = min ( l , b ) ; int totalArea = 0 ; for ( int i = 1 ; i <= maxSize ; i ++ ) { int totalSquares = ( l - size + 1 ) * ( b - size + 1 ) ; int area = totalSquares * size * size ; totalArea += area ; size ++ ; } return totalArea ; } int main ( ) { int l = 4 , b = 3 ; cout << calculateAreaSum ( l , b ) ; return 0 ; }