codeparrot/xlcost-text-to-code huggingface dataset:
Find the count of distinct numbers in a range | C ++ Program to find the distinct elements in a range ; Function to perform queries in a range ; No overlap ; Totally Overlap ; Partial Overlap ; Finding the Answer for the left Child ; Finding the Answer for the right Child ; Combining the BitMasks ; Function to perform update operation in the Segment seg ; Forming the BitMask ; Updating the left Child ; Updating the right Child ; Updating the BitMask ; Building the Segment Tree ; Building the Initial BitMask ; Building the left seg tree ; Building the right seg tree ; Forming the BitMask ; Utility Function to answer the queries ; Counting the set bits which denote the distinct elements ; Updating the value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long query ( int start , int end , int left , int right , int node , long long seg [ ] ) { if ( end < left start > right ) { return 0 ; } else if ( start >= left && end <= right ) { return seg [ node ] ; } else { int mid = ( start + end ) / 2 ; long long leftChild = query ( start , mid , left , right , 2 * node , seg ) ; long long rightChild = query ( mid + 1 , end , left , right , 2 * node + 1 , seg ) ; return ( leftChild rightChild ) ; } } void update ( int left , int right , int index , int Value , int node , int ar [ ] , long long seg [ ] ) { if ( left == right ) { ar [ index ] = Value ; seg [ node ] = ( 1LL << Value ) ; return ; } int mid = ( left + right ) / 2 ; if ( index > mid ) { update ( mid + 1 , right , index , Value , 2 * node + 1 , ar , seg ) ; } else { update ( left , mid , index , Value , 2 * node , ar , seg ) ; } seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } void build ( int left , int right , int node , int ar [ ] , long long seg [ ] ) { if ( left == right ) { seg [ node ] = ( 1LL << ar [ left ] ) ; return ; } int mid = ( left + right ) / 2 ; build ( left , mid , 2 * node , ar , seg ) ; build ( mid + 1 , right , 2 * node + 1 , ar , seg ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } void getDistinctCount ( vector < vector < int > > & queries , int ar [ ] , long long seg [ ] , int n ) { for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { int op = queries [ i ] [ 0 ] ; if ( op == 2 ) { int l = queries [ i ] [ 1 ] , r = queries [ i ] [ 2 ] ; long long tempMask = query ( 0 , n - 1 , l - 1 , r - 1 , 1 , seg ) ; int countOfBits = 0 ; for ( int i = 63 ; i >= 0 ; i -- ) { if ( tempMask & ( 1LL << i ) ) { countOfBits ++ ; } } cout << countOfBits << ' ' ; } else { int index = queries [ i ] [ 1 ] ; int val = queries [ i ] [ 2 ] ; update ( 0 , n - 1 , index - 1 , val , 1 , ar , seg ) ; } } } int main ( ) { int n = 7 ; int ar [ ] = { 1 , 2 , 1 , 3 , 1 , 2 , 1 } ; long long seg [ 4 * n ] = { 0 } ; build ( 0 , n - 1 , 1 , ar , seg ) ; int q = 5 ; vector < vector < int > > queries = { { 2 , 1 , 4 } , { 1 , 4 , 2 } , { 1 , 5 , 2 } , { 2 , 4 , 6 } , { 2 , 1 , 7 } } ; getDistinctCount ( queries , ar , seg , n ) ; return 0 ; }
Smallest subarray with GCD as 1 | Segment Tree | C ++ implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Building the segment tree ; Two pointer variables ; To store the final answer ; Looping ; Incrementing j till we don 't get  a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxLen  30 NEW_LINE int seg [ 3 * maxLen ] ; int build ( int l , int r , int in , int * arr ) { if ( l == r ) return seg [ in ] = arr [ l ] ; int mid = ( l + r ) / 2 ; return seg [ in ] = __gcd ( build ( l , mid , 2 * in + 1 , arr ) , build ( mid + 1 , r , 2 * in + 2 , arr ) ) ; } int query ( int l , int r , int l1 , int r1 , int in ) { if ( l1 <= l and r <= r1 ) return seg [ in ] ; if ( l > r1 or r < l1 ) return 0 ; int mid = ( l + r ) / 2 ; return __gcd ( query ( l , mid , l1 , r1 , 2 * in + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * in + 2 ) ) ; } int findLen ( int * arr , int n ) { build ( 0 , n - 1 , 0 , arr ) ; int i = 0 , j = 0 ; int ans = INT_MAX ; while ( i < n ) { while ( j < n and query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = min ( ( j - i + 1 ) , ans ) ; i ++ ; j = max ( j , i ) ; } if ( ans == INT_MAX ) return -1 ; else return ans ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findLen ( arr , n ) ; return 0 ; }
Find an N x N grid whose xor of every row and column is equal | C ++ implementation of the approach ; Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findGrid ( int n ) { int arr [ n ] [ n ] ; int x = 0 ; for ( int i = 0 ; i < n / 4 ; i ++ ) { for ( int j = 0 ; j < n / 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { for ( int l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k ] [ j * 4 + l ] = x ; x ++ ; } } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << arr [ i ] [ j ] << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 4 ; findGrid ( n ) ; return 0 ; }
Cartesian tree from inorder traversal | Segment Tree | C ++ implementation of the approach ; Node of the BST ; Array to store segment tree ; Function to create segment - tree to answer range - max query ; Base case ; Maximum index in left range ; Maximum index in right range ; If value at l1 > r1 ; Else ; Returning the maximum in range ; Function to answer range max query ; Base cases ; Maximum in left range ; Maximum in right range ; l1 = - 1 means left range was out - side required range ; Returning the maximum among two ranges ; Function to print the inorder traversal of the binary tree ; Base case ; Traversing the left sub - tree ; Printing current node ; Traversing the right sub - tree ; Function to build cartesian tree ; Base case ; Maximum in the range ; Creating current node ; Creating left sub - tree ; Creating right sub - tree ; Returning current node ; Driver code ; In - order traversal of cartesian tree ; Size of the array ; Building the segment tree ; Building and printing cartesian tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxLen  30 NEW_LINE struct node { int data ; node * left ; node * right ; node ( int data ) { left = NULL ; right = NULL ; this -> data = data ; } } ; int segtree [ maxLen * 4 ] ; int buildTree ( int l , int r , int i , int * arr ) { if ( l == r ) { segtree [ i ] = l ; return l ; } int l1 = buildTree ( l , ( l + r ) / 2 , 2 * i + 1 , arr ) ; int r1 = buildTree ( ( l + r ) / 2 + 1 , r , 2 * i + 2 , arr ) ; if ( arr [ l1 ] > arr [ r1 ] ) segtree [ i ] = l1 ; else segtree [ i ] = r1 ; return segtree [ i ] ; } int rangeMax ( int l , int r , int rl , int rr , int i , int * arr ) { if ( r < rl l > rr ) return -1 ; if ( l >= rl and r <= rr ) return segtree [ i ] ; int l1 = rangeMax ( l , ( l + r ) / 2 , rl , rr , 2 * i + 1 , arr ) ; int r1 = rangeMax ( ( l + r ) / 2 + 1 , r , rl , rr , 2 * i + 2 , arr ) ; if ( l1 == -1 ) return r1 ; if ( r1 == -1 ) return l1 ; if ( arr [ l1 ] > arr [ r1 ] ) return l1 ; else return r1 ; } void inorder ( node * curr ) { if ( curr == NULL ) return ; inorder ( curr -> left ) ; cout << curr -> data << " ▁ " ; inorder ( curr -> right ) ; } node * createCartesianTree ( int l , int r , int * arr , int n ) { if ( r < l ) return NULL ; int m = rangeMax ( 0 , n - 1 , l , r , 0 , arr ) ; node * curr = new node ( arr [ m ] ) ; curr -> left = createCartesianTree ( l , m - 1 , arr , n ) ; curr -> right = createCartesianTree ( m + 1 , r , arr , n ) ; return curr ; } int main ( ) { int arr [ ] = { 8 , 11 , 21 , 100 , 5 , 70 , 55 } ; int n = sizeof ( arr ) / sizeof ( int ) ; buildTree ( 0 , n - 1 , 0 , arr ) ; inorder ( createCartesianTree ( 0 , n - 1 , arr , n ) ) ; }
Kth smallest element in the array using constant space when array can 't be modified | C ++ implementation of the approach ; Function to return the kth smallest element from the array ; Minimum and maximum element from the array ; Modified binary search ; To store the count of elements from the array which are less than mid and the elements which are equal to mid ; If mid is the kth smallest ; If the required element is less than mid ; If the required element is greater than mid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthSmallest ( int * arr , int k , int n ) { int low = * min_element ( arr , arr + n ) ; int high = * max_element ( arr , arr + n ) ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int countless = 0 , countequal = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] < mid ) ++ countless ; else if ( arr [ i ] == mid ) ++ countequal ; } if ( countless < k && ( countless + countequal ) >= k ) { return mid ; } else if ( countless >= k ) { high = mid - 1 ; } else if ( countless < k && countless + countequal < k ) { low = mid + 1 ; } } } int main ( ) { int arr [ ] = { 7 , 10 , 4 , 3 , 20 , 15 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 3 ; cout << kthSmallest ( arr , k , n ) ; return 0 ; }
Interactive Problems in Competitive Programming |  ; Iterating from lower_bound to upper_bound ; Input the response from the judge
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int lower_bound = 2 ; int upper_bound = 10 ; for ( int i = lower_bound ; i <= upper_bound ; i ++ ) { cout << i << endl ; int response ; cin >> response ; if ( response == 0 ) { cout << " Number ▁ guessed ▁ is ▁ : " << i ; break ; } } return 0 ; }
Find 2 ^ ( 2 ^ A ) % B | C ++ implementation of the approach ; Function to return 2 ^ ( 2 ^ A ) % B ; Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ A ) % B
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll F ( ll A , ll B ) { if ( A == 1 ) return ( 4 % B ) ; else { ll temp = F ( A - 1 , B ) ; return ( temp * temp ) % B ; } } int main ( ) { ll A = 25 , B = 50 ; cout << F ( A , B ) ; return 0 ; }
Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | C ++ implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000007 NEW_LINE int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = max ( l , a ) ; int R = min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } int main ( ) { int l = 98 , r = 102 ; cout << rangeSum ( l , r ) ; return 0 ; }
Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | C ++ implementation of the approach ; To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > permutation ; void printPermutation ( ) { for ( auto i : permutation ) cout << i << " ▁ " ; } int give_random_number ( int l , int r ) { int x = rand ( ) % ( r - l + 1 ) + l ; return x ; } void generate_random_permutation ( int l , int r ) { if ( l > r ) return ; int n = give_random_number ( l , r ) ; permutation . push_back ( n ) ; generate_random_permutation ( l , n - 1 ) ; generate_random_permutation ( n + 1 , r ) ; } int main ( ) { int l = 5 ; int r = 15 ; generate_random_permutation ( l , r ) ; printPermutation ( ) ; return 0 ; }
Minimum number N such that total set bits of all numbers from 1 to N is at | C ++ implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  99999 NEW_LINE #define size  10 NEW_LINE int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } int findMinimum ( int x ) { int low = 0 , high = 100000 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } int main ( ) { int x = 20 ; cout << findMinimum ( x ) ; return 0 ; }
Range and Update Sum Queries with Factorial | CPP program to calculate sum of factorials in an interval and update with two types of operations ; Modulus ; Maximum size of input array ; Size for factorial array ; structure for queries with members type , leftIndex , rightIndex of the query ; function for updating the value ; function for calculating the required sum between two indexes ; function to return answer to queries ; Precomputing factorials ; Declaring a Set ; inserting indexes of those numbers which are lesser than 40 ; update query of the 1 st type ; find the left index of query in the set using binary search ; if it crosses the right index of query or end of set , then break ; update the value of arr [ i ] to its new value ; if updated value becomes greater than or equal to 40 remove it from the set ; increment the index ; update query of the 2 nd type ; update the value to its new value ; If the value is less than 40 , insert it into set , otherwise remove it ; sum query of the 3 rd type ; Driver Code to test above functions ; input array using 1 - based indexing ; declaring array of structure of type queries ; answer the Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1e9 ; const int MAX = 100 ; const int SZ = 40 ; int BIT [ MAX + 1 ] , fact [ SZ + 1 ] ; struct queries { int type , l , r ; } ; void update ( int x , int val , int n ) { for ( x ; x <= n ; x += x & - x ) BIT [ x ] += val ; } int sum ( int x ) { int s = 0 ; for ( x ; x > 0 ; x -= x & - x ) s += BIT [ x ] ; return s ; } void answerQueries ( int arr [ ] , queries que [ ] , int n , int q ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < 41 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; set < int > s ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < 40 ) { s . insert ( i ) ; update ( i , fact [ arr [ i ] ] , n ) ; } else update ( i , 0 , n ) ; } for ( int i = 0 ; i < q ; i ++ ) { if ( que [ i ] . type == 1 ) { while ( true ) { auto it = s . lower_bound ( que [ i ] . l ) ; if ( it == s . end ( ) * it > que [ i ] . r ) break ; que [ i ] . l = * it ; int val = arr [ * it ] + 1 ; update ( * it , fact [ val ] - fact [ arr [ * it ] ] , n ) ; arr [ * it ] ++ ; if ( arr [ * it ] >= 40 ) s . erase ( * it ) ; que [ i ] . l ++ ; } } else if ( que [ i ] . type == 2 ) { int idx = que [ i ] . l ; int val = que [ i ] . r ; update ( idx , fact [ val ] - fact [ arr [ idx ] ] , n ) ; arr [ idx ] = val ; if ( val < 40 ) s . insert ( idx ) ; else s . erase ( idx ) ; } else cout << ( sum ( que [ i ] . r ) - sum ( que [ i ] . l - 1 ) ) << endl ; } } int main ( ) { int q = 6 ; int arr [ ] = { 0 , 1 , 2 , 1 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; queries que [ q + 1 ] ; que [ 0 ] . type = 3 , que [ 0 ] . l = 1 , que [ 0 ] . r = 5 ; que [ 1 ] . type = 1 , que [ 1 ] . l = 1 , que [ 1 ] . r = 3 ; que [ 2 ] . type = 2 , que [ 2 ] . l = 2 , que [ 2 ] . r = 4 ; que [ 3 ] . type = 3 , que [ 3 ] . l = 2 , que [ 3 ] . r = 4 ; que [ 4 ] . type = 1 , que [ 4 ] . l = 2 , que [ 4 ] . r = 5 ; que [ 5 ] . type = 3 , que [ 5 ] . l = 1 , que [ 5 ] . r = 5 ; answerQueries ( arr , que , n , q ) ; return 0 ; }
Numbers whose factorials end with n zeros | Binary search based CPP program to find numbers with n trailing zeros . ; Function to calculate trailing zeros ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int trailingZeroes ( int n ) { int cnt = 0 ; while ( n > 0 ) { n /= 5 ; cnt += n ; } return cnt ; } void binarySearch ( int n ) { int low = 0 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } vector < int > result ; while ( trailingZeroes ( low ) == n ) { result . push_back ( low ) ; low ++ ; } for ( int i = 0 ; i < result . size ( ) ; i ++ ) cout << result [ i ] << " ▁ " ; } int main ( ) { int n = 2 ; binarySearch ( n ) ; return 0 ; }
Number of days after which tank will become empty | C / C ++ code to find number of days after which tank will become empty ; Utility method to get sum of first n numbers ; Method returns minimum number of days after which tank will become empty ; if water filling is more than capacity then after C days only tank will become empty ; initialize binary search variable ; loop until low is less than high ; if cumulate sum is greater than ( C - l ) then search on left side ; if ( C - l ) is more then search on right side ; final answer will be obtained by adding l to binary search result ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCumulateSum ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } int minDaysToEmpty ( int C , int l ) { if ( C <= l ) return C ; int lo = 0 ; int hi = 1e4 ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( getCumulateSum ( mid ) >= ( C - l ) ) hi = mid ; else lo = mid + 1 ; } return ( l + lo ) ; } int main ( ) { int C = 5 ; int l = 2 ; cout << minDaysToEmpty ( C , l ) << endl ; return 0 ; }
Number of days after which tank will become empty | C / C ++ code to find number of days after which tank will become empty ; Method returns minimum number of days after which tank will become empty ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDaysToEmpty ( int C , int l ) { if ( l >= C ) return C ; double eq_root = ( std :: sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 ; return std :: ceil ( eq_root ) + l ; } int main ( ) { cout << minDaysToEmpty ( 5 , 2 ) << endl ; cout << minDaysToEmpty ( 6514683 , 4965 ) << endl ; return 0 ; }
K | Program to find kth element from two sorted arrays ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int sorted1 [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; cout << kth ( arr1 , arr2 , 5 , 4 , k ) ; return 0 ; }
K | Program to find k - th element from two sorted arrays
#include <iostream> NEW_LINE using namespace std ; int kth ( int * arr1 , int * arr2 , int * end1 , int * end2 , int k ) { if ( arr1 == end1 ) return arr2 [ k ] ; if ( arr2 == end2 ) return arr1 [ k ] ; int mid1 = ( end1 - arr1 ) / 2 ; int mid2 = ( end2 - arr2 ) / 2 ; if ( mid1 + mid2 < k ) { if ( arr1 [ mid1 ] > arr2 [ mid2 ] ) return kth ( arr1 , arr2 + mid2 + 1 , end1 , end2 , k - mid2 - 1 ) ; else return kth ( arr1 + mid1 + 1 , arr2 , end1 , end2 , k - mid1 - 1 ) ; } else { if ( arr1 [ mid1 ] > arr2 [ mid2 ] ) return kth ( arr1 , arr2 , arr1 + mid1 , end2 , k ) ; else return kth ( arr1 , arr2 , end1 , arr2 + mid2 , k ) ; } } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; cout << kth ( arr1 , arr2 , arr1 + 5 , arr2 + 4 , k - 1 ) ; return 0 ; }
K | C ++ Program to find kth element from two sorted arrays ; In case we have reached end of array 1 ; In case we have reached end of array 2 ; k should never reach 0 or exceed sizes of arrays ; Compare first elements of arrays and return ; Size of array 1 is less than k / 2 ; Last element of array 1 is not kth We can directly return the ( k - m ) th element in array 2 ; Size of array 2 is less than k / 2 ; Normal comparison , move starting index of one array k / 2 to the right ; Driver code
#include <iostream> NEW_LINE using namespace std ; int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k , int st1 = 0 , int st2 = 0 ) { if ( st1 == m ) return arr2 [ st2 + k - 1 ] ; if ( st2 == n ) return arr1 [ st1 + k - 1 ] ; if ( k == 0 || k > ( m - st1 ) + ( n - st2 ) ) return -1 ; if ( k == 1 ) return ( arr1 [ st1 ] < arr2 [ st2 ] ) ? arr1 [ st1 ] : arr2 [ st2 ] ; int curr = k / 2 ; if ( curr - 1 >= m - st1 ) { if ( arr1 [ m - 1 ] < arr2 [ st2 + curr - 1 ] ) return arr2 [ st2 + ( k - ( m - st1 ) - 1 ) ] ; else return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } if ( curr - 1 >= n - st2 ) { if ( arr2 [ n - 1 ] < arr1 [ st1 + curr - 1 ] ) return arr1 [ st1 + ( k - ( n - st2 ) - 1 ) ] ; else return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } else { if ( arr1 [ curr + st1 - 1 ] < arr2 [ curr + st2 - 1 ] ) return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; else return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; cout << kth ( arr1 , arr2 , 5 , 4 , k ) ; return 0 ; }
K | C ++ Program to find kth element from two sorted arrays Time Complexity : O ( log k ) ; let m <= n ; Check if arr1 is empty returning k - th element of arr2 ; Check if k = 1 return minimum of first two elements of both arrays ; Now the divide and conquer part ; Now we need to find only k - j th element since we have found out the lowest j ; Now we need to find only k - i th element since we have found out the lowest i ; Driver code
#include <iostream> NEW_LINE using namespace std ; int kth ( int arr1 [ ] , int m , int arr2 [ ] , int n , int k ) { if ( k > ( m + n ) k < 1 ) return -1 ; if ( m > n ) return kth ( arr2 , n , arr1 , m , k ) ; if ( m == 0 ) return arr2 [ k - 1 ] ; if ( k == 1 ) return min ( arr1 [ 0 ] , arr2 [ 0 ] ) ; int i = min ( m , k / 2 ) , j = min ( n , k / 2 ) ; if ( arr1 [ i - 1 ] > arr2 [ j - 1 ] ) return kth ( arr1 , m , arr2 + j , n - j , k - j ) ; else return kth ( arr1 + i , m - i , arr2 , n , k - i ) ; } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int m = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int k = 5 ; int ans = kth ( arr1 , m , arr2 , n , k ) ; if ( ans == -1 ) cout << " Invalid ▁ query " ; else cout << ans ; return 0 ; }
K | C ++ Program to find kth element from two sorted arrays ; Function to find K - th min ; Declaring a min heap ; Pushing elements for array a to min - heap ; Pushing elements for array b to min - heap ; Poping - out K - 1 elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kth ( int * a , int * b , int n , int m , int k ) { priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < n ; i ++ ) { pq . push ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { pq . push ( b [ i ] ) ; } while ( k -- > 1 ) { pq . pop ( ) ; } return pq . top ( ) ; } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; cout << kth ( arr1 , arr2 , 5 , 4 , k ) ; return 0 ; }