codeparrot/xlcost-text-to-code huggingface dataset:
Minimize splits to generate monotonous Substrings from given String | C ++ program to implement the above approach ; Function to return final result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minReqSubstring ( string s , int n ) { char ongoing = ' N ' ; int count = 0 , l = s . size ( ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( ongoing == ' N ' ) { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' N ' ; } else { ongoing = ' I ' ; } } else if ( ongoing == ' I ' ) { if ( s [ i ] > s [ i - 1 ] ) { ongoing = ' I ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' I ' ; } else { ongoing = ' N ' ; count += 1 ; } } else { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' D ' ; } else { ongoing = ' N ' ; count += 1 ; } } } return count + 1 ; } int main ( ) { string S = " aeccdhba " ; int n = S . size ( ) ; cout << ( minReqSubstring ( S , n ) ) ; return 0 ; }
Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | C ++ program to implement the above approach ; Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimum_required_operations ( int arr [ ] , int n , int k ) { int answer = 0 ; long long mod = 1000000007 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer %= mod ; } } return answer ; } int main ( ) { int a [ ] = { 9 , 50 , 4 , 14 , 42 , 89 } ; int k = 10 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( minimum_required_operations ( a , n , k ) ) ; return 0 ; }
Lexicographic smallest permutation of a String containing the second String as a Substring | C ++ program to implement the above approach ; Function to print the desired lexicographic smaller string ; Calculate length of the string ; Stores the frequencies of characters of string str1 ; Stores the frequencies of characters of string str2 ; Decrease the frequency of second string from that of of the first string ; To store the resultant string ; To find the index of first character of the string str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of string str2 append str2 ; Return the resultant string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findSmallestString ( string str1 , string str2 ) { int freq1 [ 26 ] , freq2 [ 26 ] ; memset ( freq1 , 0 , sizeof freq1 ) ; memset ( freq2 , 0 , sizeof freq2 ) ; int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } string res = " " ; int minIndex = str2 [ 0 ] - ' a ' ; for ( int i = 0 ; i < 26 ; ++ i ) { for ( int j = 0 ; j < freq1 [ i ] ; ++ j ) { res += ( char ) ( i + ' a ' ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } int main ( ) { string str1 = " geeksforgeeksfor " ; string str2 = " for " ; cout << findSmallestString ( str1 , str2 ) ; }
Restore original String from given Encrypted String by the given operations | C ++ Program to implement the above approach ; Function to decrypt and print the original strings ; If length is exceeded ; Reverse the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int decryptString ( string s , unsigned int N ) { for ( unsigned int i = 0 ; i < s . size ( ) ; i += 2 * N ) { auto end = s . begin ( ) + i + N ; if ( i + N > s . size ( ) ) end = s . end ( ) ; reverse ( s . begin ( ) + i , end ) ; } cout << s << endl ; } int main ( ) { string s = " ihTs ▁ suohld ▁ ebeas ! y " ; unsigned int N = 3 ; decryptString ( s , N ) ; return 0 ; }
Determine the winner of a game of deleting Characters from a String | C ++ program for the above approach ; Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void determineWinner ( string str ) { vector < int > A ( 10 ) ; int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { A [ int ( str [ i ] ) - 48 ] ++ ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { cout << " - 1" ; } else if ( sum1 > sum2 ) { cout << " Player ▁ 1" ; } else { cout << " Player ▁ 2" ; } } int main ( ) { string str = "78787" ; determineWinner ( str ) ; return 0 ; }
Generate a String from given Strings P and Q based on the given conditions | C ++ program to implement the above approach ; Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in freq [ ] to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void manipulateStrings ( string P , string Q ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < P . size ( ) ; i ++ ) { freq [ P [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { freq [ Q [ i ] - ' a ' ] -- ; } string sb = " " ; int pos = Q [ 0 ] - ' a ' ; for ( int i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } sb += Q ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb += c ; freq [ i ] -- ; } } cout << sb << endl ; } int main ( ) { string P = " geeksforgeeksfor " ; string Q = " for " ; manipulateStrings ( P , Q ) ; }
Minimum moves required to type a word in QWERTY based keyboard | C ++ program for the above approach ; Function that calculates the moves required to print the current string ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the string ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberMoves ( string s ) { int row [ ] = { 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 } ; int n = s . length ( ) ; int move = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( row [ s [ i ] - ' a ' ] != row [ s [ i - 1 ] - ' a ' ] ) { move ++ ; } } return move ; } int main ( ) { string str = " geeksforgeeks " ; cout << numberMoves ( str ) ; return 0 ; }
Queries to calculate the Sum of Array elements in the range [ L , R ] having indices as multiple of K | C ++ Program to implement the above approach ; Structure of a Query ; Function to calculate the sum of array elements at indices from range [ L , R ] which are multiples of K for each query ; Stores Prefix Sum ; prefixSum [ i ] [ j ] : Stores the sum from indices [ 0 , j ] which are multiples of i ; If index j is a multiple of i ; Compute prefix sum ; Otherwise ; Traverse each query ; Sum of all indices upto R which are a multiple of K ; Sum of all indices upto L - 1 which are a multiple of K ; Calculate the difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int L ; int R ; int K ; } ; int kMultipleSum ( int arr [ ] , Node Query [ ] , int N , int Q ) { int prefixSum [ N + 1 ] [ N ] ; for ( int i = 1 ; i <= N ; i ++ ) { prefixSum [ i ] [ 0 ] = arr [ 0 ] ; for ( int j = 0 ; j < N ; j ++ ) { if ( j % i == 0 ) { prefixSum [ i ] [ j ] = arr [ j ] + prefixSum [ i ] [ j - 1 ] ; } else { prefixSum [ i ] [ j ] = prefixSum [ i ] [ j - 1 ] ; } } } for ( int i = 0 ; i < Q ; i ++ ) { int last = prefixSum [ Query [ i ] . K ] [ Query [ i ] . R ] ; int first ; if ( Query [ i ] . L == 0 ) { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L ] ; } else { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L - 1 ] ; } cout << last - first << endl ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int Q = 2 ; Node Query [ Q ] ; Query [ 0 ] . L = 2 , Query [ 0 ] . R = 5 , Query [ 0 ] . K = 2 ; Query [ 1 ] . L = 3 , Query [ 1 ] . R = 5 , Query [ 1 ] . K = 5 ; kMultipleSum ( arr , Query , N , Q ) ; }
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | C ++ program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1000000007 ; long countS ( string str [ ] , int n , int m ) { unordered_map < int , unordered_set < char > > counts ; for ( int i = 0 ; i < n ; i ++ ) { string s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts [ j ] . insert ( s [ j ] ) ; } } long result = 1 ; for ( auto index : counts ) { result = ( result * counts [ index . first ] . size ( ) ) % mod ; } return result ; } int main ( ) { string str [ ] = { "112" , "211" } ; int N = 2 , M = 3 ; cout << countS ( str , N , M ) ; return 0 ; }
Longest Subarrays having each Array element as the maximum | C ++ Program to implement the above approach ; Function to find the maximum length of Subarrays for each element of the array having it as the maximum ; Initialize the bounds ; Iterate to find greater element on the left ; If greater element is found ; Decrement left pointer ; If boundary is exceeded ; Iterate to find greater element on the right ; If greater element is found ; Increment right pointer ; If boundary is exceeded ; Length of longest subarray where arr [ i ] is the largest ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n , int arr [ ] ) { int i , ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { int left = max ( i - 1 , 0 ) ; int right = min ( n - 1 , i + 1 ) ; while ( left >= 0 ) { if ( arr [ left ] > arr [ i ] ) { left ++ ; break ; } left -- ; } if ( left < 0 ) left ++ ; while ( right < n ) { if ( arr [ right ] > arr [ i ] ) { right -- ; break ; } right ++ ; } if ( right >= n ) right -- ; ans = 1 + right - left ; cout << ans << " ▁ " ; } } int main ( ) { int arr [ ] = { 4 , 2 , 1 } ; int n = sizeof arr / sizeof arr [ 0 ] ; solve ( n , arr ) ; return 0 ; }
Count of Substrings having Sum equal to their Length | C ++ Program to implement the above approach ; Function to count the number of substrings with sum equal to length ; Stores the count of substrings ; Add character to sum ; Add count of substrings to result ; Increase count of subarrays ; Return count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string s , int n ) { int count = 0 , sum = 0 ; unordered_map < int , int > mp ; mp [ 0 ] ++ ; for ( int i = 0 ; i < n ; ++ i ) { sum += ( s [ i ] - '0' ) ; count += mp [ sum - ( i + 1 ) ] ; ++ mp [ sum - ( i + 1 ) ] ; } return count ; } int main ( ) { string str = "112112" ; int n = str . length ( ) ; cout << countSubstrings ( str , n ) << endl ; return 0 ; }
Count of ways to split an Array into three contiguous Subarrays having increasing Sum | C ++ program to implement the above approach ; Function to count the number of ways to split array into three contiguous subarrays of the required type ; Stores the prefix sums ; Stores the suffix sums ; Traverse the given array ; Updating curr_subarray_sum until it is less than prefix_sum [ s - 1 ] ; Increase count ; Decrease curr_subarray_sum by arr [ s [ ] ; Return count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int prefix_sum [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int suffix_sum [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int s = 1 , e = 1 ; int curr_subarray_sum = 0 , count = 0 ; while ( s < n - 1 && e < n - 1 ) { while ( e < n - 1 && curr_subarray_sum < prefix_sum [ s - 1 ] ) { curr_subarray_sum += arr [ e ++ ] ; } if ( curr_subarray_sum <= suffix_sum [ e ] ) { count ++ ; } curr_subarray_sum -= arr [ s ++ ] ; } return count ; } int32_t main ( ) { int arr [ ] = { 2 , 3 , 1 , 7 } ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << ( findCount ( arr , n ) ) ; }
Minimum number of operations required to maximize the Binary String | C ++ Program to implement the above approach ; Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s [ i ] == '0' ) cnt0 ++ ; } return cnt0 ; } int main ( ) { int n = 8 ; string s = "01001011" ; int ans = minOperation ( s , n ) ; cout << ans << endl ; }
Minimum number of operations required to maximize the Binary String | C ++ Program to implement the above approach ; Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s [ i ] == '0' && s [ j ] == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s [ i ] == '1' ) { i ++ ; } if ( s [ j ] == '0' ) { j -- ; } } return ans ; } int main ( ) { int n = 8 ; string s = "10100101" ; int ans = minOperation ( s , n ) ; cout << ans << endl ; }
Count of N digit Numbers having no pair of equal consecutive Digits | C ++ Program to implement the above approach ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp [ ] [ ] table ; Calculate the count of required N - digit numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count ( int N ) { if ( N == 1 ) { cout << ( 10 ) << endl ; return ; } int dp [ N ] [ 10 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i < 10 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 ] [ j ] ; for ( int j = 0 ; j < 10 ; j ++ ) dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; } int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 ] [ i ] ; cout << ans << endl ; } int main ( ) { int N = 2 ; count ( N ) ; return 0 ; }
Count of N digit Numbers having no pair of equal consecutive Digits | C ++ program to implement the above approach ; Iterative Function to calculate ( x ^ y ) % mod in O ( log y ) ; Initialize result ; Update x if x >= mod ; If x is divisible by mod ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y , int mod ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } void count ( int N ) { if ( N == 1 ) { cout << 10 << endl ; return ; } cout << ( power ( 9 , N , 1000000007 ) ) << endl ; } int main ( ) { int N = 3 ; count ( N ) ; return 0 ; }
Smallest String consisting of a String S exactly K times as a Substring | C ++ Program to implement the above approach ; KMP algorithm ; Function to return the required string ; Finding the longest proper prefix which is also suffix ; ans string ; Update ans appending the substring K - 1 times ; Append the original string ; Returning min length string which contain exactly k substring of given string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * kmp ( string & s ) { int n = s . size ( ) ; int * lps = new int [ n ] ; lps [ 0 ] = 0 ; int i = 1 , len = 0 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } string findString ( string & s , int k ) { int n = s . length ( ) ; int * lps = kmp ( s ) ; string ans = " " ; string suff = s . substr ( 0 , n - lps [ n - 1 ] ) ; for ( int i = 0 ; i < k - 1 ; ++ i ) { ans += suff ; } ans += s ; return ans ; } int main ( ) { int k = 3 ; string s = " geeksforgeeks " ; cout << findString ( s , k ) << endl ; }
Check if an Array can be Sorted by picking only the corner Array elements | C ++ Program to implement the above approach ; Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int n ) { int i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 10 , 4 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; if ( check ( arr , n ) == true ) cout << " Yes " " STRNEWLINE " ; else cout << " No " << " STRNEWLINE " ; return 0 ; }
Queries to find the count of connected Non | C ++ program to implement the above approach ; Count of connected cells ; Function to return the representative of the Set to which x belongs ; If x is parent of itself ; x is representative of the Set ; Otherwise ; Path Compression ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; If both are in the same set ; Decrement count ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank ; Otherwise ; Then move x under y ( doesn 't matter which one goes where) ; And increment the result tree 's  rank by 1 ; Function to count the number of connected cells in the matrix ; Store result for queries ; Store representative of each element ; Initially , all elements are in their own set ; Stores the rank ( depth ) of each node ; If the grid [ x * m + y ] is already set , store the result ; Set grid [ x * m + y ] to 1 ; Increment count . ; Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1 ; Store result . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ctr = 0 ; int find ( vector < int > & parent , int x ) { if ( parent [ x ] == x ) return x ; parent [ x ] = find ( parent , parent [ x ] ) ; return parent [ x ] ; } void setUnion ( vector < int > & parent , vector < int > & rank , int x , int y ) { int parentx = find ( parent , x ) ; int parenty = find ( parent , y ) ; if ( parenty == parentx ) return ; ctr -- ; if ( rank [ parentx ] < rank [ parenty ] ) { parent [ parentx ] = parenty ; } else if ( rank [ parentx ] > rank [ parenty ] ) { parent [ parenty ] = parentx ; } else { parent [ parentx ] = parenty ; rank [ parenty ] ++ ; } } vector < int > solve ( int n , int m , vector < pair < int , int > > & query ) { vector < int > result ( query . size ( ) ) ; vector < int > parent ( n * m ) ; for ( int i = 0 ; i < n * m ; i ++ ) parent [ i ] = i ; vector < int > rank ( n * m , 1 ) ; vector < bool > grid ( n * m , 0 ) ; for ( int i = 0 ; i < query . size ( ) ; i ++ ) { int x = query [ i ] . first ; int y = query [ i ] . second ; if ( grid [ m * x + y ] == 1 ) { result [ i ] = ctr ; continue ; } grid [ m * x + y ] = 1 ; ctr ++ ; if ( x > 0 and grid [ m * ( x - 1 ) + y ] == 1 ) setUnion ( parent , rank , m * x + y , m * ( x - 1 ) + y ) ; if ( y > 0 and grid [ m * ( x ) + y - 1 ] == 1 ) setUnion ( parent , rank , m * x + y , m * ( x ) + y - 1 ) ; if ( x < n - 1 and grid [ m * ( x + 1 ) + y ] == 1 ) setUnion ( parent , rank , m * x + y , m * ( x + 1 ) + y ) ; if ( y < m - 1 and grid [ m * ( x ) + y + 1 ] == 1 ) setUnion ( parent , rank , m * x + y , m * ( x ) + y + 1 ) ; result [ i ] = ctr ; } return result ; } int main ( ) { int N = 3 , M = 3 , K = 4 ; vector < pair < int , int > > query = { { 0 , 0 } , { 1 , 1 } , { 1 , 0 } , { 1 , 2 } } ; vector < int > result = solve ( N , M , query ) ; for ( int i = 0 ; i < K ; i ++ ) cout << result [ i ] << " ▁ " ; }
Minimum Sum of a pair at least K distance apart from an Array | C ++ Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinSum ( int A [ ] , int K , int len ) { int n = len ; int suffix_min [ n ] = { 0 } ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = min ( suffix_min [ i + 1 ] , A [ i ] ) ; int min_sum = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } cout << min_sum ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 2 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findMinSum ( A , K , n ) ; return 0 ; }
Maximum number of envelopes that can be put inside other bigger envelopes | C ++ program for the above approach ; Function that returns the maximum number of envelopes that can be inserted into another envelopes ; Number of envelopes ; Sort the envelopes in non - decreasing order ; Initialize dp [ ] array ; To store the result ; Loop through the array ; Find envelopes count for each envelope ; Store maximum envelopes count ; Return the result ; Driver Code ; Given the envelopes ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEnvelopes ( vector < vector < int > > envelopes ) { int N = envelopes . size ( ) ; if ( N == 0 ) return N ; sort ( envelopes . begin ( ) , envelopes . end ( ) ) ; int dp [ N ] ; int max_envelope = 1 ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; ++ j ) { if ( envelopes [ i ] [ 0 ] > envelopes [ j ] [ 0 ] && envelopes [ i ] [ 1 ] > envelopes [ j ] [ 1 ] && dp [ i ] < dp [ j ] + 1 ) dp [ i ] = dp [ j ] + 1 ; } max_envelope = max ( max_envelope , dp [ i ] ) ; } return max_envelope ; } int main ( ) { vector < vector < int > > envelopes = { { 4 , 3 } , { 5 , 3 } , { 5 , 6 } , { 1 , 2 } } ; cout << maxEnvelopes ( envelopes ) ; return 0 ; }