codeparrot/xlcost-text-to-code huggingface dataset:
Permutation of Array such that sum of adjacent elements are not divisible by 3 | C ++ implementation to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Function to segregate numbers based on their remainder when divided by three ; Loop to iterate over the elements of the given array ; Condition to check the remainder of the number ; Function to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Condition to check when it 's impossible to arrange ; Condition to check when there are no zeros , and only ones or only twos ; Array to store the permutation ; Place the ones on alternate places in the answer array , leaving spaces for zeros remainder elements in the array ; Adding a zero to connect it with a two ; Place the twos on alternate places in the answer array , leaving spaces for zeros ; Fill the zeros finally , between the ones and the twos ; Print the arrangement of the array ; Function to solve the problem ; As there can be only 3 remainders ; Function Call ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define hell  1000000007 NEW_LINE #define N  100005 NEW_LINE void count_k ( vector < int > & arr , int & c_0 , int & c_1 , int & c_2 , stack < int > & ones , stack < int > & twos , stack < int > & zeros ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] % 3 == 0 ) { c_0 ++ ; zeros . push ( arr [ i ] ) ; } else if ( arr [ i ] % 3 == 1 ) { c_1 ++ ; ones . push ( arr [ i ] ) ; } else { c_2 ++ ; twos . push ( arr [ i ] ) ; } } return ; } void printArrangement ( vector < int > & arr , int & c_0 , int & c_1 , int & c_2 , stack < int > & ones , stack < int > & twos , stack < int > & zeros ) { if ( ( c_0 == 0 && c_1 != 0 && c_2 != 0 ) or c_0 > c_1 + c_2 + 1 ) { cout << " - 1" ; return ; } if ( c_0 == 0 ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { cout << arr [ i ] << " ▁ " ; } return ; } int i , j , ans [ N ] ; memset ( ans , -1 , sizeof ( ans ) ) ; for ( i = 1 , j = 0 ; j < c_1 ; i += 2 , j ++ ) { ans [ i ] = ones . top ( ) ; ones . pop ( ) ; } ans [ i - 1 ] = zeros . top ( ) ; zeros . pop ( ) ; c_0 -- ; for ( j = 0 ; j < c_2 ; j ++ , i += 2 ) { ans [ i ] = twos . top ( ) ; twos . pop ( ) ; } for ( int k = 0 ; c_0 > 0 ; k += 2 ) { if ( ans [ k ] == -1 ) { ans [ k ] = zeros . top ( ) ; c_0 -- ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( ans [ i ] != -1 ) cout << ans [ i ] << " ▁ " ; } return ; } void solve ( int n , vector < int > & arr ) { stack < int > ones , zeros , twos ; int c_0 = 0 , c_1 = 0 , c_2 = 0 ; count_k ( arr , c_0 , c_1 , c_2 , ones , twos , zeros ) ; printArrangement ( arr , c_0 , c_1 , c_2 , ones , twos , zeros ) ; } signed main ( ) { int n = 5 ; vector < int > arr { 1 , 2 , 3 , 4 , 5 } ; solve ( n , arr ) ; return 0 ; }
Count of unique digits in a given number N | C ++ program for the above approach ; Function that returns the count of unique digits of the given number ; Initialize a variable to store count of unique digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is 1 ; Increment the count of unique digits ; Return the count / of unique digit ; Driver Code ; Given array arr [ ] ; Function Call
#include <iostream> NEW_LINE using namespace std ; int countUniqueDigits ( int N ) { int res = 0 ; int cnt [ 10 ] = { 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] == 1 ) { res ++ ; } } return res ; } int main ( ) { int N = 2234262 ; cout << countUniqueDigits ( N ) ; return 0 ; }
Check if frequency of each element in given array is unique or not | C ++ code for the above approach ; Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Driver Code ; Given array arr [ ] ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkUniqueFrequency ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } unordered_set < int > uniqueFreq ; for ( auto & i : freq ) { if ( uniqueFreq . count ( i . second ) ) return false ; else uniqueFreq . insert ( i . second ) ; } return true ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 5 , 5 } ; int n = sizeof arr / sizeof arr [ 0 ] ; bool res = checkUniqueFrequency ( arr , n ) ; if ( res ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Sum of first N natural numbers with all powers of 2 added twice | C ++ program to implement the above approach ; Function to raise N to the power P and return the value ; Function to calculate the log base 2 of an integer ; Calculate log2 ( N ) indirectly using log ( ) method ; Function to calculate and return the required sum ; Sum of first N natural numbers ; Sum of all powers of 2 up to N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double power ( int N , int P ) { return pow ( N , P ) ; } int Log2 ( int N ) { int result = ( int ) ( log ( N ) / log ( 2 ) ) ; return result ; } double specialSum ( int n ) { double sum = n * ( n + 1 ) / 2 ; int a = Log2 ( n ) ; sum = sum + power ( 2 , a + 1 ) - 1 ; return sum ; } int main ( ) { int n = 4 ; cout << ( specialSum ( n ) ) << endl ; return 0 ; }
Rearrange an array such that product of every two consecutive elements is a multiple of 4 | C ++ Program to rearray array elements such that the product of every two consecutive elements is a multiple of 4 ; Function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; If element is odd ; Odd ; If element is divisible by 4 ; Divisible by 4 ; If element is not divisible by 4 ; Even but not divisible by 4 ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Print the NON_FOUR [ i ] elements at the end ; No possible configuration ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Permute ( vector < int > & arr , int n ) { int odd = 0 , four = 0 ; int non_four = 0 ; vector < int > ODD , FOUR , NON_FOUR ; for ( auto x : arr ) { if ( x & 1 ) { odd ++ ; ODD . push_back ( x ) ; } else if ( x % 4 == 0 ) { four ++ ; FOUR . push_back ( x ) ; } else { non_four ++ ; NON_FOUR . push_back ( x ) ; } } if ( non_four == 0 && four >= odd - 1 ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { cout << ODD [ i ] << " ▁ " ; if ( i < y ) cout << FOUR [ i ] << " ▁ " ; } while ( i < y ) cout << FOUR [ i ] << " ▁ " ; cout << endl ; } else if ( non_four > 0 and four >= odd ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { cout << ODD [ i ] << " ▁ " ; if ( i < y ) cout << FOUR [ i ] << " ▁ " ; } while ( i < y ) cout << FOUR [ i ] << " ▁ " ; for ( int j = 0 ; j < ( int ) NON_FOUR . size ( ) ; j ++ ) cout << NON_FOUR [ j ] << " ▁ " ; cout << endl ; } else cout << " Not ▁ Possible " << endl ; } signed main ( ) { vector < int > arr = { 2 , 7 , 1 , 8 , 2 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr ) ; Permute ( arr , N ) ; return 0 ; }
Construct the smallest possible Array with given Sum and XOR | C ++ program for the above approach ; Function to find array ; array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Driver Code ; Given sum and value of Bitwise XOR ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArray ( int sum , int xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { cout << " No ▁ Array ▁ Possible STRNEWLINE " ; return ; } if ( sum == xorr ) { if ( sum == 0 ) cout << " Array ▁ is ▁ empty " << " ▁ with ▁ size ▁ 0 STRNEWLINE " ; else cout << " Array ▁ size ▁ is ▁ " << 1 << " Array is " << sum << " STRNEWLINE " ; return ; } int mid = ( sum - xorr ) / 2 ; if ( xorr & mid == 1 ) { cout << " Array ▁ size ▁ is ▁ " << 3 << " STRNEWLINE " ; cout << " Array ▁ is ▁ " << xorr << " ▁ " << mid << " ▁ " << mid << " STRNEWLINE " ; } else { cout << " Array ▁ size ▁ is ▁ " << 2 << " STRNEWLINE " ; cout << " Array ▁ is ▁ " << ( xorr + mid ) << " ▁ " << mid << " STRNEWLINE " ; } } int main ( ) { int sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ; cout << " STRNEWLINE " ; return 0 ; }
Smallest number greater than X which is K | C ++ Program to find the smallest K periodic integer greater than X ; Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary string ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Kperiodicinteger ( string X , int N , int K ) { string temp = X ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( X >= temp ) { return X ; } int POS ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return X ; } int main ( ) { int N = 4 , K = 2 ; string X = "1215" ; cout << Kperiodicinteger ( X , N , K ) ; return 0 ; }
Maximum cost path in an Undirected Graph such that no edge is visited twice in a row | C ++ program for the above approach ; To store the resulting sum of the cost ; To store largest cost leaf vertex ; DFS Traversal to find the update the maximum cost of from any node to leaf ; Mark vertex as visited ; Store vertex initial cost ; Initially assuming edge not to be traversed ; Back edge found so , edge can be part of traversal ; New vertex is found ; Bitwise AND the current check with the returned check by the previous DFS Call ; Adds parent and its children cost ; Updates total cost of parent including child nodes ; Edge is part of the cycle ; Add cost of vertex to the answer ; Updates the largest cost leaf vertex ; Function to find the maximum cost from source vertex such that no two edges is traversed twice ; DFS Call ; Print the maximum cost ; Driver Code ; Cost Array ; Given Graph ; Given Source Node ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 100000 ; int canTake ; int best ; int dp [ N ] ; bool vis [ N ] ; int dfs ( vector < vector < int > > & g , int * cost , int u , int pre ) { vis [ u ] = true ; dp [ u ] = cost [ u ] ; bool check = 1 ; int cur = cost [ u ] ; for ( auto & x : g [ u ] ) { if ( vis [ x ] && x != pre ) { check = 0 ; } else if ( ! vis [ x ] ) { check &= dfs ( g , cost , x , u ) ; cur = max ( cur , cost [ u ] + dp [ x ] ) ; } } dp [ u ] = cur ; if ( ! check ) { canTake += cost [ u ] ; } else { best = max ( best , dp [ u ] ) ; } return check ; } int FindMaxCost ( vector < vector < int > > & g , int * cost , int source ) { dfs ( g , cost , source , -1 ) ; cout << canTake + best ; } int main ( ) { int n = 5 , m = 5 ; int cost [ ] = { 2 , 2 , 8 , 6 , 9 } ; vector < vector < int > > g ( n ) ; g [ 0 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 0 ) ; g [ 0 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 0 ) ; g [ 0 ] . push_back ( 3 ) ; g [ 3 ] . push_back ( 0 ) ; g [ 1 ] . push_back ( 2 ) ; g [ 2 ] . push_back ( 1 ) ; g [ 1 ] . push_back ( 4 ) ; g [ 4 ] . push_back ( 1 ) ; int source = 1 ; FindMaxCost ( g , cost , source ) ; return 0 ; }
Reverse a subarray of the given array to minimize the sum of elements at even position | C ++ implementation to reverse a subarray of the given array to minimize the sum of elements at even position ; Function that will give the max negative value ; Check for count greater than 0 as we require only negative solution ; Function to print the minimum sum ; Taking sum of only even index elements ; Initialize two vectors v1 , v2 ; v1 will keep account for change if 1 th index element goes to 0 ; v2 will keep account for change if 1 th index element goes to 2 ; Get the max negative value ; Driver code
#include <bits/stdc++.h> NEW_LINE #define N  5 NEW_LINE using namespace std ; int after_rev ( vector < int > v ) { int mini = 0 , count = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { count += v [ i ] ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } void print ( int arr [ N ] ) { int sum = 0 ; for ( int i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; vector < int > v1 , v2 ; for ( int i = 0 ; i + 1 < N ; i += 2 ) v1 . push_back ( arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 1 ; i + 1 < N ; i += 2 ) v2 . push_back ( arr [ i ] - arr [ i + 1 ] ) ; int change = min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; cout << sum << endl ; } int main ( ) { int arr [ N ] = { 0 , 1 , 4 , 3 } ; print ( arr ) ; return 0 ; }
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | C ++ program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { map < int , int > mapA , mapB ; for ( int i = 0 ; i < n ; i ++ ) { mapA [ A [ i ] ] ++ ; mapB [ B [ i ] ] ++ ; } set < int > possibleValues ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . insert ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = INT_MAX ; for ( auto it : possibleValues ) { bool possible = true ; for ( auto it2 : mapA ) { if ( it2 . second != mapB [ ( it2 . first + it ) % m ] ) { possible = false ; break ; } } if ( possible ) { ans = min ( ans , it ) ; } } return ans ; } int main ( ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; cout << moduloEquality ( A , B , n , m ) << endl ; return 0 ; }
Count all indices of cyclic regular parenthesis | C ++ program for the above approach ; Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; ChecK if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCyclicShifts ( string & S , int n ) { int aux [ n ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = -1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } int main ( ) { string S = " ) ( ) ( " ; int N = S . length ( ) ; cout << countCyclicShifts ( S , N ) ; return 0 ; }
Maximize Profit by trading stocks based on given rate per day | C ++ program for the above approach ; Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Driver Code ; Given array of prices ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int * prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } int main ( ) { int prices [ ] = { 2 , 3 , 5 } ; int N = sizeof ( prices ) / sizeof ( prices [ 0 ] ) ; cout << maxProfit ( prices , N ) ; return 0 ; }
Check given string is oddly palindrome or not | Set 2 | C ++ program for the above approach ; Function to check if string formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic string is not compared ; If stack is empty then return true ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOddStringPalindrome ( string str , int n ) { int oddStringSize = n / 2 ; bool lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; stack < char > s ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . top ( ) == str [ i ] ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } int main ( ) { int N = 10 ; string s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Check given string is oddly palindrome or not | Set 2 | C ++ program for the above approach ; Functions checks if characters at odd index of the string forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOddStringPalindrome ( string str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str [ left ] != str [ right ] ) return false ; left += 2 ; right -= 2 ; } return true ; } int main ( ) { int n = 10 ; string s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Remove minimum characters from string to split it into three substrings under given constraints | C ++ program for the above approach ; Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Print the characters to be removed ; Driver Code ; Given String ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void min_remove ( string str ) { int N = str . length ( ) ; int prefix_a [ N + 1 ] ; int prefix_b [ N + 1 ] ; int prefix_c [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( str [ i - 1 ] == ' a ' ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( str [ i - 1 ] == ' b ' ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( str [ i - 1 ] == ' c ' ) ; } int maxi = INT_MIN ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } cout << ( N - maxi ) << endl ; } int main ( ) { string str = " aaaabaaxccac " ; min_remove ( str ) ; return 0 ; }
Create an array of size N with sum S such that no subarray exists with sum S or S | C ++ for the above approach ; Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Driver Code ; Given N and sum S ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { cout << "2" << " ▁ " ; s -= 2 ; } cout << s << endl ; cout << "1" << endl ; } else cout << " - 1" << endl ; } int main ( ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; }
Maximize partitions such that no two substrings have any common character | C ++ program to find the maximum number of partitions possible such that no two substrings have common character ; Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the string ; Traverse the string and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver Program
#include using namespace std ; int maximum_partition ( string str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; unordered_map m ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( m [ str [ i ] ] == 0 ) { m [ str [ i ] ] = i ; } } i = 0 ; k = m [ str [ i ] ] ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = max ( k , m [ str [ i ] ] ) ; } else { r = r + 1 ; c = 1 ; k = max ( k , m [ str [ i ] ] ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } int main ( ) { string str = " ababcbacadefegdehijhklij " ; cout << maximum_partition ( str ) ; }
Minimize steps required to move all 1 's in a matrix to a given index | C ++ program to calculate the minimum steps required to reach a given cell from all cells consisting of 1 's ; Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Driver Code ; Given matrix ; Given position ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findAns ( vector < vector < int > > mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += abs ( x - i ) + abs ( y - j ) ; } } } return ans ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; cout << findAns ( mat , x , y , mat . size ( ) , mat [ 0 ] . size ( ) ) << endl ; return 0 ; }