codeparrot/xlcost-text-to-code huggingface dataset:
Count positions in a chessboard that can be visited by the Queen which are not visited by the King | C ++ program for the above approach ; Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Driver Code ; Dimension of Board ; Position of Cell ; Function Call
#include <iostream> NEW_LINE using namespace std ; int Moves_Calculator ( int x , int y , int row , int col ) { int total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; int king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } int main ( ) { int n = 8 , m = 8 ; int x = 1 , y = 1 ; cout << ( Moves_Calculator ( x , y , m , n ) ) ; return 0 ; }
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number | C ++ program to implement the above approach ; Function to find the nearest smaller number satisfying the condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int clstNum ( int N ) { return ( N - 1 ) ; } int main ( ) { int N = 11 ; cout << clstNum ( N ) ; }
Count array elements having modular inverse under given prime number P equal to itself | C ++ program to implement the above approach ; Function to get the count of elements that satisfy the given condition . ; Stores count of elements that satisfy the condition ; Traverse the given array . ; If square of current element is equal to 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equvInverse ( int arr [ ] , int N , int P ) { int cntElem = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) { cntElem ++ ; } } return cntElem ; } int main ( ) { int arr [ ] = { 1 , 6 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int P = 7 ; cout << equvInverse ( arr , N , P ) ; }
Count ways to split array into K non | C ++ program to implement the above approach ; Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } int cntWays ( int arr [ ] , int N , int K ) { int cntways = 0 ; int M = 0 ; unordered_set < int > st ; for ( int i = 0 ; i < N ; i ++ ) { st . insert ( arr [ i ] ) ; } M = st . size ( ) ; cntways = power ( K , M ) ; return cntways ; } int main ( ) { int arr [ ] = { 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << cntWays ( arr , N , K ) ; return 0 ; }
Minimize product of first N | C ++ program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1e9 + 7 ; void minProduct ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= ( n - 4 ) / 2 ; i ++ ) { ans = ( 1LL * ans * ( n - 2 ) ) % mod ; } ans = ( 1LL * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; cout << ans << endl ; } int main ( ) { int N = 8 ; minProduct ( N ) ; return 0 ; }
Bitwise XOR of all unordered pairs from a given array | C ++ program to implement the above approach ; Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TotalXorPair ( int arr [ ] , int N ) { int totalXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << TotalXorPair ( arr , N ) ; }
Count permutations of all integers upto N that can form an acyclic graph based on given conditions | C ++ implementation of above approach ; Find the count of possible graphs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleAcyclicGraph ( int N ) { cout << pow ( 2 , N - 1 ) ; return ; } int main ( ) { int N = 4 ; possibleAcyclicGraph ( N ) ; return 0 ; }
Minimize positive product of two given numbers by at most N decrements | C ++ program to implement the above approach ; Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minProd ( int X , int Y , int N ) { if ( X <= Y ) { if ( N < X ) return ( X - N ) * Y ; else { return max ( Y - ( N - X + 1 ) , 1 ) ; } } if ( Y >= N ) return ( Y - N ) * X ; return max ( X - ( N - Y + 1 ) , 1 ) ; ; } int main ( ) { int X = 47 , Y = 42 , N = 167 ; cout << minProd ( X , Y , N ) ; }
Count pairs from an array having product of their sum and difference equal to 1 | C ++ program for the above approach ; Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int oneCount = 0 ; int desiredPair = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } int main ( ) { int arr [ ] = { 3 , 1 , 1 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }
Smallest missing non | C ++ program to implement the above approach ; Function to print the smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smlstNonNeg ( int arr [ ] , int N ) { int smNonNeg = 0 ; bool hash [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 and arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } cout << smNonNeg << " ▁ " ; } } int main ( ) { int arr [ ] = { 0 , 1 , 2 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smlstNonNeg ( arr , N ) ; }
Split array into two subsequences having minimum count of pairs with sum equal to X | C ++ program for above approach ; Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int arr [ ] , int N , int X ) { vector < int > A , B ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . push_back ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . push_back ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . push_back ( arr [ i ] ) ; } else { B . push_back ( arr [ i ] ) ; } c ++ ; } } cout << " The ▁ First ▁ Array ▁ is ▁ - ▁ " ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { cout << A [ i ] << " ▁ " ; } cout << endl ; cout << " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) { cout << B [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( arr , N , X ) ; }
Split array into minimum number of subsets having maximum pair sum at most K | C ++ program to implement the above approach ; Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntMinSub ( int arr [ ] , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; sort ( arr , arr + N ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } int main ( ) { int arr [ ] = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 26 ; cout << cntMinSub ( arr , N , K ) ; }
Count possible values of K such that X | C ++ program for the above approach ; Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int condition ( int a , int b ) { int d = abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } int main ( ) { int x = 2 , y = 6 ; cout << condition ( x , y ) << endl ; return 0 ; }
Count pairs in an array whose product is composite number | C ++ program to implement the above approach ; Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isComposite ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } int compositePair ( int arr [ ] , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << compositePair ( arr , N ) ; return 0 ; }
Count N | C ++ Program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1e9 + 7 ; long long power ( int x , int y ) { if ( y == 0 ) return 1 ; long long int p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( y & 1 ) { p = ( x * p ) % mod ; } return p ; } int TotalNumber ( int N ) { int ans = ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ; return ans ; } int main ( ) { int N = 10 , X = 3 , Y = 4 ; cout << TotalNumber ( N ) << endl ; return 0 ; }
Array obtained by repeatedly reversing array after every insertion from given array | C ++ program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateArray ( int arr [ ] , int n ) { deque < int > ans ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) ans . push_front ( arr [ i ] ) ; else ans . push_back ( arr [ i ] ) ; } if ( n & 1 ) { reverse ( ans . begin ( ) , ans . end ( ) ) ; } for ( auto x : ans ) { cout << x << " ▁ " ; } cout << endl ; } int32_t main ( ) { int n = 4 ; int arr [ n ] = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; return 0 ; }
Find the greater number closest to N having at most one non | C ++ program to implement the above approach ; Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant string ; Append n - 1 times '0' to the end of resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string closestgtNum ( string str ) { string res = " " ; int n = str . length ( ) ; if ( str [ 0 ] < '9' ) { res . push_back ( str [ 0 ] + 1 ) ; } else { res . push_back ( '1' ) ; res . push_back ( '0' ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res . push_back ( '0' ) ; } return res ; } int main ( ) { string str = "120" ; cout << closestgtNum ( str ) ; }
Count maximum non | C ++ program for the above approach ; Function to count maximum number of non - overlapping subarrays with sum equals to the target ; Stores the final count ; Next subarray should start from index >= availIdx ; Tracks the prefix sum ; Map to store the prefix sum for respective indices ; Check if cur_sum - target is present in the array or not ; Update the index of current prefix sum ; Return the count of subarrays ; Driver Code ; Given array arr [ ] ; Given sum target ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSubarrays ( int arr [ ] , int N , int target ) { int ans = 0 ; int availIdx = -1 ; int cur_sum = 0 ; unordered_map < int , int > mp ; mp [ 0 ] = -1 ; for ( int i = 0 ; i < N ; i ++ ) { cur_sum += arr [ i ] ; if ( mp . find ( cur_sum - target ) != mp . end ( ) && mp [ cur_sum - target ] >= availIdx ) { ans ++ ; availIdx = i ; } mp [ cur_sum ] = i ; } return ans ; } int main ( ) { int arr [ ] = { 2 , -1 , 4 , 3 , 6 , 4 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int target = 6 ; cout << maximumSubarrays ( arr , N , target ) ; return 0 ; }
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | C ++ program for the above approach ; Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } void maximumPossibleSum ( int N , int X ) { cout << ( X * ( power ( 2 , N ) - 1 ) ) << endl ; } int main ( ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; }
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | C ++ program for the above approach ; Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the final count ; Driver Code ; Given X and Y ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x y ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 and bit2 == 0 ) { counter *= 2 ; continue ; } if ( bit1 & bit2 ) { counter = 0 ; break ; } } return counter ; } int main ( ) { int X = 2 , Y = 5 ; cout << countOfPairs ( X , Y ) ; return 0 ; }
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | C ++ Program to implement the above approach ; Function to return gcd of a and b ; Base Case ; Recursive Call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findGCD ( int arr [ ] , int N ) { int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { result = gcd ( result , arr [ i ] ) ; if ( result == 1 ) { return 1 ; } } return result ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGCD ( arr , N ) ; return 0 ; }
Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | C ++ program for the above approach ; Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factorsOf3 ( int arr [ ] , int N ) { int a = 0 , b = 0 , c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 3 == 0 ) a ++ ; else if ( arr [ i ] % 3 == 1 ) b ++ ; else if ( arr [ i ] % 3 == 2 ) c ++ ; } if ( a >= 1 && a <= b + c + 1 ) cout << " Yes " << endl ; else if ( a == 0 && b == 0 && c > 0 ) cout << " Yes " << endl ; else if ( a == 0 && c == 0 && b > 0 ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; factorsOf3 ( arr , N ) ; return 0 ; }
Check if a number is a perfect square having all its digits as a perfect square | C ++ program for the above approach ; Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check_digits ( long N ) { while ( N > 0 ) { int n = N % 10 ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return 0 ; } N = N / 10 ; } return 1 ; } bool is_perfect ( long N ) { long double n = sqrt ( N ) ; if ( floor ( n ) != ceil ( n ) ) { return 0 ; } return 1 ; } void isFullSquare ( long N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { long N = 144 ; isFullSquare ( N ) ; return 0 ; }
Count substrings with different first and last characters | C ++ program for the above approach ; Function to count the substrings having different first & last character ; Stores frequency of each char ; Loop to store frequency of the characters in a Map ; To store final result ; Traversal of string ; Store answer for every iteration ; Map traversal ; Compare current char ; Print the final count ; Driver Code ; Given string ; Length of the string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstring ( string s , int n ) { map < char , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ s [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; m [ s [ i ] ] -- ; for ( auto value : m ) { if ( value . first == s [ i ] ) { continue ; } else { cnt += value . second ; } } ans += cnt ; } cout << ans ; } int main ( ) { string S = " abcab " ; int N = 5 ; countSubstring ( S , N ) ; return 0 ; }
Maximize count of empty water bottles from N filled bottles | C ++ program for the above approach ; Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxBottles ( int n , int e ) { int s = 0 , b = 0 ; int a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } int main ( ) { int n = 9 , e = 3 ; int s = maxBottles ( n , e ) ; cout << s << endl ; }
Count all N digit numbers whose digits are multiple of X | C ++ program for the above approach ; Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count all N - digit numbers whose digits are multiples of x ; Count all digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Driver Code ; Given N and X ; Function Call
#include <stdio.h> NEW_LINE #define ll  long long NEW_LINE ll power ( ll x , ll n ) { ll temp ; if ( n == 0 ) return 1 ; temp = power ( x , n / 2 ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } ll count_Total_Numbers ( ll n , ll x ) { ll total , multiples = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } int main ( ) { ll N = 1 , X = 3 ; printf ( " % lld ▁ " , count_Total_Numbers ( N , X ) ) ; return 0 ; }
Find the vertex diagonally opposite to the vertex M from an N | C ++ program for the above approach ; Function to return the required vertex ; Case 1 : ; Case 2 : ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPosition ( int N , int M ) { if ( M > ( N / 2 ) ) { return ( M - ( N / 2 ) ) ; } return ( M + ( N / 2 ) ) ; } int main ( ) { int N = 8 , M = 5 ; cout << getPosition ( N , M ) ; return 0 ; }