codeparrot/xlcost-text-to-code huggingface dataset:
Length of smallest substring of a given string which contains another string as subsequence | C ++ program to implement the above approach ; Function to find the length of smallest substring of a having string b as a subsequence ; Stores the characters present in string b ; Find index of characters of a that are also present in string b ; If character is present in string b ; Store the index of character ; Flag is used to check if substring is possible ; Assume that substring is possible ; Stores first and last indices of the substring respectively ; For first character of string b ; If the first character of b is not present in a ; If the first character of b is present in a ; Remove the index from map ; Update indices of the substring ; For the remaining characters of b ; If index possible for current character ; If no index is possible ; If no more substring is possible ; Update the minimum length of substring ; Return the result ; Driver Code ; Given two string
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( string a , string b ) { map < char , int > Char ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { Char [ b [ i ] ] ++ ; } map < char , vector < int > > CharacterIndex ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { char x = a [ i ] ; if ( Char . find ( x ) != Char . end ( ) ) { CharacterIndex [ x ] . push_back ( i ) ; } } int len = INT_MAX ; int flag ; while ( true ) { flag = 1 ; int firstVar , lastVar ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { if ( i == 0 ) { if ( CharacterIndex . find ( b [ i ] ) == CharacterIndex . end ( ) ) { flag = 0 ; break ; } else { int x = * ( CharacterIndex [ b [ i ] ] . begin ( ) ) ; CharacterIndex [ b [ i ] ] . erase ( CharacterIndex [ b [ i ] ] . begin ( ) ) ; firstVar = x ; lastVar = x ; } } else { int elementFound = 0 ; for ( auto e : CharacterIndex [ b [ i ] ] ) { if ( e > lastVar ) { elementFound = 1 ; lastVar = e ; break ; } } if ( elementFound == 0 ) { flag = 0 ; break ; } } } if ( flag == 0 ) { break ; } len = min ( len , abs ( lastVar - firstVar ) + 1 ) ; } return len ; } int main ( ) { string a = " abcdefababaef " ; string b = " abf " ; int len = minLength ( a , b ) ; if ( len != INT_MAX ) { cout << len << endl ; } else { cout << " Impossible " << endl ; } }
Count smaller primes on the right of each array element | C ++ Program for the above approach ; Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; bool is_prime ( int n ) { if ( n <= 1 ) return 0 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return 0 ; } return 1 ; } void countSmallerPrimes ( int ar [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } cout << count << " ▁ " ; } } int main ( ) { int ar [ ] = { 43 , 3 , 5 , 7 , 2 , 41 } ; int N = sizeof ar / sizeof ar [ 0 ] ; countSmallerPrimes ( ar , N ) ; return 0 ; }
Check if a string is concatenation of another given string | C ++ program to implement the above approach ; Function to check if a string is concatenation of another string ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the strings ; If str1 is not concatenation of str2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkConcat ( string str1 , string str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 [ i ] != str2 [ i % M ] ) { return false ; } } return true ; } int main ( ) { string str1 = " abcabcabc " ; string str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { cout << " Yes " ; } else { cout << " No " ; } }
Minimize length of string by replacing K pairs of distinct adjacent characters | C ++ program to implement the above approach ; Function to minimize the length of the string by replacing distinct pairs of adjacent characters ; Stores the length of the given string ; Stores the index of the given string ; Traverse the given string ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinLen ( string str , int K ) { int N = str . length ( ) ; int i = 0 ; while ( i < N - 1 ) { if ( str [ i ] != str [ i + 1 ] ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return max ( 1 , N - K ) ; } int main ( ) { string str = " aabc " ; int K = 1 ; cout << MinLen ( str , K ) ; }
Length of smallest sequence having sum X and product Y | C ++ program for the above approach ; Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define pb  push_back NEW_LINE double temp ( int n , int x ) { return pow ( x * 1.0 / n , n ) ; } bool check ( int n , int y , int x ) { double v = temp ( n , x ) ; return ( v >= y ) ; } void find ( int x , int y ) { int high = ( int ) floor ( x / exp ( 1.0 ) ) ; int low = 1 ; if ( x == y ) cout << 1 << endl ; else if ( ! check ( high , y , x ) ) cout << -1 << endl ; else { while ( high - low > 1 ) { int mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } cout << high << endl ; } } int main ( ) { int x = 9 , y = 8 ; find ( x , y ) ; return 0 ; }
Print the middle nodes of each level of a Binary Tree | C ++ program for the above approach ; Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Driver Code ; Given Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; struct node * newnode ( int d ) { struct node * temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> data = d ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } void dfs ( node * root , int l , map < int , vector < int > > & M ) { if ( root == NULL ) return ; M [ l ] . push_back ( root -> data ) ; dfs ( root -> left , l + 1 , M ) ; dfs ( root -> right , l + 1 , M ) ; } void printMidNodes ( node * root ) { map < int , vector < int > > M ; dfs ( root , 0 , M ) ; for ( auto & it : M ) { int size = it . second . size ( ) ; if ( size & 1 ) { cout << it . second [ ( size - 1 ) / 2 ] << endl ; } else { cout << it . second [ ( size - 1 ) / 2 ] << ' ▁ ' << it . second [ ( size - 1 ) / 2 + 1 ] << endl ; } } } int main ( ) { struct node * root = newnode ( 1 ) ; root -> left = newnode ( 2 ) ; root -> right = newnode ( 3 ) ; root -> left -> left = newnode ( 4 ) ; root -> left -> right = newnode ( 5 ) ; root -> left -> right -> left = newnode ( 11 ) ; root -> left -> right -> right = newnode ( 6 ) ; root -> left -> right -> right -> left = newnode ( 7 ) ; root -> left -> right -> right -> right = newnode ( 9 ) ; root -> right -> left = newnode ( 10 ) ; root -> right -> right = newnode ( 8 ) ; printMidNodes ( root ) ; return 0 ; }
Smallest array that can be obtained by replacing adjacent pairs with their products | C ++ implementation of the above approach ; Function to minimize the size of array by performing given operations ; If all array elements are not same ; If all array elements are same ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return 1 ; } } return N ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minLength ( arr , N ) ; return 0 ; }
Maximum XOR of Two Numbers in an Array | Set 2 | C ++ program for the above approach ; Structure of Trie ; Function to insert binary representation of element x in the Trie ; Store the head ; Find the i - th bit ; If curr -> left is NULL ; Update curr to curr -> left ; If curr -> right is NULL ; Update curr to curr -> right ; Function that finds the maximum Bitwise XOR value for all such pairs ; head Node of Trie ; Insert each element in trie ; Stores the maximum XOR value ; Traverse the given array ; Stores the XOR with current value arr [ i ] ; Finding ith bit ; Check if the bit is 0 ; If right node exists ; Update the currentXOR ; Check if left node exists ; Update the currentXOR ; Update M to M / 2 for next set bit ; Update the maximum XOR ; Return the maximum XOR found ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : node * left ; node * right ; } ; void insert ( int x , node * head ) { node * curr = head ; for ( int i = 30 ; i >= 0 ; i -- ) { int val = ( x >> i ) & 1 ; if ( val == 0 ) { if ( ! curr -> left ) curr -> left = new node ( ) ; curr = curr -> left ; } else { if ( ! curr -> right ) curr -> right = new node ( ) ; curr = curr -> right ; } } } int findMaximumXOR ( int arr [ ] , int n ) { node * head = new node ( ) ; for ( int i = 0 ; i < n ; i ++ ) { insert ( arr [ i ] , head ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; int M = pow ( 2 , 30 ) ; node * curr = head ; for ( int j = 30 ; j >= 0 ; j -- ) { int val = ( arr [ i ] >> j ) & 1 ; if ( val == 0 ) { if ( curr -> right ) { curr_xor += M ; curr = curr -> right ; } else { curr = curr -> left ; } } else { if ( curr -> left ) { curr_xor += M ; curr = curr -> left ; } else { curr = curr -> right ; } } M /= 2 ; } ans = max ( ans , curr_xor ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaximumXOR ( arr , N ) ; return 0 ; }
Subarrays whose sum is a perfect square | C ++ program to implement the above approach ; Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintIndexes ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int currSubSum = 0 ; for ( int j = i ; j < N ; j ++ ) { currSubSum += arr [ j ] ; int sq = sqrt ( currSubSum ) ; if ( sq * sq == currSubSum ) { cout << " ( " << i << " , ▁ " << j << " ) ▁ " ; } } } } int main ( ) { int arr [ ] = { 65 , 79 , 81 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; PrintIndexes ( arr , N ) ; }
Count positions in Binary Matrix having equal count of set bits in corresponding row and column | C ++ 14 program to implement above approach ; Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPosition ( vector < vector < int > > mat ) { int n = mat . size ( ) ; int m = mat [ 0 ] . size ( ) ; vector < int > row ( n ) ; vector < int > col ( m ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 } , { 1 , 1 } } ; cout << ( countPosition ( mat ) ) ; }
Maximum value at each level in an N | C ++ program for the above approach ; Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Driver Code ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAtLevel ( int N , int M , vector < int > Value , int Edges [ ] [ 2 ] ) { vector < int > adj [ N ] ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . push_back ( v ) ; } queue < int > q ; q . push ( 0 ) ; while ( ! q . empty ( ) ) { int count = q . size ( ) ; int maxVal = 0 ; while ( count -- ) { int temp = q . front ( ) ; q . pop ( ) ; maxVal = max ( maxVal , Value [ temp ] ) ; for ( int i = 0 ; i < adj [ temp ] . size ( ) ; i ++ ) { q . push ( adj [ temp ] [ i ] ) ; } } cout << maxVal << " ▁ " ; } } int main ( ) { int N = 10 ; int Edges [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; vector < int > Value = { 1 , 2 , -1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAtLevel ( N , N - 1 , Value , Edges ) ; return 0 ; }
Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | C ++ program to implement the above approach ; Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ctTriplets ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; vector < int > temp ( N ) ; int index = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } for ( int i = 0 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } int ct = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && i + 1 < N ) { if ( temp [ i ] < temp [ i + 1 ] && temp [ i ] < temp [ i - 1 ] ) { ct ++ ; } } } cout << " Count ▁ of ▁ triplets : " << ct << endl ; cout << " Array : " ; for ( int i = 0 ; i < N ; i ++ ) { cout << temp [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ctTriplets ( arr , N ) ; }
Rearrange array to make it non | C ++ program for above approach ; Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { int b [ n ] ; int minElement = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = min ( minElement , a [ i ] ) ; } sort ( b , b + n ) ; int k = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } int main ( ) { int a [ ] = { 4 , 3 , 6 , 6 , 2 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( check ( a , n ) == true ) cout << " Yes ▁ STRNEWLINE " ; else cout << " No ▁ STRNEWLINE " ; return 0 ; }
Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | C ++ program for the above approach ; Function to count required 1 s from the given matrix ; Stores the dimensions of the mat [ ] [ ] ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the final count ; Driver Code ; Given matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numSpecial ( vector < vector < int > > & mat ) { int m = mat . size ( ) , n = mat [ 0 ] . size ( ) ; int rows [ m ] ; int cols [ n ] ; int i , j ; for ( i = 0 ; i < m ; i ++ ) { rows [ i ] = 0 ; for ( j = 0 ; j < n ; j ++ ) rows [ i ] += mat [ i ] [ j ] ; } for ( i = 0 ; i < n ; i ++ ) { cols [ i ] = 0 ; for ( j = 0 ; j < m ; j ++ ) cols [ i ] += mat [ j ] [ i ] ; } int cnt = 0 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 1 && rows [ i ] == 1 && cols [ j ] == 1 ) cnt ++ ; } } return cnt ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ; cout << numSpecial ( mat ) << endl ; return 0 ; }
Find all matrix elements which are minimum in their row and maximum in their column | C ++ program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver Code ; Function call ; If no such matrix element is found
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > minmaxNumbers ( vector < vector < int > > & matrix , vector < int > & res ) { unordered_set < int > set ; for ( int i = 0 ; i < matrix . size ( ) ; i ++ ) { int minr = INT_MAX ; for ( int j = 0 ; j < matrix [ i ] . size ( ) ; j ++ ) { minr = min ( minr , matrix [ i ] [ j ] ) ; } set . insert ( minr ) ; } for ( int j = 0 ; j < matrix [ 0 ] . size ( ) ; j ++ ) { int maxc = INT_MIN ; for ( int i = 0 ; i < matrix . size ( ) ; i ++ ) { maxc = max ( maxc , matrix [ i ] [ j ] ) ; } if ( set . find ( maxc ) != set . end ( ) ) { res . push_back ( maxc ) ; } } return res ; } int main ( ) { vector < vector < int > > mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; vector < int > ans ; minmaxNumbers ( mat , ans ) ; if ( ans . size ( ) == 0 ) cout << " - 1" << endl ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << endl ; return 0 ; }
Node whose removal minimizes the maximum size forest from an N | C ++ program to implement the above approach ; Function to create the graph ; Function to traverse the graph and find the minimum of maximum size forest after removing a node ; Traversing every child subtree except the parent node ; Traverse all subtrees ; Update the maximum size of forests ; Update the minimum of maximum size of forests obtained ; Condition to find the minimum of maximum size forest ; Update and store the corresponding node ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mini = 105 , ans , n ; vector < vector < int > > g ( 100 ) ; int size [ 100 ] ; void create_graph ( ) { g [ 1 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 3 ) ; g [ 3 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 4 ) ; g [ 4 ] . push_back ( 1 ) ; g [ 2 ] . push_back ( 5 ) ; g [ 5 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 6 ) ; g [ 6 ] . push_back ( 2 ) ; } void dfs ( int node , int parent ) { size [ node ] = 1 ; int mx = 0 ; for ( int y : g [ node ] ) { if ( y == parent ) continue ; dfs ( y , node ) ; size [ node ] += size [ y ] ; mx = max ( mx , size [ y ] ) ; } mx = max ( mx , n - size [ node ] ) ; if ( mx < mini ) { mini = mx ; ans = node ; } } int main ( ) { n = 6 ; create_graph ( ) ; dfs ( 1 , -1 ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Minimum non | C ++ program for the above approach ; Function to find minimum flips required to remove all 0 s from a given binary string ; Length of given string ; Stores the indices of previous 0 s ; Stores the minimum operations ; Traverse string to find minimum operations to obtain required string ; Current character ; If current character is '0' ; Update temp1 with current index , if both temp variables are empty ; Update temp2 with current index , if temp1 contains prev index but temp2 is empty ; If temp1 is not empty ; Reset temp1 to - 1 ; Increase ans ; If temp2 is not empty but temp1 is empty ; Reset temp2 to - 1 ; Increase ans ; If both temp variables are not empty ; Otherwise ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string s ) { int n = s . length ( ) ; int temp1 = -1 , temp2 = -1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s [ i ] ; if ( curr == '0' ) { if ( temp1 == -1 && temp2 == -1 ) { temp1 = i ; } else if ( temp1 != -1 && temp2 == -1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != -1 ) { temp1 = -1 ; ans ++ ; } else if ( temp1 == -1 && temp2 != -1 && i - temp2 != 1 ) { temp2 = -1 ; ans ++ ; } } } if ( temp1 != -1 && temp2 != -1 ) { ans += 2 ; } else if ( temp1 != -1 temp2 != -1 ) { ans += 1 ; } return ans ; } int main ( ) { string s = "110010" ; cout << ( minOperation ( s ) ) ; }
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | C ++ program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver Code ; Given arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinLength ( vector < int > & arr ) { int index = ( int ) arr . size ( ) - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } int main ( ) { vector < int > arr = { 7 , 8 , 5 , 0 , -1 , -1 , 0 , 1 , 2 , 3 , 4 } ; cout << findMinLength ( arr ) ; return 0 ; }
Count array elements exceeding sum of preceding K elements | C ++ program for the above approach ; Function to count array elements exceeding sum of preceding K elements ; Iterate over the array ; Update prefix sum ; Check if arr [ K ] > arr [ 0 ] + . . + arr [ K - 1 ] ; Increment count ; Check if arr [ i ] > arr [ i - K - 1 ] + . . + arr [ i - 1 ] ; Increment count ; Driver Code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPrecedingK ( int a [ ] , int n , int K ) { int prefix [ n ] ; prefix [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + a [ i ] ; } int ctr = 0 ; if ( prefix [ K - 1 ] < a [ K ] ) ctr ++ ; for ( int i = K + 1 ; i < n ; i ++ ) { if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) ctr ++ ; } return ctr ; } int main ( ) { int arr [ ] = { 2 , 3 , 8 , 10 , -2 , 7 , 5 , 5 , 9 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << countPrecedingK ( arr , N , K ) ; return 0 ; }
Count minimum moves required to convert A to B | C ++ program for the above approach ; Function to find minimum number of moves to obtained B from A ; Stores the minimum number of moves ; Absolute difference ; K is in range [ 0 , 10 ] ; Print the required moves ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convertBfromA ( int a , int b ) { int moves = 0 ; int x = abs ( a - b ) ; for ( int i = 10 ; i > 0 ; i -- ) { moves += x / i ; x = x % i ; } cout << moves << " ▁ " ; } int main ( ) { int A = 188 , B = 4 ; convertBfromA ( A , B ) ; return 0 ; }