codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C ++ program for the above approach ; Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void number_of_strings ( int N , int M ) { int ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } cout << ans ; } int main ( ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; return 0 ; }
Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | C ++ program for the above approach ; Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Driver Code ; Given array ; Length of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( int A [ ] , int N ) { int elem = A [ 0 ] , count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] == elem ) { count ++ ; } else { break ; } } if ( count == N ) return N ; else return 1 ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minLength ( arr , N ) << endl ; return 0 ; }
Reduce N to 0 or less by given X and Y operations | C ++ Program to implement the above approach ; Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool NegEqu ( int N , int X , int Y ) { while ( X -- and N > N / 2 + 10 ) { N = N / 2 + 10 ; } while ( Y -- ) { N = N - 10 ; } if ( N <= 0 ) return true ; return false ; } int main ( ) { int N = 100 ; int X = 3 ; int Y = 4 ; if ( NegEqu ( N , X , Y ) ) { cout << " Yes " ; } else { cout << " No " ; } }
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | C ++ program for the above approach ; Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSubarrays ( int arr [ ] , int n ) { int right = n - 1 ; int left = 0 ; int subarrays = 0 ; while ( right >= 0 ) { for ( left = 0 ; left <= right ; left += 1 ) { if ( __gcd ( arr [ left ] , arr [ right ] ) > 1 ) { subarrays += 1 ; right = left - 1 ; break ; } if ( left == right && __gcd ( arr [ left ] , arr [ right ] ) == 1 ) { return 0 ; } } } return subarrays ; } int main ( ) { int N = 6 ; int arr [ ] = { 2 , 3 , 4 , 4 , 4 , 3 } ; cout << minSubarrays ( arr , N ) ; return 0 ; }
Find the largest possible value of K such that K modulo X is Y | C ++ program for the above approach ; Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSoln ( int n , int x , int y ) { int ans = INT_MIN ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : -1 ) ; } int main ( ) { int n = 15 , x = 10 , y = 5 ; cout << findMaxSoln ( n , x , y ) ; return 0 ; }
Count all numbers up to N having M as the last digit | C ++ Program to implement the above approach ; Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int N , int M ) { int total_count = 0 ; total_count += ( N / 10 ) ; int x = ( N / 10 ) * 10 ; if ( ( N - x ) >= M ) { total_count = total_count + 1 ; } return total_count ; } int main ( ) { int N = 100 , M = 1 ; cout << getCount ( N , M ) ; return 0 ; }
Count of Right | C ++ program for the above approach ; Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int RightAngled ( int a [ ] [ 2 ] , int n ) { unordered_map < int , int > xpoints ; unordered_map < int , int > ypoints ; for ( int i = 0 ; i < n ; i ++ ) { xpoints [ a [ i ] [ 0 ] ] ++ ; ypoints [ a [ i ] [ 1 ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints [ a [ i ] [ 0 ] ] >= 1 && ypoints [ a [ i ] [ 1 ] ] >= 1 ) { count += ( xpoints [ a [ i ] [ 0 ] ] - 1 ) * ( ypoints [ a [ i ] [ 1 ] ] - 1 ) ; } } return count ; } int main ( ) { int N = 5 ; int arr [ ] [ 2 ] = { { 1 , 2 } , { 2 , 1 } , { 2 , 2 } , { 2 , 3 } , { 3 , 2 } } ; cout << RightAngled ( arr , N ) ; return 0 ; }
Smallest subarray which upon repetition gives the original array | C ++ program for the above approach ; Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void printArray ( vector < int > & brr ) { for ( auto & it : brr ) { cout << it << ' ▁ ' ; } } void RepeatingSubarray ( int arr [ ] , int N ) { if ( N < 2 ) { cout << " - 1" ; } vector < int > brr ; brr . push_back ( arr [ 0 ] ) ; brr . push_back ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { bool a = false ; int n = brr . size ( ) ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr [ K ] ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . push_back ( arr [ i ] ) ; } cout << " - 1" ; return ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; RepeatingSubarray ( arr , N ) ; return 0 ; }
Find all missing numbers from a given sorted array | C ++ program for the above approach ; Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMissingElements ( int arr [ ] , int N ) { int diff = arr [ 0 ] - 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { cout << i + diff << " ▁ " ; diff ++ ; } } } } int main ( ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = sizeof ( arr ) / sizeof ( int ) ; printMissingElements ( arr , N ) ; return 0 ; }
Find all missing numbers from a given sorted array | C ++ program for the above approach ; Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMissingElements ( int arr [ ] , int N ) { int b [ arr [ N - 1 ] + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { cout << i << " ▁ " ; } } } int main ( ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = sizeof ( arr ) / sizeof ( int ) ; printMissingElements ( arr , N ) ; return 0 ; }
Least common element in given two Arithmetic sequences | C ++ program to implement the above approach ; Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield true in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long smallestCommon ( long a , long b , long c , long d ) { if ( a == c ) return a ; if ( a > c ) { swap ( a , c ) ; swap ( b , d ) ; } long first_term_diff = ( c - a ) ; long possible_y ; for ( possible_y = 0 ; possible_y < b ; possible_y ++ ) { if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) { break ; } } if ( possible_y != b ) { return c + possible_y * d ; } return -1 ; } int main ( ) { long A = 2 , B = 20 , C = 19 , D = 9 ; cout << smallestCommon ( A , B , C , D ) ; return 0 ; }
Minimum number of flips with rotation to make binary string alternating | C ++ program for the above approach ; Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Driver Code ; Given String ; Length of given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumFlips ( string s , int n ) { int a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( s [ i ] == '1' ? 1 : 0 ) ; } int oddone [ n + 1 ] ; int evenone [ n + 1 ] ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } int minimum = min ( oddone [ n ] , evenone [ n ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } int main ( ) { string S = "000001100" ; int n = S . length ( ) ; cout << ( MinimumFlips ( S , n ) ) ; }
Create a Graph by connecting divisors from N to M and find shortest path | C ++ program for the above approach ; Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To avoid duplicate printing of same element ; Print the factors from n ; Driver Code ; Given N and M ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } void shortestpath ( int m , int n ) { vector < int > mfactor , nfactor ; map < int , int > fre ; mfactor . push_back ( m ) ; fre [ m ] = 1 ; while ( m != 1 ) { if ( isprm ( m ) ) { mfactor . push_back ( 1 ) ; fre [ 1 ] = 1 ; m = 1 ; } else { for ( int i = 2 ; i <= sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . push_back ( m / i ) ; fre [ m / i ] = 1 ; m = ( m / i ) ; break ; } } } } nfactor . push_back ( n ) ; while ( fre [ n ] != 1 ) { if ( isprm ( n ) ) { nfactor . push_back ( 1 ) ; n = 1 ; } else { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . push_back ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( int i = 0 ; i < mfactor . size ( ) ; i ++ ) { if ( mfactor [ i ] == n ) break ; cout << mfactor [ i ] << " ▁ < - - > ▁ " ; } for ( int i = nfactor . size ( ) - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) cout << nfactor [ i ] ; else cout << nfactor [ i ] << " ▁ < - - > ▁ " ; } } int main ( ) { int m = 18 , n = 19 ; shortestpath ( m , n ) ; return 0 ; }
Possible number of Trees having N vertex | C ++ program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the final count of trees ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1e9 + 7 ; int NumberOfTrees ( int arr [ ] , int N ) { int maxElement = * max_element ( arr , arr + N ) ; int level [ maxElement + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { for ( int j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } int main ( ) { int N = 7 ; int arr [ ] = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; cout << NumberOfTrees ( arr , N ) ; return 0 ; }
Possible number of Trees having N vertex | C ++ program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1e9 + 7 ; int power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( y & 1 ) p = ( x * p ) % mod ; return p ; } int NumberOfTrees ( int arr [ ] , int N ) { int maxElement = * max_element ( arr , arr + N ) ; int level [ maxElement + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod ; } return ans ; } int main ( ) { int N = 7 ; int arr [ ] = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; cout << NumberOfTrees ( arr , N ) ; return 0 ; }
A Binary String Game | C ++ program for the above approach ; Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Driver Code ; Given string S ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string gameMax ( string S ) { int N = S . length ( ) ; vector < int > list ; int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { one ++ ; } else { if ( one != 0 ) { list . push_back ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . push_back ( one ) ; } sort ( list . begin ( ) , list . end ( ) , greater < int > ( ) ) ; int score_1 = 0 , score_2 = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { if ( list [ i ] % 2 == 1 ) { score_1 += list [ i ] ; } else { score_2 += list [ i ] ; } } if ( score_1 == score_2 ) return " - 1" ; return ( score_1 > score_2 ) ? " Player ▁ 1" : " Player ▁ 2" ; } int main ( ) { string S = "11111101" ; cout << gameMax ( S ) ; }
Count of carry operations on adding two Binary numbers | C ++ Program for the above approach ; Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to final count ; Return the final count ; Driver Code ; Given two numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int carryCount ( int num1 , int num2 ) { int count = 0 ; while ( num2 != 0 ) { int carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += __builtin_popcount ( num2 ) ; } return count ; } int main ( ) { int A = 15 , B = 10 ; cout << carryCount ( 15 , 10 ) ; return 0 ; }
Count of all possible ways to reach a target by a Knight | C ++ Program to implement the above approach ; Function to return X ^ Y % Mod ; Base Case ; Function to return the inverse of factorial of N ; Base case ; Function to return factorial of n % Mod ; Base case ; Function to return the value of n ! / ( ( n - k ) ! * k ! ) ; Function to return the count of ways to reach ( n , m ) from ( 0 , 0 ) ; If ( N + M ) % 3 != 0 ; No possible way exists ; Calculate X and Y from the equations X + 2 Y = N and 2 X + Y == M ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int Mod = 1e9 + 7 ; int power ( int X , int Y , int Mod ) { if ( Y == 0 ) return 1 ; int p = power ( X , Y / 2 , Mod ) % Mod ; p = ( p * p ) % Mod ; if ( Y & 1 ) { p = ( X * p ) % Mod ; } return p ; } int Inversefactorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return power ( fact , Mod - 2 , Mod ) ; } int factorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return fact ; } int nck ( int N , int K ) { int factN = factorial ( N ) ; int inv = Inversefactorial ( K ) ; int invFact = Inversefactorial ( N - K ) ; return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod ; } int TotalWaYs ( int N , int M ) { if ( ( N + M ) % 3 != 0 ) return 0 ; int X = N - ( N + M ) / 3 ; int Y = M - ( N + M ) / 3 ; if ( X < 0 Y < 0 ) return 0 ; return nck ( X + Y , Y ) ; } int main ( ) { int N = 3 , M = 3 ; cout << TotalWaYs ( N , M ) ; return 0 ; }
Lead a life problem | C ++ program for the above approach ; Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Driver Code ; Given days ; Given earnings ; Given cost ; Given energy e ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calculateProfit ( int n , int * earnings , int * cost , int e ) { int profit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int earning_per_day = 0 ; int daily_spent_food = 0 ; if ( i == ( n - 1 ) ) { earning_per_day = earnings [ i ] * e ; profit = profit + earning_per_day ; break ; } if ( cost [ i ] < earnings [ i ] ) { earning_per_day = earnings [ i ] * e ; daily_spent_food = cost [ i ] * e ; profit = profit + earning_per_day - daily_spent_food ; } } cout << profit << endl ; } int main ( ) { int n = 4 ; int earnings [ ] = { 1 , 8 , 6 , 7 } ; int cost [ ] = { 1 , 3 , 4 , 1 } ; int e = 5 ; calculateProfit ( n , earnings , cost , e ) ; }
Check if N numbers with Even Sum can be selected from a given Array | C ++ efficient program to check if N numbers with Odd sum can be selected from the given array ; Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEvenSum ( int arr [ ] , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] & 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq & 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } return false ; } int main ( ) { int arr [ ] = { 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int N = 5 ; if ( checkEvenSum ( arr , N , size ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }
Minimum count of digits required to obtain given Sum | C ++ program to implement the above approach ; Function to return the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mindigits ( int n ) { if ( n % 9 == 0 ) { cout << n / 9 << endl ; } else { cout << ( n / 9 ) + 1 << endl ; } } int main ( ) { int n1 = 24 ; int n2 = 14 ; mindigits ( n1 ) ; mindigits ( n2 ) ; }
Count of all possible numbers not exceeding M having suffix N | C ++ Program to implement the above approach ; Function to count the no . of digits of N ; Function to count all possible numbers having Suffix as N ; Difference of the A . P ; Count of the number of terms ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitsOf ( int num ) { return to_string ( num ) . size ( ) ; } int count ( int a , int tn ) { int diff = pow ( 10 , digitsOf ( a ) ) ; return ( ( tn - a ) / diff ) + 1 ; } int main ( ) { int n , m ; n = 25 , m = 4500 ; cout << count ( n , m ) ; return 0 ; }