codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of subsets having product of smallest element and size of the subset at least X | C ++ Program to implement the above approach ; Comparator function to return the greater of two numbers ; Function to return the maximum count of subsets possible which satisfy the above condition ; Sort the array in descending order ; Stores the count of subsets ; Stores the size of the current subset ; Check for the necessary conditions ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool comp ( int a , int b ) { return a > b ; } int maxSubset ( int arr [ ] , int N , int X ) { sort ( arr , arr + N , comp ) ; int counter = 0 ; int sz = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sz ++ ; if ( arr [ i ] * sz >= X ) { counter ++ ; sz = 0 ; } } return counter ; } int main ( ) { int arr [ ] = { 7 , 11 , 2 , 9 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 10 ; cout << maxSubset ( arr , N , X ) ; }
Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array | C ++ Program to implement the above approach ; Function to find the minimum of two numbers ; Function to find the maximum value possible of the given expression from all permutations of the array ; Stores the minimum value from the array ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min ( int a , int b ) { return ( a > b ) ? b : a ; } int maximumModuloValue ( int A [ ] , int n ) { int mn = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { mn = min ( A [ i ] , mn ) ; } return mn ; } int main ( ) { int A [ ] = { 7 , 10 , 12 } ; int n = ( sizeof ( A ) / ( sizeof ( A [ 0 ] ) ) ) ; cout << maximumModuloValue ( A , n ) << endl ; return 0 ; }
Final Matrix after incrementing submatrices by K in range given by Q queries | C ++ program for the above approach ; Query data type ; Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix mat [ ] [ ] by adding elements of aux [ ] [ ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the final matrix by adding mat and aux matrix at each cell ; Function that prints matrix mat [ ] ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the final answer ; Print the updated matrix ; Driver Code ; Given Matrix ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  4 NEW_LINE struct query { int x1 , x2 , y1 , y2 , K ; } ; void updateQuery ( int from_x , int from_y , int to_x , int to_y , int k , int aux [ ] [ M ] ) { aux [ from_x ] [ from_y ] += k ; if ( to_x + 1 < N ) aux [ to_x + 1 ] [ from_y ] -= k ; if ( to_x + 1 < N && to_y + 1 < M ) aux [ to_x + 1 ] [ to_y + 1 ] += k ; if ( to_y + 1 < M ) aux [ from_x ] [ to_y + 1 ] -= k ; } void updateMatrix ( int mat [ ] [ M ] , int aux [ ] [ M ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { aux [ j ] [ i ] += aux [ j - 1 ] [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { mat [ i ] [ j ] += aux [ i ] [ j ] ; } } } void printMatrix ( int mat [ ] [ M ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << " STRNEWLINE " ; } } void matrixQuery ( int mat [ ] [ M ] , int Q , query q [ ] ) { int aux [ N ] [ M ] = { } ; for ( int i = 0 ; i < Q ; i ++ ) { updateQuery ( q [ i ] . x1 , q [ i ] . x2 , q [ i ] . y1 , q [ i ] . y2 , q [ i ] . K , aux ) ; } updateMatrix ( mat , aux ) ; printMatrix ( mat ) ; } int main ( ) { int mat [ N ] [ M ] = { { 1 , 0 , 1 , 2 } , { 0 , 2 , 4 , 1 } , { 1 , 2 , 1 , 0 } } ; int Q = 1 ; query q [ ] = { { 0 , 0 , 1 , 1 , 2 } } ; matrixQuery ( mat , Q , q ) ; return 0 ; }
Find Second largest element in an array | C ++ program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; sort the array ; start from second last element as the largest element is at last ; if the element is not equal to largest element ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } sort ( arr , arr + arr_size ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { printf ( " The ▁ second ▁ largest ▁ element ▁ is ▁ % d STRNEWLINE " , arr [ i ] ) ; return ; } } printf ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element STRNEWLINE " ) ; } int main ( ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print2largest ( arr , n ) ; return 0 ; }
Find sum of all left leaves in a given Binary Tree | C ++ program to find sum of all left leaves ; A binary tree node ; constructor to create a new Node ; Return the sum of left leaf nodes ; A queue of pairs to do bfs traversal and keep track if the node is a left or right child if boolean value is true then it is a left child . ; do bfs traversal ; if temp is a leaf node and left child of its parent ; if it is not leaf then push its children nodes into queue ; boolean value is true here because it is left child of its parent ; boolean value is false here because it is right child of its parent ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int key ; Node * left , * right ; Node ( int key_ ) { key = key_ ; left = NULL ; right = NULL ; } } ; int sumOfLeftLeaves ( Node * root ) { if ( root == NULL ) return 0 ; queue < pair < Node * , bool > > q ; q . push ( { root , 0 } ) ; int sum = 0 ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) . first ; bool is_left_child = q . front ( ) . second ; q . pop ( ) ; if ( ! temp -> left && ! temp -> right && is_left_child ) sum = sum + temp -> key ; if ( temp -> left ) { q . push ( { temp -> left , 1 } ) ; } if ( temp -> right ) { q . push ( { temp -> right , 0 } ) ; } } return sum ; } int main ( ) { Node * root = new Node ( 20 ) ; root -> left = new Node ( 9 ) ; root -> right = new Node ( 49 ) ; root -> right -> left = new Node ( 23 ) ; root -> right -> right = new Node ( 52 ) ; root -> right -> right -> left = new Node ( 50 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 12 ) ; root -> left -> right -> right = new Node ( 12 ) ; cout << " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " << sumOfLeftLeaves ( root ) << endl ; return 0 ; }
Count of nested polygons that can be drawn by joining vertices internally | C ++ program for the above approach ; Function that counts the nested polygons inside another polygons ; Stores the count ; Child polygons can only existss if parent polygon has sides > 5 ; Get next nested polygon ; Return the count ; Driver Code ; Given side of polygon ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countNestedPolygons ( int sides ) { int count = 0 ; while ( sides > 5 ) { sides /= 2 ; count += 1 ; } return count ; } int main ( ) { int N = 12 ; cout << countNestedPolygons ( N ) ; return 0 ; }
Find Second largest element in an array | C ++ program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; find the largest element ; find the second largest element ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int largest = second = INT_MIN ; for ( int i = 0 ; i < arr_size ; i ++ ) { largest = max ( largest , arr [ i ] ) ; } for ( int i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] != largest ) second = max ( second , arr [ i ] ) ; } if ( second == INT_MIN ) printf ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element STRNEWLINE " ) ; else printf ( " The ▁ second ▁ largest ▁ element ▁ is ▁ % d STRNEWLINE " , second ) ; } int main ( ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print2largest ( arr , n ) ; return 0 ; }
Find Second largest element in an array | C ++ program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { cout << " ▁ Invalid ▁ Input ▁ " ; return ; } first = second = INT_MIN ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) { second = arr [ i ] ; } } if ( second == INT_MIN ) cout << " There ▁ is ▁ no ▁ second ▁ largest " " element STRNEWLINE " ; else cout << " The ▁ second ▁ largest ▁ element ▁ is ▁ " << second ; } int main ( ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; print2largest ( arr , n ) ; return 0 ; }
Queries to count characters having odd frequency in a range [ L , R ] | C ++ Program to implement the above problem ; Function to print the number of characters having odd frequencies for each query ; A function to construct the arr [ ] and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void queryResult ( int prefix [ ] , pair < int , int > Q ) { int l = Q . first ; int r = Q . second ; if ( l == 0 ) { int xorval = prefix [ r ] ; cout << __builtin_popcount ( xorval ) << endl ; } else { int xorval = prefix [ r ] ^ prefix [ l - 1 ] ; cout << __builtin_popcount ( xorval ) << endl ; } } void calculateCount ( string S , pair < int , int > Q [ ] , int m ) { int n = S . length ( ) ; int arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = ( 1 << ( S [ i ] - ' a ' ) ) ; } int prefix [ n ] ; int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x ^= arr [ i ] ; prefix [ i ] = x ; } for ( int i = 0 ; i < m ; i ++ ) { queryResult ( prefix , Q [ i ] ) ; } } int main ( ) { string S = " geeksforgeeks " ; pair < int , int > Q [ ] = { { 2 , 4 } , { 0 , 3 } , { 0 , 12 } } ; calculateCount ( S , Q , 3 ) ; }
Count of Binary Strings possible as per given conditions | C ++ Program to implement the above approach ; Function to generate maximum possible strings that can be generated ; Maximum possible strings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long countStrings ( long long A , long long B , long long K ) { long long X = ( A + B ) / ( K + 1 ) ; return ( min ( A , min ( B , X ) ) * ( K + 1 ) ) ; } int main ( ) { long long N = 101 , M = 231 , K = 15 ; cout << countStrings ( N , M , K ) ; return 0 ; }
Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | C ++ program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver Code ; Given matrix size ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { cout << f << " ▁ " ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { cout << k << " ▁ " ; } element_value += N ; } cout << endl ; i = i + 1 ; } } int main ( ) { int N = 4 ; UniqueMatrix ( N ) ; }
Maximum and minimum of an array using minimum number of comparisons | C ++ program of above implementation ; Pair struct is used to return two values from getMinMax ( ) ; If there is only one element then return it as min and max both ; If there are more than one elements , then initialize min and max ; Driver code
#include <iostream> NEW_LINE using namespace std ; struct Pair { int min ; int max ; } ; struct Pair getMinMax ( int arr [ ] , int n ) { struct Pair minmax ; int i ; if ( n == 1 ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 0 ] ; return minmax ; } if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . max = arr [ 1 ] ; minmax . min = arr [ 0 ] ; } for ( i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > minmax . max ) minmax . max = arr [ i ] ; else if ( arr [ i ] < minmax . min ) minmax . min = arr [ i ] ; } return minmax ; } int main ( ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; struct Pair minmax = getMinMax ( arr , arr_size ) ; cout << " Minimum ▁ element ▁ is ▁ " << minmax . min << endl ; cout << " Maximum ▁ element ▁ is ▁ " << minmax . max ; return 0 ; }
Find sum of all nodes of the given perfect binary tree |  ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; vector < int > vec [ l ] ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec [ l - 1 ] . push_back ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . size ( ) - 1 ) { vec [ i ] . push_back ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) ; k += 2 ; } } int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < vec [ i ] . size ( ) ; j ++ ) sum += vec [ i ] [ j ] ; } return sum ; } int main ( ) { int l = 3 ; cout << sumNodes ( l ) ; return 0 ; }
Maximum and minimum of an array using minimum number of comparisons | C ++ program of above implementation ; Structure is used to return two values from minMax ( ) ; If array has even number of elements then initialize the first two elements as minimum and maximum ; Set the starting index for loop ; If array has odd number of elements then initialize the first element as minimum and maximum ; Set the starting index for loop ; In the while loop , pick elements in pair and compare the pair with max and min so far ; Increment the index by 2 as two elements are processed in loop ; Driver code
#include <iostream> NEW_LINE using namespace std ; struct Pair { int min ; int max ; } ; struct Pair getMinMax ( int arr [ ] , int n ) { struct Pair minmax ; int i ; if ( n % 2 == 0 ) { if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 1 ] ; } i = 2 ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 0 ] ; i = 1 ; } while ( i < n - 1 ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] > minmax . max ) minmax . max = arr [ i ] ; if ( arr [ i + 1 ] < minmax . min ) minmax . min = arr [ i + 1 ] ; } else { if ( arr [ i + 1 ] > minmax . max ) minmax . max = arr [ i + 1 ] ; if ( arr [ i ] < minmax . min ) minmax . min = arr [ i ] ; } i += 2 ; } return minmax ; } int main ( ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; cout << " nMinimum ▁ element ▁ is ▁ " << minmax . min << endl ; cout << " nMaximum ▁ element ▁ is ▁ " << minmax . max ; return 0 ; }
MO 's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction) | Program to compute sum of ranges for different range queries . ; Structure to represent a query range ; Prints sum of all query ranges . m is number of queries n is the size of the array . ; One by one compute sum of all queries ; Left and right boundaries of current range ; Compute sum of current query range ; Print sum of current query range ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Query { int L , R ; } ; void printQuerySums ( int a [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; int sum = 0 ; for ( int j = L ; j <= R ; j ++ ) sum += a [ j ] ; cout << " Sum ▁ of ▁ [ " << L << " , ▁ " << R << " ] ▁ is ▁ " << sum << endl ; } } int main ( ) { int a [ ] = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Query q [ ] = { { 0 , 4 } , { 1 , 3 } , { 2 , 4 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; printQuerySums ( a , n , q , m ) ; return 0 ; }
Count of unique palindromic strings of length X from given string | C ++ implementation to count different palindromic string of length X from the given string S ; Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array ; Intitalise frequency array with 0 ; Count the frequency in the string ; Store frequency of the char ; check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; decrease the value of the char we put on the position i and n - i ; different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findways ( string s , int x ) { if ( x > ( int ) s . length ( ) ) return 0 ; long long int n = ( int ) s . length ( ) ; int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < n ; ++ i ) freq [ s [ i ] - ' a ' ] ++ ; multiset < int > se ; for ( int i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . insert ( freq [ i ] ) ; long long ans = 1 ; for ( int i = 0 ; i < x / 2 ; ++ i ) { long long int count = 0 ; for ( auto u : se ) { if ( u >= 2 ) count ++ ; } if ( count == 0 ) return 0 ; else ans = ans * count ; auto p = se . end ( ) ; p -- ; int val = * p ; se . erase ( p ) ; if ( val > 2 ) se . insert ( val - 2 ) ; } if ( x % 2 != 0 ) { long long int count = 0 ; for ( auto u : se ) if ( u > 0 ) count ++ ; ans = ans * count ; } return ans ; } int main ( ) { string s = " aaa " ; int x = 2 ; cout << findways ( s , x ) ; return 0 ; }
Min operations to reduce N to 1 by multiplying by A or dividing by B | C ++ program for the above approach ; Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable ' c ' ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Driver Code ; Given n , a and b ; Function Call
#include <iostream> NEW_LINE using namespace std ; int findIfPossible ( int n , int a , int b ) { if ( b % a != 0 ) { if ( n == 1 ) return 0 ; else if ( n % b != 0 ) return -1 ; else return ( int ) n / b ; } int c = b / a ; int x = 0 , y = 0 ; while ( n % b == 0 ) { n = n / b ; x ++ ; } while ( n % c == 0 ) { n = n / c ; y ++ ; } if ( n == 1 ) { int total_steps = x + ( 2 * y ) ; return total_steps ; } else return -1 ; } int main ( ) { int n = 48 ; int a = 3 , b = 12 ; cout << findIfPossible ( n , a , b ) ; return 0 ; }
Sqrt ( or Square Root ) Decomposition Technique | Set 1 ( Introduction ) | C ++ program to demonstrate working of Square Root Decomposition . ; original array ; decomposed array ; block size ; Time Complexity : O ( 1 ) ; Time Complexity : O ( sqrt ( n ) ) ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Fills values in input [ ] ; initiating block pointer ; calculating size of block ; building the decomposed array ; entering next block incementing block pointer ; Driver code ; We have used separate array for input because the purpose of this code is to explain SQRT decomposition in competitive programming where we have multiple inputs .
#include " iostream " NEW_LINE #include " math . h " NEW_LINE using namespace std ; #define MAXN  10000 NEW_LINE #define SQRSIZE  100 NEW_LINE int arr [ MAXN ] ; int block [ SQRSIZE ] ; int blk_sz ; void update ( int idx , int val ) { int blockNumber = idx / blk_sz ; block [ blockNumber ] += val - arr [ idx ] ; arr [ idx ] = val ; } int query ( int l , int r ) { int sum = 0 ; while ( l < r and l % blk_sz != 0 and l != 0 ) { sum += arr [ l ] ; l ++ ; } while ( l + blk_sz <= r ) { sum += block [ l / blk_sz ] ; l += blk_sz ; } while ( l <= r ) { sum += arr [ l ] ; l ++ ; } return sum ; } void preprocess ( int input [ ] , int n ) { int blk_idx = -1 ; blk_sz = sqrt ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = input [ i ] ; if ( i % blk_sz == 0 ) { blk_idx ++ ; } block [ blk_idx ] += arr [ i ] ; } } int main ( ) { int input [ ] = { 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 } ; int n = sizeof ( input ) / sizeof ( input [ 0 ] ) ; preprocess ( input , n ) ; cout << " query ( 3,8 ) ▁ : ▁ " << query ( 3 , 8 ) << endl ; cout << " query ( 1,6 ) ▁ : ▁ " << query ( 1 , 6 ) << endl ; update ( 8 , 0 ) ; cout << " query ( 8,8 ) ▁ : ▁ " << query ( 8 , 8 ) << endl ; return 0 ; }
Find two numbers with sum N such that neither of them contains digit K | C ++ implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break string D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; string removeLeadingZeros ( string str ) { int i = 0 ; int n = str . length ( ) ; while ( str [ i ] == '0' && i < n ) i ++ ; str . erase ( 0 , i ) ; return str ; } void findPairs ( int sum , int K ) { string A , B ; A = " " ; B = " " ; string N = to_string ( sum ) ; int n = N . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int D = N [ i ] - '0' ; if ( D == K ) { int D1 , D2 ; D1 = D / 2 ; D2 = D / 2 + D % 2 ; A = A + char ( D1 + '0' ) ; B = B + char ( D2 + '0' ) ; } else { A = A + char ( D + '0' ) ; B = B + '0' ; } } A = removeLeadingZeros ( A ) ; B = removeLeadingZeros ( B ) ; cout << A << " , ▁ " << B << endl ; } int main ( ) { int N = 33673 ; int K = 3 ; findPairs ( N , K ) ; return 0 ; }