codeparrot/xlcost-text-to-code huggingface dataset:
Minimize flips on K |  ; store previous flip events ; remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; flip . push ( i ) ; insert ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minKBitFlips ( int A [ ] , int K , int N ) { queue < int > flip ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( flip . size ( ) > 0 && ( i - flip . front ( ) >= K ) ) { flip . pop ( ) ; } if ( A [ i ] % 2 == flip . size ( ) % 2 ) { if ( i + K - 1 >= N ) { return -1 ; } count ++ ; } } return count ; } int main ( ) { int A [ ] = { 0 , 1 , 0 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 1 ; int ans = minKBitFlips ( A , K , 3 ) ; cout << ans ; return 0 ; }
Minimize flips on K |  ; We 're flipping the subarray from A[i] to A[i+K-1]  ; If not flip entire array , impossible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minKBitFlips ( int A [ ] , int K , int N ) { int temp [ N ] ; memset ( temp , 0 , N ) ; int count = 0 ; int flip = 0 ; count ++ ; for ( int i = 0 ; i < N ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > N ) { return -1 ; } flip ^= 1 ; if ( i + K < N ) { temp [ i + K ] ^= 1 ; } } } return count ; } int main ( ) { int A [ ] = { 0 , 1 , 0 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 1 ; int ans = minKBitFlips ( A , K , N ) ; cout << ans ; return 0 ; }
Generate an N | C ++ program for the above approach ; Function to calculate GCD of given array ; Utility function to check for all the combinations ; If a sequence of size N is obtained ; If gcd of current combination is K ; If current element from first array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; If current element from second array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; Function to check for all possible combinations ; Stores the subsequence ; If GCD of any sequence is not equal to K ; Driver Code ; Given arrays ; size of the array ; Given value of K ; Function call to generate a subsequence whose GCD is K
#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 ) ; } int GCDArr ( vector < int > a ) { int ans = a [ 0 ] ; for ( auto val : a ) { ans = GCD ( ans , val ) ; } return ans ; } bool findSubseqUtil ( int a [ ] , int b [ ] , vector < int > ans , int k , int i , int N ) { if ( ans . size ( ) == N ) { if ( GCDArr ( ans ) == k ) { for ( auto val : ans ) cout << val << " ▁ " ; return true ; } else { return false ; } } if ( a [ i ] % k == 0 ) { ans . push_back ( a [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp == true ) return true ; ans . pop_back ( ) ; } if ( b [ i ] % k == 0 ) { ans . push_back ( b [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp == true ) return true ; ans . pop_back ( ) ; } return false ; } void findSubseq ( int A [ ] , int B [ ] , int K , int i , int N ) { vector < int > ans ; bool ret = findSubseqUtil ( A , B , ans , K , i , N ) ; if ( ret == false ) cout << -1 << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 5 , 3 , 6 , 2 , 9 } ; int B [ ] = { 21 , 7 , 14 , 12 , 28 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 3 ; findSubseq ( A , B , K , 0 , N ) ; return 0 ; }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | C ++ program for the above approach ; Function to find the K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver Code ; functions call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findKthElement ( int N , int K ) { vector < int > v ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . push_back ( i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . push_back ( i ) ; } } cout << v [ K - 1 ] ; } int main ( ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; return 0 ; }
Minimum subsequences of a string A required to be appended to obtain the string B | C ++ program for the above approach ; Function to count the minimum subsequences of a string A required to be appended to obtain the string B ; Size of the string ; Maps characters to their respective indices ; Insert indices of characters into the sets ; Stores the position of the last visited index in the string A . Initially set it to - 1. ; Stores the required count ; Iterate over the characters of B ; If the character in B is not present in A , return - 1 ; Fetch the next index from B [ i ] 's set ; If the iterator points to the end of that set ; If it doesn 't point to the  end, update previous ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countminOpsToConstructAString ( string A , string B ) { int N = A . length ( ) ; int i = 0 ; map < char , set < int > > mp ; for ( i = 0 ; i < N ; i ++ ) { mp [ A [ i ] ] . insert ( i ) ; } int previous = -1 ; int ans = 1 ; for ( i = 0 ; i < B . length ( ) ; i ++ ) { char ch = B [ i ] ; if ( mp [ ch ] . size ( ) == 0 ) { cout << -1 ; return ; } auto it = mp [ ch ] . upper_bound ( previous ) ; if ( it == mp [ ch ] . end ( ) ) { previous = -1 ; ans ++ ; -- i ; continue ; } previous = * it ; } cout << ans ; } int main ( ) { string A = " abc " , B = " abac " ; countminOpsToConstructAString ( A , B ) ; return 0 ; }
Maximize difference between odd and even indexed array elements by shift operations | C ++ program for the above approach ; Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimize ( int n ) { int optEle = n ; string strEle = to_string ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = stoi ( strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ) ; optEle = min ( optEle , temp ) ; } return optEle ; } int maximize ( int n ) { int optEle = n ; string strEle = to_string ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = stoi ( strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ) ; optEle = max ( optEle , temp ) ; } return optEle ; } void minimumDifference ( int arr [ ] , int N ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = abs ( maxVal - minVal ) ; } cout << max ( caseOne , caseTwo ) << endl ; } int main ( ) { int arr [ ] = { 332 , 421 , 215 , 584 , 232 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumDifference ( arr , N ) ; return 0 ; }
Find if given vertical level of binary tree is sorted or not | CPP program to determine whether vertical level l of binary tree is sorted or not . ; Structure of a tree node . ; Function to create new tree node . ; Helper function to determine if vertical level l of given binary tree is sorted or not . ; If root is null , then the answer is an empty subset and an empty subset is always considered to be sorted . ; Variable to store previous value in vertical level l . ; Variable to store current level while traversing tree vertically . ; Variable to store current node while traversing tree vertically . ; Declare queue to do vertical order traversal . A pair is used as element of queue . The first element in pair represents the node and the second element represents vertical level of that node . ; Insert root in queue . Vertical level of root is 0. ; Do vertical order traversal until all the nodes are not visited . ; Check if level of node extracted from queue is required level or not . If it is the required level then check if previous value in that level is less than or equal to value of node . ; If left child is not NULL then push it in queue with level reduced by 1. ; If right child is not NULL then push it in queue with level increased by 1. ; If the level asked is not present in the given binary tree , that means that level will contain an empty subset . Therefore answer will be true . ; Driver program ; 1 / \ 2 5 / \ 7 4 / 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } bool isSorted ( Node * root , int level ) { if ( root == NULL ) return true ; int prevVal = INT_MIN ; int currLevel ; Node * currNode ; queue < pair < Node * , int > > q ; q . push ( make_pair ( root , 0 ) ) ; while ( ! q . empty ( ) ) { currNode = q . front ( ) . first ; currLevel = q . front ( ) . second ; q . pop ( ) ; if ( currLevel == level ) { if ( prevVal <= currNode -> key ) prevVal = currNode -> key ; else return false ; } if ( currNode -> left ) q . push ( make_pair ( currNode -> left , currLevel - 1 ) ) ; if ( currNode -> right ) q . push ( make_pair ( currNode -> right , currLevel + 1 ) ) ; } return true ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> right = newNode ( 4 ) ; root -> left -> right -> left = newNode ( 6 ) ; int level = -1 ; if ( isSorted ( root , level ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimize operations of removing 2 i | C ++ program to implement the above approach ; Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumStepReqArr ( int arr [ ] , int N ) { int cntStep = 0 ; N += 1 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( N & ( 1 << i ) ) { cntStep += 1 ; } } return cntStep ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumStepReqArr ( arr , N ) ; return 0 ; }
Minimize removal of alternating subsequences to empty given Binary String |  ; Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver code + ; input string ; length of string ; Function Call
#include <iostream> NEW_LINE using namespace std ; void minOpsToEmptyString ( string S , int N ) { int one = 0 , zero = 0 ; int x0 = 0 , x1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = max ( x0 , zero ) ; one = max ( x1 , one ) ; } cout << max ( one , zero ) << endl ; } int main ( ) { string S = "0100100111" ; int N = S . length ( ) ; minOpsToEmptyString ( S , N ) ; }
Swap upper and lower triangular halves of a given Matrix | C ++ program for the above approach ; Function to swap laterally inverted images of upper and lower triangular halves of a given matrix ; Store the matrix elements from upper & lower triangular halves ; Traverse the matrix mat [ ] [ ] ; Find the index ; If current element lies on the principal diagonal ; If current element lies below the principal diagonal ; If current element lies above the principal diagonal ; Traverse again to swap values ; Find the index ; Principal diagonal ; Below main diagonal ; Above main diagonal ; Traverse the matrix and print ; Driver Code ; Given Matrix mat [ ] [ ] ; Swap the upper and lower triangular halves
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ReverseSwap ( vector < vector < int > > & mat , int n ) { vector < int > lowerEle [ n ] ; vector < int > upperEle [ n ] ; int index ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { lowerEle [ index ] . push_back ( mat [ i ] [ j ] ) ; } else { upperEle [ index ] . push_back ( mat [ i ] [ j ] ) ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { mat [ i ] [ j ] = upperEle [ index ] . back ( ) ; upperEle [ index ] . pop_back ( ) ; } else { mat [ i ] [ j ] = lowerEle [ index ] . back ( ) ; lowerEle [ index ] . pop_back ( ) ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { vector < vector < int > > mat = { { 1 , 2 } , { 4 , 5 } } ; int N = mat . size ( ) ; ReverseSwap ( mat , N ) ; return 0 ; }
Minimize sum of K positive integers with given LCM | C ++ program to implement the above approach ; Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of arr [ ] into K groups ; Traverse the array arr [ ] ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > primePower ( int X ) { vector < int > primePow ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { int p = 1 ; while ( X % i == 0 ) { X /= i ; p *= i ; } primePow . push_back ( p ) ; } } if ( X > 1 ) { primePow . push_back ( X ) ; } return primePow ; } int getSum ( vector < int > & ar ) { int sum = 0 ; for ( int i : ar ) { sum += i ; } return sum ; } int getMinSum ( int pos , vector < int > & arr , vector < int > & primePow ) { if ( pos == primePow . size ( ) ) { return getSum ( arr ) ; } int res = INT_MAX ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { arr [ i ] *= primePow [ pos ] ; res = min ( res , getMinSum ( pos + 1 , arr , primePow ) ) ; arr [ i ] /= primePow [ pos ] ; } return res ; } int minimumSumWithGivenLCM ( int k , int x ) { vector < int > primePow = primePower ( x ) ; int n = primePow . size ( ) ; int sum = 0 ; if ( n <= k ) { for ( int i : primePow ) { sum += i ; } sum += k - n ; } else { vector < int > arr ( k , 1 ) ; sum = getMinSum ( 0 , arr , primePow ) ; } return sum ; } int main ( ) { int k = 3 , x = 210 ; cout << minimumSumWithGivenLCM ( k , x ) ; return 0 ; }
Minimum removal of subsequences of distinct consecutive characters required to empty a given string | C ++ program to implement the above approach ; Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinOperationsReqEmpStr ( string str ) { int cntOne = 0 ; int cntZero = 0 ; int N = str . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { if ( cntOne ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } int main ( ) { string str = "0100100111" ; cout << findMinOperationsReqEmpStr ( str ) ; }
Minimum number of flips required such that the last cell of matrix can be reached from any other cell | C ++ program for the above approach ; Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Driver Code ; Given matrix ; Function call ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countChanges ( vector < vector < char > > mat ) { int n = mat . size ( ) ; int m = mat [ 0 ] . size ( ) ; int ans = 0 ; for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 ] [ j ] != ' R ' ) ans ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i ] [ m - 1 ] != ' D ' ) ans ++ ; } return ans ; } int main ( ) { vector < vector < char > > arr = { { ' R ' , ' R ' , ' R ' , ' D ' } , { ' D ' , ' D ' , ' D ' , ' R ' } , { ' R ' , ' D ' , ' R ' , ' F ' } } ; int cnt = countChanges ( arr ) ; cout << cnt << endl ; return 0 ; }
Check if a binary tree is sorted level | CPP program to determine whether binary tree is level sorted or not . ; Structure of a tree node . ; Function to create new tree node . ; Function to determine if given binary tree is level sorted or not . ; to store maximum value of previous level . ; to store minimum value of current level . ; to store maximum value of current level . ; to store number of nodes in current level . ; queue to perform level order traversal . ; find number of nodes in current level . ; traverse current level and find minimum and maximum value of this level . ; if minimum value of this level is not greater than maximum value of previous level then given tree is not level sorted . ; maximum value of this level is previous maximum value for next level . ; Driver program ; 1 / 4 \ 6 / \ 8 9 / \ 12 10
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } int isSorted ( Node * root ) { int prevMax = INT_MIN ; int minval ; int maxval ; int levelSize ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { levelSize = q . size ( ) ; minval = INT_MAX ; maxval = INT_MIN ; while ( levelSize > 0 ) { root = q . front ( ) ; q . pop ( ) ; levelSize -- ; minval = min ( minval , root -> key ) ; maxval = max ( maxval , root -> key ) ; if ( root -> left ) q . push ( root -> left ) ; if ( root -> right ) q . push ( root -> right ) ; } if ( minval <= prevMax ) return 0 ; prevMax = maxval ; } return 1 ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 8 ) ; root -> left -> right -> right = newNode ( 9 ) ; root -> left -> right -> left -> left = newNode ( 12 ) ; root -> left -> right -> right -> right = newNode ( 10 ) ; if ( isSorted ( root ) ) cout << " Sorted " ; else cout << " Not ▁ sorted " ; return 0 ; }
Smallest subsequence having GCD equal to GCD of given array | C ++ program to implement the above approach ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSmallSub ( int arr [ ] , int N ) { int gcdArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { gcdArr = __gcd ( gcdArr , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { cout << arr [ i ] << " ▁ " ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( __gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { cout << arr [ i ] << " ▁ " << arr [ j ] ; return ; } } } } int main ( ) { int arr [ ] = { 4 , 6 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printSmallSub ( arr , N ) ; }
Count pairs in an array whose product is composite number | C ++ program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the boolean value to check if a number is prime or not ; Mark all non prime numbers as false ; If i is prime number ; Mark j as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the boolean value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define X  1000000 NEW_LINE vector < bool > getPrimeNum ( ) { vector < bool > isPrime ( X , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( isPrime [ i ] == true ) { for ( int j = i * i ; j < X ; j += i ) { isPrime [ j ] = false ; } } } return isPrime ; } int cntPairs ( int arr [ ] , int N ) { vector < bool > isPrime = getPrimeNum ( ) ; int cntOne = 0 ; int cntPrime = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOne += 1 ; } else if ( isPrime [ i ] ) { cntPrime += 1 ; } } int cntNonComp = 0 ; cntNonComp = cntPrime * cntOne + cntOne * ( cntOne - 1 ) / 2 ; int res = 0 ; res = N * ( N - 1 ) / 2 - cntNonComp ; return res ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntPairs ( arr , N ) ; }
Find the greater number closest to N having at most one non | C ++ program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int X , int n ) { int res = 1 ; while ( n ) { if ( n & 1 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } int closestgtNum ( int N ) { int n = log10 ( N ) + 1 ; int P = power ( 10 , n - 1 ) ; int Y = N % P ; int res = N + ( P - Y ) ; return res ; } int main ( ) { int N = 120 ; cout << closestgtNum ( N ) ; }