codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange array to maximize count of local minima | C ++ program to implement the above approach ; Function to rearrange array elements to maximize count of local minima in the array ; Sort the array in ascending order ; Stores index of left pointer ; Stores index of right pointer ; Traverse the array elements ; if right is less than N ; Print array element ; Update right ; Print array element ; Update left ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrangeArrMaxcntMinima ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int left = 0 ; int right = N / 2 ; while ( left < N / 2 right < N ) { if ( right < N ) { cout << arr [ right ] << " ▁ " ; right ++ ; } if ( left < N / 2 ) { cout << arr [ left ] << " ▁ " ; left ++ ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrangeArrMaxcntMinima ( arr , N ) ; return 0 ; }
Minimum jumps required to make a group of persons sit together | C ++ program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the string ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the final count ; Driver Code ; Given arrange of seats ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int MOD = 1e9 + 7 ; int minJumps ( string seats ) { vector < int > position ; int count = 0 ; int len = seats . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' x ' ) { position . push_back ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position [ med_index ] ; int ans = 0 ; for ( int i = 0 ; i < position . size ( ) ; i ++ ) { ans = ( ans % MOD + abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } int main ( ) { string S = " . . . . x . . xx . . . x . . " ; cout << minJumps ( S ) ; return 0 ; }
Count pairs whose product contains single distinct prime factor | C ++ program to implement the above approach ; Function to find a single distinct prime factor of N ; Stores distinct prime factors of N ; Calculate prime factor of N ; Calculate distinct prime factor ; Insert i into disPrimeFact ; Update N ; If N is not equal to 1 ; Insert N into disPrimeFact ; If N contains a single distinct prime factor ; Return single distinct prime factor of N ; If N contains more than one distinct prime factor ; Function to count pairs in the array whose product contains only single distinct prime factor ; Stores count of 1 s in the array ; mp [ i ] : Stores count of array elements whose distinct prime factor is only i ; Traverse the array arr [ ] ; If current element is 1 ; Store distinct prime factor of arr [ i ] ; If arr [ i ] contains more than one prime factor ; If arr [ i ] contains a single prime factor ; Stores the count of pairs whose product of elements contains only a single distinct prime factor ; Traverse the map mp [ ] ; Stores count of array elements whose prime factor is ( it . first ) ; Update res ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int singlePrimeFactor ( int N ) { unordered_set < int > disPrimeFact ; for ( int i = 2 ; i * i <= N ; ++ i ) { while ( N % i == 0 ) { disPrimeFact . insert ( i ) ; N /= i ; } } if ( N != 1 ) { disPrimeFact . insert ( N ) ; } if ( disPrimeFact . size ( ) == 1 ) { return * disPrimeFact . begin ( ) ; } return -1 ; } int cntsingleFactorPair ( int arr [ ] , int N ) { int countOf1 = 0 ; unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { countOf1 ++ ; continue ; } int factorValue = singlePrimeFactor ( arr [ i ] ) ; if ( factorValue == -1 ) { continue ; } else { mp [ factorValue ] ++ ; } } int res = 0 ; for ( auto it : mp ) { int X = it . second ; res += countOf1 * X + ( X * ( X - 1 ) ) / 2 ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntsingleFactorPair ( arr , N ) ; return 0 ; }
Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | C ++ program to implement the above approach ; Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; if str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MincntBothPalin ( string str1 , string str2 , int N ) { int i = 0 ; int j = N - 1 ; int cntOp = 0 ; while ( i < j ) { if ( str1 [ i ] == str1 [ j ] && str2 [ i ] != str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] == str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == str2 [ j ] && str2 [ i ] == str1 [ j ] ) { cntOp += 1 ; } else { cntOp += 2 ; } } i += 1 ; j -= 1 ; } return cntOp ; } int main ( ) { string str1 = " dbba " ; string str2 = " abcd " ; int N = str1 . length ( ) ; cout << MincntBothPalin ( str1 , str2 , N ) ; }
Find an integral solution of the non | C ++ program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long power ( long long x , long long N ) { long long res = 1 ; while ( N > 0 ) { if ( N & 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } void findValX_Y ( long long N ) { if ( N <= 1 ) { cout << -1 << endl ; return ; } int xMax ; xMax = log2 ( N ) ; int yMax ; yMax = ( log2 ( N ) / log2 ( 5.0 ) ) ; for ( long long i = 1 ; i <= xMax ; i ++ ) { for ( long long j = 1 ; j <= yMax ; j ++ ) { long long a = power ( 2 , i ) ; long long b = power ( 5 , j ) ; if ( a + b == N ) { cout << i << " ▁ " << j << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { long long N = 129 ; findValX_Y ( N ) ; return 0 ; }
Count pairs from two arrays with difference exceeding K | set 2 | C ++ program for the above approach ; Function to count pairs that satisfy the given conditions ; Stores the count of pairs ; If v1 [ ] is smaller than v2 [ ] ; Sort the array v1 [ ] ; Traverse the array v2 [ ] ; Returns the address of the first number which is >= v2 [ j ] - k ; Increase the count by all numbers less than v2 [ j ] - k ; Otherwise ; Sort the array v2 [ ] ; Traverse the array v1 [ ] ; Returns the address of the first number which is > v1 [ i ] + k ; Increase the count by all numbers greater than v1 [ i ] + k ; Return the total count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int v1 [ ] , int v2 [ ] , int n , int m , int k ) { int count = 0 ; if ( n <= m ) { sort ( v1 , v1 + n ) ; for ( int j = 0 ; j < m ; j ++ ) { int index = lower_bound ( v1 , v1 + n , v2 [ j ] - k ) - v1 ; count += index ; } } else { sort ( v2 , v2 + m ) ; for ( int i = 0 ; i < n ; i ++ ) { int index = upper_bound ( v2 , v2 + m , v1 [ i ] + k ) - v2 ; count += m - index ; } } return count ; } int main ( ) { int arr [ ] = { 5 , 9 , 1 , 8 } ; int brr [ ] = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = sizeof ( brr ) / sizeof ( brr [ 0 ] ) ; cout << countPairs ( arr , brr , N , M , K ) ; return 0 ; }
Print siblings of a given Node in N | C ++ program for the above approach ; Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Driver Code ; Stores root of the constructed tree ; Print siblings of Node X
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; vector < Node * > child ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; return temp ; } void Siblings ( Node * root , int value ) { int flag = 0 ; if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; q . pop ( ) ; for ( int i = 0 ; i < temp -> child . size ( ) ; i ++ ) { if ( temp -> child [ i ] -> key == value ) { flag = 1 ; for ( int j = 0 ; j < temp -> child . size ( ) ; j ++ ) { if ( value != temp -> child [ j ] -> key ) cout << temp -> child [ j ] -> key << " ▁ " ; } break ; } q . push ( temp -> child [ i ] ) ; } } if ( flag == 0 ) cout << " No ▁ siblings ! ! " ; } Node * constructTree ( ) { Node * root = newNode ( 10 ) ; ( root -> child ) . push_back ( newNode ( 20 ) ) ; ( root -> child ) . push_back ( newNode ( 30 ) ) ; ( root -> child ) . push_back ( newNode ( 40 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( newNode ( 50 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( newNode ( 60 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( newNode ( 70 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( newNode ( 80 ) ) ; ( root -> child [ 2 ] -> child ) . push_back ( newNode ( 90 ) ) ; ( root -> child [ 2 ] -> child ) . push_back ( newNode ( 100 ) ) ; ( root -> child [ 2 ] -> child ) . push_back ( newNode ( 110 ) ) ; return root ; } int main ( ) { Node * root = constructTree ( ) ; int X = 30 ; Siblings ( root , X ) ; return 0 ; }
Maximum sum subarray of size K with sum less than X | C ++ program for the above approach ; Function to calculate maximum sum among all subarrays of size K with the sum less than X ; Initialize sum_K to 0 ; Calculate sum of first K elements ; If sum_K is less than X ; Initialize MaxSum with sum_K ; Iterate over the array from ( K + 1 ) - th index ; Subtract the first element from the previous K elements and add the next element ; If sum_K is less than X ; Update the Max_Sum ; Driver Code ; Size of Array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSumSubarr ( int A [ ] , int N , int K , int X ) { int sum_K = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum_K += A [ i ] ; } int Max_Sum = 0 ; if ( sum_K < X ) { Max_Sum = sum_K ; } for ( int i = K ; i < N ; i ++ ) { sum_K -= ( A [ i - K ] - A [ i ] ) ; if ( sum_K < X ) { Max_Sum = max ( Max_Sum , sum_K ) ; } } cout << Max_Sum << endl ; } int main ( ) { int arr [ ] = { -5 , 8 , 7 , 2 , 10 , 1 , 20 , -4 , 6 , 9 } ; int K = 5 ; int X = 30 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSumSubarr ( arr , N , K , X ) ; return 0 ; }
Rearrange array to make sum of all subarrays starting from first index non | C ++ program for the above approach ; Function to rearrange the array such that sum of all elements of subarrays from the 1 st index is non - zero ; Initialize sum of subarrays ; Sum of all elements of array ; If sum is 0 , the required array could never be formed ; If sum is non zero , array might be formed ; Sort array in ascending order ; When current subarray sum becomes 0 replace it with the largest element ; Swap Operation ; If largest element is same as element to be replaced , then rearrangement impossible ; If b = 1 , then rearrangement is not possible . Hence check with reverse configuration ; Sort array in descending order ; When current subarray sum becomes 0 replace it with the smallest element ; Swap Operation ; If smallest element is same as element to be replaced , then rearrangement impossible ; If neither of the configurations worked then print " - 1" ; Otherwise , print the formed rearrangement ; Driver Code ; Given array ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrangeArray ( int a [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; } if ( sum == 0 ) { cout << " - 1" ; return ; } sum = 0 ; int b = 0 ; sort ( a , a + N ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ N - 1 ] ) { sum -= a [ i ] ; swap ( a [ i ] , a [ N - 1 ] ) ; sum += a [ i ] ; } else { b = 1 ; break ; } } } if ( b == 1 ) { b = 0 ; sum = 0 ; sort ( a , a + N , greater < int > ( ) ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) { sum += a [ i ] ; if ( sum == 0 ) { if ( a [ i ] != a [ 0 ] ) { sum -= a [ i ] ; swap ( a [ i ] , a [ 0 ] ) ; sum += a [ i ] ; } else { b = 1 ; break ; } } } } if ( b == 1 ) { cout << " - 1" ; return ; } for ( int i = 0 ; i < N ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , -1 , 2 , 4 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrangeArray ( arr , N ) ; return 0 ; }
Length of smallest subarray to be removed to make sum of remaining elements divisible by K | C ++ program for the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is divisible by K ; Stores the remainder of each arr [ i ] when divided by K ; Stores total sum of elements ; K has been added to each arr [ i ] to handle - ve integers ; Update the total sum ; Remainder when total_sum is divided by K ; If given array is already divisible by K ; Stores curr_remainder and the most recent index at which curr_remainder has occurred ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; If not possible ; Print the result ; Driver Code ; Given array arr [ ] ; Size of array ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeSmallestSubarray ( int arr [ ] , int n , int k ) { int mod_arr [ n ] ; int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mod_arr [ i ] = ( arr [ i ] + k ) % k ; total_sum += arr [ i ] ; } int target_remainder = total_sum % k ; if ( target_remainder == 0 ) { cout << "0" ; return ; } unordered_map < int , int > map1 ; map1 [ 0 ] = -1 ; int curr_remainder = 0 ; int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 [ curr_remainder ] = i ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . find ( mod ) != map1 . end ( ) ) res = min ( res , i - map1 [ mod ] ) ; } if ( res == INT_MAX res == n ) { res = -1 ; } cout << res ; } int main ( ) { int arr [ ] = { 3 , 1 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 6 ; removeSmallestSubarray ( arr , N , K ) ; return 0 ; }
Queries to flip characters of a binary string in given range | C ++ program to implement the above approach ; Function to find the binary string by performing all the given queries ; Stores length of the string ; prefixCnt [ i ] : Stores number of times str [ i ] toggled by performing all the queries ; Update prefixCnt [ Q [ i ] [ 0 ] ] ; Update prefixCnt [ Q [ i ] [ 1 ] + 1 ] ; Calculate prefix sum of prefixCnt [ i ] ; Traverse prefixCnt [ ] array ; If ith element toggled odd number of times ; Toggled i - th element of binary string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string toggleQuery ( string str , int Q [ ] [ 2 ] , int M ) { int N = str . length ( ) ; int prefixCnt [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < M ; i ++ ) { prefixCnt [ Q [ i ] [ 0 ] ] += 1 ; prefixCnt [ Q [ i ] [ 1 ] + 1 ] -= 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { prefixCnt [ i ] += prefixCnt [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixCnt [ i ] % 2 ) { str [ i ] = '1' - str [ i ] + '0' ; } } return str ; } int main ( ) { string str = "101010" ; int Q [ ] [ 2 ] = { { 0 , 1 } , { 2 , 5 } , { 2 , 3 } , { 1 , 4 } , { 0 , 5 } } ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; cout << toggleQuery ( str , Q , M ) ; }
Maximum length possible by cutting N given woods into at least K pieces | C ++ program to implement the above approach ; Function to check if it is possible to cut woods into K pieces of length len ; Stores count of pieces having length equal to K ; Traverse wood [ ] array ; Update count ; Function to find the maximum value of L ; Stores minimum possible of L ; Stores maximum possible value of L ; Apply binary search over the range [ left , right ] ; Stores mid value of left and right ; If it is possible to cut woods into K pieces having length of each piece equal to mid ; Update left ; Update right ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( int wood [ ] , int N , int len , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += wood [ i ] / len ; } return count >= K ; } int findMaxLen ( int wood [ ] , int N , int K ) { int left = 1 ; int right = * max_element ( wood , wood + N ) ; while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( isValid ( wood , N , mid , K ) ) { left = mid + 1 ; } else { right = mid - 1 ; } } return right ; } int main ( ) { int wood [ ] = { 5 , 9 , 7 } ; int N = sizeof ( wood ) / sizeof ( wood [ 0 ] ) ; int K = 4 ; cout << findMaxLen ( wood , N , K ) ; }
Count quadruplets with sum K from given array | C ++ program for the above approach ; Function to return the number of quadruplets with the given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second elements ; All possible third elements ; All possible fourth elements ; Increment counter by 1 if quadruplet sum is S ; Return the final count ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countSum ( int a [ ] , int n , int sum ) { int i , j , k , l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { for ( k = j + 1 ; k < n - 1 ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ i ] + a [ j ] + a [ k ] + a [ l ] == sum ) count ++ ; } } } } return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSum ( arr , N , S ) ; return 0 ; }
Minimum adjacent swaps to group similar characters together | C ++ program for the above approach ; Function to find minimum adjacent swaps required to make all the same character adjacent ; Initialize answer ; Create a 2D array of size 26 ; Traverse the string ; Get character ; Append the current index in the corresponding vector ; Traverse each character from a to z ; Add difference of adjacent index ; Return answer ; Driver Code ; Given string ; Size of string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( string S , int n ) { int swaps = 0 ; vector < vector < int > > arr ( 26 ) ; for ( int i = 0 ; i < n ; i ++ ) { int pos = S [ i ] - ' a ' ; arr [ pos ] . push_back ( i ) ; } for ( char ch = ' a ' ; ch <= ' z ' ; ++ ch ) { int pos = ch - ' a ' ; for ( int i = 1 ; i < arr [ pos ] . size ( ) ; ++ i ) { swaps += abs ( arr [ pos ] [ i ] - arr [ pos ] [ i - 1 ] - 1 ) ; } } return swaps ; } int main ( ) { string S = " abbccabbcc " ; int N = S . length ( ) ; cout << minSwaps ( S , N ) ; return 0 ; }
Construct a graph which does not contain any pair of adjacent nodes with same value | C ++ program for the above approach ; Function that prints the edges of the generated graph ; First print connections stored in store [ ] ; Check if there is more than one occurrence of 1 st unique element ; Print all other occurrence of 1 st unique element with second unique element ; Function to construct the graph such that the every adjacent nodes have different value ; Stores pair of edges formed ; Stores first unique occurrence ; Check for the second unique occurrence ; Store indices of 2 nd unique occurrence ; To check if arr has only 1 unique element or not ; Store the connections of all unique elements with Node 1 ; If value at node ( i + 1 ) is same as value at Node 1 then store its indices ; If count is zero then it 's not  possible to construct the graph ; If more than 1 unique element is present ; Print the edges ; Driver Code ; Given array having node values ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printConnections ( vector < pair < int , int > > store , vector < int > ind , int ind1 ) { for ( auto pr : store ) { cout << pr . first << " ▁ " << pr . second << " STRNEWLINE " ; } if ( ind . size ( ) != 0 ) { for ( auto x : ind ) { cout << ind1 << " ▁ " << x + 1 << " STRNEWLINE " ; } } } void constructGraph ( char arr [ ] , int N ) { vector < int > ind ; vector < pair < int , int > > store ; char x = arr [ 0 ] ; int count = 0 , ind1 ; for ( int i = 1 ; i <= N - 1 ; ++ i ) { if ( arr [ i ] != x ) { ind1 = i + 1 ; count ++ ; store . push_back ( { 1 , i + 1 } ) ; } else { ind . push_back ( i ) ; } } if ( count == 0 ) { cout << " Not ▁ Possible " ; } else { cout << " Possible " << " STRNEWLINE " ; printConnections ( store , ind , ind1 ) ; } } int main ( ) { int N = 5 ; char arr [ ] = { ' a ' , ' b ' , ' a ' , ' b ' , ' c ' } ; constructGraph ( arr , N ) ; return 0 ; }
Minimum substring flips required to convert given binary string to another | C ++ program for the above approach ; Function that finds the minimum number of operations required such that string A and B are the same ; Stores the count of steps ; Stores the last index whose bits are not same ; Iterate until both string are unequal ; Check till end of string to find rightmost unequals bit ; Update the last index ; Flipping characters up to the last index ; Flip the bit ; Increasing steps by one ; Print the count of steps ; Driver Code ; Given strings A and B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumOperations ( string a , string b ) { int step = 0 ; int last_index ; while ( a != b ) { for ( int i = 0 ; i < a . length ( ) ; i ++ ) { if ( a [ i ] != b [ i ] ) { last_index = i ; } } for ( int i = 0 ; i <= last_index ; i ++ ) { a [ i ] = ( a [ i ] == '0' ) ? '1' : '0' ; } step ++ ; } cout << step ; } int main ( ) { string A = "101010" , B = "110011" ; findMinimumOperations ( A , B ) ; return 0 ; }
Count all disjoint pairs having absolute difference at least K from a given array | C ++ program for the above approach ; Function to check if it is possible to form M pairs with abs diff at least K ; Traverse the array over [ 0 , M ] ; If valid index ; Return 1 ; Function to count distinct pairs with absolute difference atleasr K ; Stores the count of all possible pairs ; Initialize left and right ; Sort the array ; Perform Binary Search ; Find the value of mid ; Check valid index ; Update ans ; Print the answer ; Driver Code ; Given array arr [ ] ; Given difference K ; Size of the array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( int arr [ ] , int n , int m , int d ) { for ( int i = 0 ; i < m ; i ++ ) { if ( abs ( arr [ n - m + i ] - arr [ i ] ) < d ) { return 0 ; } } return 1 ; } int countPairs ( int arr [ ] , int N , int K ) { int ans = 0 ; int left = 0 , right = N / 2 + 1 ; sort ( arr , arr + N ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isValid ( arr , N , mid , K ) ) { ans = mid ; left = mid + 1 ; } else right = mid - 1 ; } cout << ans << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 5 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N , K ) ; return 0 ; }