codeparrot/xlcost-text-to-code huggingface dataset:
Search in a sorted 2D matrix ( Stored in row major order ) | C ++ program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  3 NEW_LINE #define N  4 NEW_LINE bool binarySearch1D ( int arr [ ] , int K ) { int low = 0 ; int high = N - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] == K ) return true ; if ( arr [ mid ] < K ) low = mid + 1 ; else high = mid - 1 ; } return false ; } bool searchMatrix ( int matrix [ M ] [ N ] , int K ) { int low = 0 ; int high = M - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( K >= matrix [ mid ] [ 0 ] && K <= matrix [ mid ] [ N - 1 ] ) return binarySearch1D ( matrix [ mid ] , K ) ; if ( K < matrix [ mid ] [ 0 ] ) high = mid - 1 ; else low = mid + 1 ; } return false ; } int main ( ) { int matrix [ M ] [ N ] = { { 1 , 3 , 5 , 7 } , { 10 , 11 , 16 , 20 } , { 23 , 30 , 34 , 50 } } ; int K = 3 ; if ( searchMatrix ( matrix , K ) ) cout << " Found " << endl ; else cout << " Not ▁ found " << endl ; }
Program to find Nth term divisible by a or b | C ++ program to find nth term divisible by a or b ; Function to return gcd of a and b ; Function to calculate how many numbers from 1 to num are divisible by a or b ; calculate number of terms divisible by a and by b then , remove the terms which is are divisible by both a and b ; Binary search to find the nth term divisible by a or b ; set low to 1 and high to max ( a , b ) * n , here we have taken high as 10 ^ 18 ; if the current term is less than n then we need to increase low to mid + 1 ; if current term is greater than equal to n then high = mid ; Driver code
#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 divTermCount ( int a , int b , int lcm , int num ) { return num / a + num / b - num / lcm ; } int findNthTerm ( int a , int b , int n ) { int low = 1 , high = INT_MAX , mid ; int lcm = ( a * b ) / gcd ( a , b ) ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , lcm , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } int main ( ) { int a = 2 , b = 5 , n = 10 ; cout << findNthTerm ( a , b , n ) << endl ; return 0 ; }
Replace all occurrences of pi with 3.14 in a given string | C ++ program to replace all pi in a given string with 3.14 ; Function to replace all occurrences of pi in a given with 3.14 ; Iterate through second last element of the string ; If current and current + 1 alphabets form the word ' pi ' append 3.14 to output ; Append the current letter ; Return the output string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string replacePi ( string input ) { string output ; int size = input . length ( ) ; for ( int i = 0 ; i < size ; ++ i ) { if ( i + 1 < size and input [ i ] == ' p ' and input [ i + 1 ] == ' i ' ) { output += "3.14" ; i ++ ; } else { output += input [ i ] ; } } return output ; } int main ( ) { string input = "2 ▁ * ▁ pi ▁ + ▁ 3 ▁ * ▁ pi ▁ = ▁ 5 ▁ * ▁ pi " ; cout << replacePi ( input ) ; return 0 ; }
Number of elements that can be seen from right side | CPP program to find number of elements that can be seen from right side . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfElements ( int height [ ] , int n ) { int max_so_far = 0 ; int coun = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] > max_so_far ) { max_so_far = height [ i ] ; coun ++ ; } } return coun ; } int main ( ) { int n = 6 ; int height [ ] = { 4 , 8 , 2 , 0 , 0 , 5 } ; cout << numberOfElements ( height , n ) ; return 0 ; }
Check if a pair with given product exists in Linked list | CPP code to find the pair with given product ; Link list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Function to check if pair with the given product exists in the list Takes head pointer of the linked list and product ; Check if pair exits ; Driver program to test above function ; Start with the empty list ; Use push ( ) to construct linked list ; function to print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } bool check_pair_product ( struct Node * head , int prod ) { unordered_set < int > s ; struct Node * p = head ; while ( p != NULL ) { int curr = p -> data ; if ( ( prod % curr == 0 ) && ( s . find ( prod / curr ) != s . end ( ) ) ) { cout << curr << " ▁ " << prod / curr ; return true ; } s . insert ( p -> data ) ; p = p -> next ; } return false ; } int main ( ) { struct Node * head = NULL ; push ( & head , 1 ) ; push ( & head , 2 ) ; push ( & head , 1 ) ; push ( & head , 12 ) ; push ( & head , 1 ) ; push ( & head , 18 ) ; push ( & head , 47 ) ; push ( & head , 16 ) ; push ( & head , 12 ) ; push ( & head , 14 ) ; bool res = check_pair_product ( head , 24 ) ; if ( res == false ) cout << " NO ▁ PAIR ▁ EXIST " ; return 0 ; }
Largest element in the array that is repeated exactly k times | C ++ implementation of the approach ; Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; if ( k == 1 && arr [ n - 2 ] != arr [ n - 1 ] ) { cout << arr [ n - 1 ] << endl ; return ; } int count = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) count ++ ; else count = 1 ; if ( count == k && ( i == 0 || ( arr [ i - 1 ] != arr [ i ] ) ) ) { cout << arr [ i ] << endl ; return ; } } cout << " No ▁ such ▁ element " << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 2 ; int n = sizeof ( arr ) / sizeof ( int ) ; solve ( arr , n , k ) ; return 0 ; }
Largest element in the array that is repeated exactly k times | C ++ implementation of above approach ; Function that finds the largest element that occurs exactly ' k ' times ; store the frequency of each element ; to store the maximum element ; if current element has frequency ' k ' and current maximum hasn 't been set ; set the current maximum ; if current element has frequency ' k ' and it is greater than the current maximum ; change the current maximum ; if there is no element with frequency ' k ' ; print the largest element with frequency ' k ' ; Driver code ; find the largest element that is repeated K times
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int arr [ ] , int n , int k ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { m [ arr [ i ] ] ++ ; } int max = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ arr [ i ] ] == k && max == INT_MIN ) { max = arr [ i ] ; } else if ( m [ arr [ i ] ] == k && max < arr [ i ] ) { max = arr [ i ] ; } } if ( max == INT_MIN ) cout << " No ▁ such ▁ element " << endl ; else cout << max << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 4 ; int n = sizeof ( arr ) / sizeof ( int ) ; solve ( arr , n , k ) ; return 0 ; }
Sum and Product of minimum and maximum element of an Array | CPP program to find the sum and product of minimum and maximum element in an array ; Function to find minimum element ; Function to find maximum element ; Function to get Sum ; Function to get product ; Driver Code ; Sum of min and max element ; Product of min and max element
#include <iostream> NEW_LINE using namespace std ; int getMin ( int arr [ ] , int n ) { int res = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) res = min ( res , arr [ i ] ) ; return res ; } int getMax ( int arr [ ] , int n ) { int res = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) res = max ( res , arr [ i ] ) ; return res ; } int findSum ( int arr [ ] , int n ) { int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; return min + max ; } int findProduct ( int arr [ ] , int n ) { int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; return min * max ; } int main ( ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Sum ▁ = ▁ " << findSum ( arr , n ) << endl ; cout << " Product ▁ = ▁ " << findProduct ( arr , n ) ; return 0 ; }
Count the number of pop operations on stack to get each element of the array | C ++ program to implement above approach ; Function to find the count ; Hashmap to store all the elements which are popped once . ; Check if the number is present in the hashmap Or in other words been popped out from the stack before . ; Keep popping the elements while top is not equal to num ; Pop the top ie . equal to num ; Print the number of elements popped . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countEle ( stack < int > & s , int a [ ] , int N ) { unordered_map < int , bool > mp ; for ( int i = 0 ; i < N ; ++ i ) { int num = a [ i ] ; if ( mp . find ( num ) != mp . end ( ) ) cout << "0 ▁ " ; else { int cnt = 0 ; while ( s . top ( ) != num ) { mp [ s . top ( ) ] = true ; s . pop ( ) ; cnt ++ ; } s . pop ( ) ; cnt ++ ; cout << cnt << " ▁ " ; } } } int main ( ) { int N = 5 ; stack < int > s ; s . push ( 1 ) ; s . push ( 2 ) ; s . push ( 3 ) ; s . push ( 4 ) ; s . push ( 6 ) ; int a [ ] = { 6 , 3 , 4 , 1 , 2 } ; countEle ( s , a , N ) ; return 0 ; }
Queries to check whether a given digit is present in the given Range | CPP program to answer Queries to check whether a given digit is present in the given range ; Segment Tree with set at each node ; Funtiom to build the segment tree ; Left child node ; Right child node ; Merging child nodes to get parent node . Since set is used , it will remove redundant digits . ; Function to query a range ; Complete Overlapp condition return true if digit is present . else false . ; No Overlapp condition Return false ; If digit is found in any child return true , else False ; Driver Code ; Build the tree ; Query 1 ; Query 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  6 NEW_LINE set < int > Tree [ 6 * N ] ; void buildTree ( int * arr , int idx , int s , int e ) { if ( s == e ) { Tree [ idx ] . insert ( arr [ s ] ) ; return ; } int mid = ( s + e ) >> 1 ; buildTree ( arr , 2 * idx , s , mid ) ; buildTree ( arr , 2 * idx + 1 , mid + 1 , e ) ; for ( auto it : Tree [ 2 * idx ] ) { Tree [ idx ] . insert ( it ) ; } for ( auto it : Tree [ 2 * idx + 1 ] ) { Tree [ idx ] . insert ( it ) ; } } bool query ( int idx , int s , int e , int qs , int qe , int x ) { if ( qs <= s && e <= qe ) { if ( Tree [ idx ] . count ( x ) != 0 ) { return true ; } else return false ; } if ( qe < s e < qs ) { return false ; } int mid = ( s + e ) >> 1 ; bool LeftAns = query ( 2 * idx , s , mid , qs , qe , x ) ; bool RightAns = query ( 2 * idx + 1 , mid + 1 , e , qs , qe , x ) ; return LeftAns or RightAns ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 9 , 8 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; buildTree ( arr , 1 , 0 , n - 1 ) ; int l , r , x ; l = 0 , r = 3 , x = 2 ; if ( query ( 1 , 0 , n - 1 , l , r , x ) ) cout << " YES " << ' ' ; else cout << " NO " << ' ' ; l = 2 , r = 5 , x = 3 ; if ( query ( 1 , 0 , n - 1 , l , r , x ) ) cout << " YES " << ' ' ; else cout << " NO " << ' ' ; return 0 ; }
Count characters with same neighbors | C ++ implementation of above approach ; Function to count the characters with same adjacent characters ; if length is less than 3 then return length as there will be only two characters ; Traverse the string ; Increment the count if the previous and next character is same ; Return count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countChar ( string str ) { int n = str . length ( ) ; if ( n <= 2 ) return n ; int count = 2 ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( str [ i - 1 ] == str [ i + 1 ] ) count ++ ; return count ; } int main ( ) { string str = " egeeksk " ; cout << countChar ( str ) ; return 0 ; }
Length of largest sub | C ++ implementation of above approach ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the length of longest subarray having count of primes more than count of non - primes ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider - 1 as non primes and 1 as primes ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime [ 1000000 + 5 ] ; void findPrime ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= 1000000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= 1000000 ; i += p ) prime [ i ] = false ; } } } int lenOfLongSubarr ( int arr [ ] , int n ) { unordered_map < int , int > um ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += prime [ arr [ i ] ] == 0 ? -1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - 1 ) != um . end ( ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } int main ( ) { findPrime ( ) ; int arr [ ] = { 1 , 9 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << lenOfLongSubarr ( arr , n ) << endl ; return 0 ; }
First strictly smaller element in a sorted array in Java | C ++ program to find first element that is strictly smaller than given target . ; Minimum size of the array should be 1 ; If target lies beyond the max element , than the index of strictly smaller value than target should be ( end - 1 ) ; Move to the left side if the target is smaller ; Move right side ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int next ( int arr [ ] , int target , int end ) { if ( end == 0 ) return -1 ; if ( target > arr [ end - 1 ] ) return end - 1 ; int start = 0 ; int ans = -1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] >= target ) { end = mid - 1 ; } else { ans = mid ; start = mid + 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 , 8 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( next ( arr , 5 , n ) ) ; return 0 ; }
First strictly greater element in a sorted array in Java | C ++ program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int next ( int arr [ ] , int target , int end ) { int start = 0 ; int ans = -1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] <= target ) start = mid + 1 ; else { ans = mid ; end = mid - 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 , 8 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << next ( arr , 8 , n ) ; return 0 ; }
Josephus Problem | ( Iterative Solution ) | Iterative solution for Josephus Problem ; Function for finding the winning child . ; For finding out the removed chairs in each iteration ; Driver function to find the winning child
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int find ( long long int n , long long int k ) { long long int sum = 0 , i ; for ( i = 2 ; i <= n ; i ++ ) sum = ( sum + k ) % i ; return sum + 1 ; } int main ( ) { int n = 14 , k = 2 ; cout << find ( n , k ) ; return 0 ; }
Reallocation of elements based on Locality of Reference | C ++ program to implement search for an item that is searched again and again . ; A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool search ( int arr [ ] , int n , int x ) { int res = -1 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res = i ; if ( res == -1 ) return false ; int temp = arr [ res ] ; for ( int i = res ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = temp ; return true ; } int main ( ) { int arr [ ] = { 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 } ; int q [ ] = { 63 , 63 , 86 , 63 , 78 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; for ( int i = 0 ; i < m ; i ++ ) search ( arr , n , q [ i ] ) ? cout << " Yes ▁ " : cout << " No ▁ " ; return 0 ; }
Probability of a key K present in array | C ++ code to find the probability of search key K present in array ; Function to find the probability ; find probability ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float kPresentProbability ( int a [ ] , int n , int k ) { float count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == k ) count ++ ; return count / n ; } int main ( ) { int A [ ] = { 4 , 7 , 2 , 0 , 8 , 7 , 5 } ; int K = 3 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << kPresentProbability ( A , N , K ) ; return 0 ; }
Largest subarray having sum greater than k | CPP program to find largest subarray having sum greater than k . ; Comparison function used to sort preSum vector . ; Function to find index in preSum vector upto which all prefix sum values are less than or equal to val . ; Starting and ending index of search space . ; To store required index value . ; If middle value is less than or equal to val then index can lie in mid + 1. . n else it lies in 0. . mid - 1. ; Function to find largest subarray having sum greater than or equal to k . ; Length of largest subarray . ; Vector to store pair of prefix sum and corresponding ending index value . ; To store current value of prefix sum . ; To store minimum index value in range 0. . i of preSum vector . ; Insert values in preSum vector . ; Update minInd array . ; If sum is greater than k , then answer is i + 1. ; If the sum is less than or equal to k , then find if there is a prefix array having sum that needs to be added to the current sum to make its value greater than k . If yes , then compare the length of updated subarray with maximum length found so far . ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( const pair < int , int > & a , const pair < int , int > & b ) { if ( a . first == b . first ) return a . second < b . second ; return a . first < b . first ; } int findInd ( vector < pair < int , int > > & preSum , int n , int val ) { int l = 0 ; int h = n - 1 ; int mid ; int ans = -1 ; while ( l <= h ) { mid = ( l + h ) / 2 ; if ( preSum [ mid ] . first <= val ) { ans = mid ; l = mid + 1 ; } else h = mid - 1 ; } return ans ; } int largestSub ( int arr [ ] , int n , int k ) { int i ; int maxlen = 0 ; vector < pair < int , int > > preSum ; int sum = 0 ; int minInd [ n ] ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; preSum . push_back ( { sum , i } ) ; } sort ( preSum . begin ( ) , preSum . end ( ) , compare ) ; minInd [ 0 ] = preSum [ 0 ] . second ; for ( i = 1 ; i < n ; i ++ ) { minInd [ i ] = min ( minInd [ i - 1 ] , preSum [ i ] . second ) ; } sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; if ( sum > k ) maxlen = i + 1 ; else { int ind = findInd ( preSum , n , sum - k - 1 ) ; if ( ind != -1 && minInd [ ind ] < i ) maxlen = max ( maxlen , i - minInd [ ind ] ) ; } } return maxlen ; } int main ( ) { int arr [ ] = { -2 , 1 , 6 , -3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 5 ; cout << largestSub ( arr , n , k ) ; return 0 ; }
Find the slope of the given number | C ++ implementation to find slope of a number ; function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int slopeOfNum ( string num , int n ) { int slope = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( num [ i ] > num [ i - 1 ] && num [ i ] > num [ i + 1 ] ) slope ++ ; else if ( num [ i ] < num [ i - 1 ] && num [ i ] < num [ i + 1 ] ) slope ++ ; } return slope ; } int main ( ) { string num = "1213321" ; int n = num . size ( ) ; cout << " Slope ▁ = ▁ " << slopeOfNum ( num , n ) ; return 0 ; }
Sudo Placement | Beautiful Pairs | C ++ code for finding required pairs ; The function to check if beautiful pair exists ; Set for hashing ; Traversing the first array ; Traversing the second array to check for every j corresponding to single i ; x + y = z = > x = y - z ; if such x exists then we return true ; hash to make use of it next time ; no pair exists ; Driver Code ; If pair exists then 1 else 0 2 nd argument as size of first array fourth argument as sizeof 2 nd array
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool pairExists ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( s . find ( arr2 [ j ] - arr1 [ i ] ) != s . end ( ) ) return true ; } s . insert ( arr1 [ i ] ) ; } return false ; } int main ( ) { int arr1 [ ] = { 1 , 5 , 10 , 8 } ; int arr2 [ ] = { 2 , 20 , 13 } ; if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) cout << 1 << endl ; else cout << 0 << endl ; return 0 ; }
Sudo Placement | Placement Tour | CPP Program to find the optimal number of elements such that the cumulative value should be less than given number ; This function returns true if the value cumulative according to received integer K is less than budget B , otherwise returns false ; Initialize a temporary array which stores the cumulative value of the original array ; Sort the array to find the smallest K values ; Check if the value is less than budget ; This function prints the optimal number of elements and respective cumulative value which is less than the given number ; Initialize answer as zero as optimal value may not exists ; If the current Mid Value is an optimal value , then try to maximize it ; Call Again to set the corresponding cumulative value for the optimal ans ; Driver Code ; Budget
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeOptimalValue ( int K , int arr [ ] , int N , int B , int & value ) { int tmp [ N ] ; for ( int i = 0 ; i < N ; i ++ ) tmp [ i ] = ( arr [ i ] + K * ( i + 1 ) ) ; sort ( tmp , tmp + N ) ; value = 0 ; for ( int i = 0 ; i < K ; i ++ ) value += tmp [ i ] ; return value <= B ; } void findNoOfElementsandValue ( int arr [ ] , int N , int B ) { int ans = 0 ; int cumulativeValue = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( canBeOptimalValue ( mid , arr , N , B , cumulativeValue ) ) { ans = mid ; start = mid + 1 ; } else end = mid - 1 ; } canBeOptimalValue ( ans , arr , N , B , cumulativeValue ) ; cout << ans << " ▁ " << cumulativeValue << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 6 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int B = 90 ; findNoOfElementsandValue ( arr , N , B ) ; return 0 ; }