codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the number of indices such that element is greater than element to its left | C ++ program of the above approach ; Function to find the maximum pairs such that arr [ i + 1 ] > arr [ i ] ; To store the frequency of the element in arr [ ] ; Store the frequency in map M ; To find the maximum frequency store in map M ; Print the maximum number of possible pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N ) { unordered_map < int , int > M ; for ( int i = 0 ; i < N ; i ++ ) { M [ arr [ i ] ] ++ ; } int maxFreq = 0 ; for ( auto & it : M ) { maxFreq = max ( maxFreq , it . second ) ; } cout << N - maxFreq << endl ; } int main ( ) { int arr [ ] = { 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Find minimum value of the expression by choosing K elements from given array | CPP program to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Function to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumValue ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } int main ( ) { int arr [ ] = { 4 , 21 , 5 , 3 , 8 } , k = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumValue ( arr , n , k ) ; return 0 ; }
Transform N to Minimum possible value | CPP program to transform N to the minimum value ; Initialising the answer ; Function to find the digitsum ; Iterate over all digits and add them ; Return the digit su , ; Function to transform N to the minimum value ; If the final value is lesser than least value ; If final value is equal to least value then check for lesser number of steps to reach this value ; The value will be obtained in less than 15 steps as proved so applying normal recursive operations ; Driver code ; Function call ; Print the answers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_val = INT_MAX ; int min_steps = 0 ; int sumOfDigits ( int n ) { string s = to_string ( n ) ; int sum = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum += ( s [ i ] - '0' ) ; } return sum ; } void Transform ( int n , int d , int steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } int main ( ) { int N = 9 , D = 3 ; Transform ( N , D , 0 ) ; cout << min_val << " ▁ " << min_steps ; return 0 ; }
Remove all 1 s from the adjacent left of 0 s in a Binary Array | C ++ implementation of the above approach ; Function to find the maximum number of 1 's before 0 ; Traverse the array ; If value is 1 ; If consecutive 1 followed by 0 , then update the maxCnt ; Print the maximum consecutive 1 's  followed by 0 ; Driver Code ; Function Call ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void noOfMoves ( int arr [ ] , int n ) { int cnt = 0 ; int maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = max ( maxCnt , cnt ) ; cnt = 0 ; } } } cout << maxCnt << endl ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; noOfMoves ( arr , N ) ; int arr1 [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 } ; N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; noOfMoves ( arr1 , N ) ; return 0 ; }
Check if given coins can be used to pay a value of S | C ++ implementation to check if it is possible to pay a value ; Function to check if it is possible to pay a value ; Loop to add the value of coin A ; Condition to check if it is possible to pay a value of S ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void knowPair ( int a , int b , int n , int s , int m ) { int i = 0 , rem = 0 ; int count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = rem / m ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { cout << " YES " << endl ; } else { cout << " NO " << endl ; } } int main ( ) { int A = 1 ; int B = 2 ; int n = 3 ; int S = 4 ; int m = 2 ; knowPair ( A , B , n , S , m ) ; return 0 ; }
Sum of all numbers in the given range which are divisible by M | C ++ program to find the sum of numbers divisible by M in the given range ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; Running a loop from A to B and check if a number is divisible by i . ; If the number is divisible , then add it to sum ; Return the sum ; Driver code ; A and B define the range M is the dividend ; Printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumDivisibles ( int A , int B , int M ) { int sum = 0 ; for ( int i = A ; i <= B ; i ++ ) if ( i % M == 0 ) sum += i ; return sum ; } int main ( ) { int A = 6 , B = 15 , M = 3 ; cout << sumDivisibles ( A , B , M ) << endl ; return 0 ; }
Sum of all numbers in the given range which are divisible by M | C ++ program to find the sum of numbers divisible by M in the given range ; Function to find the largest number smaller than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the smallest number greater than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; To bring the smallest and largest numbers in the range [ A , B ] ; To count the number of terms in the AP ; Sum of n terms of an AP ; Driver code ; A and B define the range , M is the dividend ; Printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSmallNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } int findLargeNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } int sumDivisibles ( int A , int B , int M ) { int sum = 0 ; int first = findSmallNum ( A , M ) ; int last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; int n = ( B / M ) - ( A - 1 ) / M ; return n * ( first + last ) / 2 ; } int main ( ) { int A = 6 , B = 15 , M = 3 ; cout << sumDivisibles ( A , B , M ) ; return 0 ; }
Count of all unique substrings with non | C ++ program to find the count of all unique sub - strings with non - repeating characters ; Function to count all unique distinct character substrings ; Hashmap to store all substrings ; Iterate over all the substrings ; Boolean array to maintain all characters encountered so far ; Variable to maintain the substring till current position ; Get the position of the character in the string ; Check if the character is encountred ; Add the current character to the substring ; Insert substring in Hashmap ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctSubstring ( string & P , int N ) { unordered_set < string > S ; for ( int i = 0 ; i < N ; ++ i ) { vector < bool > freq ( 26 , false ) ; string s ; for ( int j = i ; j < N ; ++ j ) { int pos = P [ j ] - ' a ' ; if ( freq [ pos ] == true ) break ; freq [ pos ] = true ; s += P [ j ] ; S . insert ( s ) ; } } return S . size ( ) ; } int main ( ) { string S = " abba " ; int N = S . length ( ) ; cout << distinctSubstring ( S , N ) ; return 0 ; }
Sum of all subarrays of size K | C ++ implementation to find the sum of all subarrays of size K ; Function to find the sum of all subarrays of size K ; Loop to consider every subarray of size K ; Initialize sum = 0 ; Calculate sum of all elements of current subarray ; Print sum of each subarray ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calcSum ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) { int sum = 0 ; for ( int j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; cout << sum << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; calcSum ( arr , n , k ) ; return 0 ; }
Sum of all subarrays of size K | C ++ implementation to find the sum of all subarrays of size K ; Function to find the sum of all subarrays of size K ; Initialize sum = 0 ; Consider first subarray of size k Store the sum of elements ; Print the current sum ; Consider every subarray of size k Remove first element and add current element to the window ; Add the element which enters into the window and subtract the element which pops out from the window of the size K ; Print the sum of subarray ; Drivers Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calcSum ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; cout << sum << " ▁ " ; for ( int i = k ; i < n ; i ++ ) { sum = ( sum - arr [ i - k ] ) + arr [ i ] ; cout << sum << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; calcSum ( arr , n , k ) ; return 0 ; }
Number of indices pair such that element pair sum from first Array is greater than second Array | C ++ program to find the number of indices pair such that pair sum from first Array is greater than second Array ; Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPairs ( vector < int > A , vector < int > B , int n ) { vector < int > D ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { D [ i ] = A [ i ] - B [ i ] ; } sort ( D . begin ( ) , D . end ( ) ) ; long long total = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( D [ i ] > 0 ) { total += n - i - 1 ; } else { int k = upper_bound ( D . begin ( ) , D . end ( ) , - D [ i ] ) - D . begin ( ) ; total += n - k ; } } return total ; } int main ( ) { int n = 5 ; vector < int > A ; vector < int > B ; A . push_back ( 4 ) ; A . push_back ( 8 ) ; A . push_back ( 2 ) ; A . push_back ( 6 ) ; A . push_back ( 2 ) ; B . push_back ( 4 ) ; B . push_back ( 5 ) ; B . push_back ( 4 ) ; B . push_back ( 1 ) ; B . push_back ( 3 ) ; cout << getPairs ( A , B , n ) ; }
Number of pairs such that their HCF and LCM is equal | Naive C ++ program to count number of pairs such that their hcf and lcm are equal ; Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } int countPairs ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Number of pairs such that their HCF and LCM is equal | C ++ program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int a [ ] , int n ) { unordered_map < int , int > frequency ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] ++ ; } int count = 0 ; for ( auto x : frequency ) { int f = x . second ; count += f * ( f - 1 ) / 2 ; } return count ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Find the maximum possible value of last element of the Array | C ++ program to find the maximum possible value of last element of the array ; Function to find the maximum possible value of last element of the array ; Traverse for all element ; Find the distance ; If moves less than distance then we can not move this number to end ; How many number we can move to end ; Take the minimum of both of them ; Increment in the end ; Remove taken moves ; Return the last element ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxValue ( int arr [ ] , int n , int moves ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { int distance = n - 1 - i ; if ( moves < distance ) break ; int can_take = moves / distance ; int take = min ( arr [ i ] , can_take ) ; arr [ n - 1 ] += take ; moves -= take * distance ; } } return arr [ n - 1 ] ; } int main ( ) { int arr [ ] = { 2 , 3 , 0 , 1 } ; int M = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxValue ( arr , N , M ) ; return 0 ; }
Minimum number of Factorials whose sum is equal to N | C ++ program to find minimum number of factorials ; Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Print min count ; Print result ; Driver program
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll fact [ 14 ] ; int size = 1 ; void preCompute ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } void findMin ( int N ) { preCompute ( N ) ; int originalN = N ; vector < int > ans ; for ( int i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . push_back ( fact [ i ] ) ; } } cout << ans . size ( ) << " STRNEWLINE " ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int n = 27 ; findMin ( n ) ; return 0 ; }
Longest increasing sub | C ++ implementation to find the longest increasing subsequence in repeating element of array ; Function to find the LCS ; Loop to create frequency array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCS ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } return mp . size ( ) ; } int main ( ) { int n = 3 ; int arr [ ] = { 3 , 2 , 1 } ; cout << findLCS ( arr , n ) ; return 0 ; }
Path with maximum product in 2 | C ++ Program to find maximum product path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Function to find maximum product ; It will store the maximum product till a given cell . ; It will store the minimum product till a given cell ( for - ve elements ) ; If we are at topmost or leftmost , just copy the elements . ; If we 're not at the  above, we can consider  the above value. ; If we 're not on the  leftmost, we can consider  the left value. ; Store max & min product till i , j . ; Return the max product path from 0 , 0 -> N - 1 , M - 1. ; Driver Code ; Print maximum product from ( 0 , 0 ) to ( N - 1 , M - 1 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  3 NEW_LINE int maxProductPath ( int arr [ N ] [ M ] ) { vector < vector < int > > maxPath ( N , vector < int > ( M , INT_MIN ) ) ; vector < vector < int > > minPath ( N , vector < int > ( M , INT_MAX ) ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int minVal = INT_MAX ; int maxVal = INT_MIN ; if ( i == 0 && j == 0 ) { maxVal = arr [ i ] [ j ] ; minVal = arr [ i ] [ j ] ; } if ( i > 0 ) { int tempMax = max ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; maxVal = max ( maxVal , tempMax ) ; int tempMin = min ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; minVal = min ( minVal , tempMin ) ; } if ( j > 0 ) { int tempMax = max ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; maxVal = max ( maxVal , tempMax ) ; int tempMin = min ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; minVal = min ( minVal , tempMin ) ; } maxPath [ i ] [ j ] = maxVal ; minPath [ i ] [ j ] = minVal ; } } return maxPath [ N - 1 ] [ M - 1 ] ; } int main ( ) { int arr [ N ] [ M ] = { { 1 , -2 , 3 } , { 4 , -5 , 6 } , { -7 , -8 , 9 } } ; cout << maxProductPath ( arr ) << endl ; return 0 ; }
Vertical and Horizontal retrieval ( MRT ) on Tapes | C ++ program to print Vertical filling ; 2D matrix for vertical insertion on tapes ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; It is used for calculating mean retrieval time ; vertical insertion on tape ; initialize variables to 0 for each iteration ; Used for getting ' sum ' sizes of records in tape to determine whether tape is full or not ; if tape is not full ; check for ability of tapes to hold value ; initialize variables to 0 for each iteration ; display elements of tape ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
#include <bits/stdc++.h> NEW_LINE using namespace std ; void vertical_Fill ( int records [ ] , int tape [ ] , int m , int n ) { int v [ m ] [ n ] = { 0 } ; int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int z = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { sum = 0 ; for ( int k = 0 ; k < i ; k ++ ) { sum += v [ j ] [ k ] ; } if ( sum + records [ z ] <= tape [ j ] ) { v [ j ] [ i ] = records [ z ] ; z ++ ; } } if ( v [ 2 ] [ i ] == 0 ) { break ; } } for ( int i = 0 ; i < m ; i ++ ) { Retrieval_Time = 0 ; cout << " tape ▁ " << i + 1 << " ▁ : ▁ [ ▁ " ; for ( j = 0 ; j < n ; j ++ ) { if ( v [ i ] [ j ] != 0 ) { cout << v [ i ] [ j ] << " ▁ " ; } else { break ; } } cout << " ] " ; for ( int k = 0 ; v [ i ] [ k ] != 0 ; k ++ ) { Retrieval_Time += v [ i ] [ k ] * ( j - k ) ; } Mrt = ( double ) Retrieval_Time / j ; cout << " TABSYMBOL MRT ▁ : ▁ " << Mrt << endl ; } } int main ( ) { int records [ ] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int tape [ ] = { 25 , 80 , 160 } ; int n = sizeof ( records ) / sizeof ( records [ 0 ] ) ; int m = sizeof ( tape ) / sizeof ( tape [ 0 ] ) ; sort ( records , records + n ) ; vertical_Fill ( records , tape , m , n ) ; return 0 ; }
Minimum number of subsequences required to convert one string to another | C ++ program to find the Minimum number of subsequences required to convert one string to another ; Function to find the no of subsequences ; Push the values of indexes of each character ; Find the next index available in the array ; If Character is not in string A ; Check if the next index is not equal to the size of array which means there is no index greater than minIndex in the array ; Update value of minIndex with this index ; Update the value of counter and minIndex for next operation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSubsequnces ( string A , string B ) { vector < int > v [ 26 ] ; int minIndex = -1 , cnt = 1 , j = 0 ; int flag = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { int p = ( int ) A [ i ] - 97 ; v [ p ] . push_back ( i ) ; } while ( j < B . length ( ) ) { int p = ( int ) B [ j ] - 97 ; int k = upper_bound ( v [ p ] . begin ( ) , v [ p ] . end ( ) , minIndex ) - v [ p ] . begin ( ) ; if ( v [ p ] . size ( ) == 0 ) { flag = 1 ; break ; } if ( k != v [ p ] . size ( ) ) { minIndex = v [ p ] [ k ] ; j = j + 1 ; } else { cnt = cnt + 1 ; minIndex = -1 ; } } if ( flag == 1 ) { return -1 ; } return cnt ; } int main ( ) { string A1 = " abbace " ; string B1 = " acebbaae " ; cout << minSubsequnces ( A1 , B1 ) << endl ; return 0 ; }
Find two equal subsequences of maximum length with at least one different index | C ++ implementation of the approach ; Function to return the required length of the subsequences ; To store the result ; To store the last visited position of lowercase letters ; Initialisation of frequency array to - 1 to indicate no character has previously occured ; For every character of the string ; Get the index of the current character ; If the current character has appeared before in the string ; Update the result ; Update the last position of the current character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; int maxLength ( string str , int len ) { int res = 0 ; int lastPos [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { lastPos [ i ] = -1 ; } for ( int i = 0 ; i < len ; i ++ ) { int C = str [ i ] - ' a ' ; if ( lastPos [ C ] != -1 ) { res = max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; } lastPos [ C ] = i ; } return res ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; cout << maxLength ( str , len ) ; return 0 ; }
Maximum profit by selling N items at two markets | C ++ implementation of the approach ; Max profit will be saved here ; loop to check all possible combinations of sales ; the sum of the profit after the sale for products 0 to i in market A ; the sum of the profit after the sale for products i to n in market B ; Replace the value of Max Profit with a bigger value among maxP and sumA + sumB ; Return the value of Max Profit ; Driver Program11111111111111111111111
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( vector < int > a , vector < int > b , int n ) { int maxP = -1 ; for ( int i = 0 ; i < n + 1 ; i ++ ) { int sumA = 0 ; for ( int j = 0 ; j < min ( i , ( int ) a . size ( ) ) ; j ++ ) sumA += a [ j ] ; int sumB = 0 ; for ( int j = i ; j < b . size ( ) ; j ++ ) sumB += b [ j ] ; maxP = max ( maxP , sumA + sumB ) ; } return maxP ; } int main ( ) { vector < int > a = { 2 , 3 , 2 } ; vector < int > b = { 10 , 30 , 40 } ; cout << maxProfit ( a , b , 4 ) ; return 0 ; }