codeparrot/xlcost-text-to-code huggingface dataset:
Arithmetic Progression containing X and Y with least possible first term | C ++ program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minFirstTerm ( int X , int diff , int N ) { int first_term ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } void printAP ( int N , int X , int Y ) { if ( X > Y ) swap ( X , Y ) ; int maxDiff = Y - X ; int first_term = INT_MAX , diff ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { cout << first_term << " ▁ " ; first_term += diff ; } } int main ( ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; return 0 ; }
Check if a number can be represented as sum of two consecutive perfect cubes | C ++ Program of the above approach ; Function to check if a number can be expressed as the sum of cubes of two consecutive numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCubeSum ( int n ) { for ( int i = 1 ; i * i * i <= n ; i ++ ) { if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) return true ; } return false ; } int main ( ) { int n = 35 ; if ( isCubeSum ( n ) ) cout << " Yes " ; else cout << " No " ; }
Check if a number can be represented as sum of two consecutive perfect cubes | C ++ Program to implement above approach ; Function to check that a number is the sum of cubes of 2 consecutive numbers or not ; Condition to check if a number is the sum of cubes of 2 consecutive numbers or not ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSumCube ( int N ) { int a = cbrt ( N ) ; int b = a - 1 ; return ( ( a * a * a + b * b * b ) == N ) ; } int main ( ) { int i = 35 ; if ( isSumCube ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Maximize sum of remaining elements after every removal of the array half with greater sum | C ++ 14 program to implement the above approach ; Function to find the maximum sum ; Base case len of array is 1 ; Stores the final result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the final ans ; Function to print maximum sum ; Dicitionary to store prefix sums ; Traversing the array ; Add prefix sum of the array ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxweight ( int s , int e , unordered_map < int , int > & pre ) { if ( s == e ) return 0 ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre [ i ] - pre [ s - 1 ] ; int right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = max ( { ans , left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) } ) ; } if ( left > right ) ans = max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } void maxSum ( vector < int > arr ) { unordered_map < int , int > pre ; pre [ -1 ] = 0 ; pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } cout << maxweight ( 0 , arr . size ( ) - 1 , pre ) ; } int main ( ) { vector < int > arr = { 6 , 2 , 3 , 4 , 5 , 5 } ; maxSum ( arr ) ; return 0 ; }
Maximum time in HH : MM : SS format that can be represented by given six digits | C ++ Program of the above approach ; Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestTimeFromDigits ( vector < int > & A ) { map < int , int > mp1 , mp2 ; for ( auto x : A ) { mp1 [ x ] ++ ; } mp2 = mp1 ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; vector < int > arr { h0 , h1 , m0 , m1 , s0 , s1 } ; for ( auto & it : arr ) { if ( mp1 [ it ] > 0 ) { mp1 [ it ] -- ; } else { p = 1 ; } } if ( p == 0 ) { string s = " " ; s = to_string ( h0 ) + to_string ( h1 ) ; s += ' : ' + to_string ( m0 ) + to_string ( m1 ) ; s += ' : ' + to_string ( s0 ) + to_string ( s1 ) ; return s ; } mp1 = mp2 ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } int main ( ) { vector < int > v = { 0 , 2 , 1 , 9 , 3 , 2 } ; cout << largestTimeFromDigits ( v ) ; }
Minimum size substring to be removed to make a given string palindromic | C ++ program of the above approach ; Function to find palindromic prefix of maximum length ; Finding palindromic prefix of maximum length ; Checking if curr substring is palindrome or not . ; Condition to check if the prefix is a palindrome ; if no palindrome exist ; Function to find the maximum size palindrome such that after removing minimum size substring ; Finding prefix and suffix of same length ; Matching the ranges ; Case 1 : Length is even and whole string is palindrome ; Case 2 : Length is odd and whole string is palindrome ; Adding that mid character ; Add prefix or suffix of the remaining string or suffix , whichever is longer ; Reverse the remaining string to find the palindromic suffix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string palindromePrefix ( string S ) { int n = S . size ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { string curr = S . substr ( 0 , i + 1 ) ; int l = 0 , r = curr . size ( ) - 1 ; bool is_palindrome = 1 ; while ( l < r ) { if ( curr [ l ] != curr [ r ] ) { is_palindrome = 0 ; break ; } l ++ ; r -- ; } if ( is_palindrome ) return curr ; } return " " ; } string maxPalindrome ( string S ) { int n = S . size ( ) ; if ( n <= 1 ) { return S ; } string pre = " " , suff = " " ; int i = 0 , j = n - 1 ; while ( S [ i ] == S [ j ] && i < j ) { i ++ ; j -- ; } i -- ; j ++ ; pre = S . substr ( 0 , i + 1 ) ; suff = S . substr ( j ) ; if ( j - i == 1 ) { return pre + suff ; } if ( j - i == 2 ) { string mid_char = S . substr ( i + 1 , 1 ) ; return pre + mid_char + suff ; } string rem_str = S . substr ( i + 1 , j - i - 1 ) ; string pre_of_rem_str = palindromePrefix ( rem_str ) ; reverse ( rem_str . begin ( ) , rem_str . end ( ) ) ; string suff_of_rem_str = palindromePrefix ( rem_str ) ; if ( pre_of_rem_str . size ( ) >= suff_of_rem_str . size ( ) ) { return pre + pre_of_rem_str + suff ; } else { return pre + suff_of_rem_str + suff ; } } int main ( ) { string S = " geeksforskeeg " ; cout << maxPalindrome ( S ) ; return 0 ; }
Count numbers from a given range that contains a given number as the suffix | C ++ Program of the above approach ; Function to count the number ends with given number in range ; Find number of digits in A ; Find the power of 10 ; Incrementing the A ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNumEnds ( int A , int L , int R ) { int temp , count = 0 , digits ; int cycle ; digits = log10 ( A ) + 1 ; temp = pow ( 10 , digits ) ; cycle = temp ; while ( temp <= R ) { if ( temp >= L ) count ++ ; temp += cycle ; } cout << count ; } int main ( ) { int A = 2 , L = 2 , R = 20 ; countNumEnds ( A , L , R ) ; }
Minimize replacement of characters to its nearest alphabet to make a string palindromic | C ++ program for the above approach ; Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string s ) { int len = s . length ( ) ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = max ( s [ i ] , s [ len - 1 - i ] ) - min ( s [ i ] , s [ len - 1 - i ] ) ; int D2 = 26 - D1 ; result += min ( D1 , D2 ) ; } return result ; } int main ( ) { string s = " abccdb " ; cout << minOperations ( s ) ; return 0 ; }
Check if all the pairs of an array are coprime with each other | C ++ implementation of the above approach ; Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findFactor ( int value , unordered_set < int > & factors ) { factors . insert ( value ) ; for ( int i = 2 ; i * i <= value ; i ++ ) { if ( value % i == 0 ) { if ( value / i == i ) { if ( factors . find ( i ) != factors . end ( ) ) { return true ; } else { factors . insert ( i ) ; } } else { if ( factors . find ( i ) != factors . end ( ) || factors . find ( value / i ) != factors . end ( ) ) { return true ; } else { factors . insert ( i ) ; factors . insert ( value / i ) ; } } } } return false ; } bool allCoprime ( int A [ ] , int n ) { bool all_coprime = true ; unordered_set < int > factors ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == 1 ) continue ; if ( findFactor ( A [ i ] , factors ) ) { all_coprime = false ; break ; } } return all_coprime ; } int main ( ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( allCoprime ( A , arr_size ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count subsequences which contains both the maximum and minimum array element | C ++ program for the above approach ; Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int arr [ ] , int n , int value ) ; double countSubSequence ( int arr [ ] , int n ) { int maximum = * max_element ( arr , arr + n ) ; int minimum = * min_element ( arr , arr + n ) ; if ( maximum == minimum ) return pow ( 2 , n ) - 1 ; int i = count ( arr , n , maximum ) ; int j = count ( arr , n , minimum ) ; double res = ( pow ( 2 , i ) - 1 ) * ( pow ( 2 , j ) - 1 ) * pow ( 2 , n - i - j ) ; return res ; } int count ( int arr [ ] , int n , int value ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubSequence ( arr , n ) << endl ; }
Minimize elements to be added to a given array such that it contains another given array as its subsequence | C ++ 14 program for the above approach ; Function that finds the minimum number of the element must be added to make A as a subsequence in B ; Base Case ; dp [ i ] [ j ] indicates the length of LCS of A of length i & B of length j ; If there are no elements either in A or B then the length of lcs is 0 ; If the element present at ith and jth index of A and B are equal then include in LCS ; If they are not equal then take the max ; Return difference of length of A and lcs of A and B ; Driver Code ; Given sequence A and B ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int transformSubsequence ( int n , int m , vector < int > A , vector < int > B ) { if ( B . size ( ) == 0 ) return n ; vector < vector < int > > dp ( n + 1 , vector < int > ( m + 1 , 0 ) ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 or j == 0 ) dp [ i ] [ j ] = 0 ; else if ( A [ i - 1 ] == B [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return n - dp [ n ] [ m ] ; } int main ( ) { int N = 5 ; int M = 6 ; vector < int > A = { 1 , 2 , 3 , 4 , 5 } ; vector < int > B = { 2 , 5 , 6 , 4 , 9 , 12 } ; cout << transformSubsequence ( N , M , A , B ) ; return 0 ; }
Check if the sum of a subarray within a given range is a perfect square or not | C ++ implementation of the above approach ; Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Driver Code ; Given Array ; Given range ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkForPerfectSquare ( vector < int > arr , int i , int j ) { int mid , sum = 0 ; for ( int m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } int low = 0 , high = sum / 2 ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return -1 ; } int main ( ) { vector < int > arr ; arr = { 2 , 19 , 33 , 48 , 90 , 100 } ; int L = 1 , R = 3 ; cout << checkForPerfectSquare ( arr , L , R ) ; return 0 ; }
Count of subarrays forming an Arithmetic Progression ( AP ) | C ++ implementation of the above approach ; Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Driver Code ; Given array ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calcSubarray ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { bool flag = true ; int comm_diff = A [ i + 1 ] - A [ i ] ; for ( int k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } int main ( ) { int A [ 5 ] = { 8 , 7 , 4 , 1 , 0 } ; int N = sizeof ( A ) / sizeof ( int ) ; cout << calcSubarray ( A , N ) ; }
Largest Sum Contiguous Subarray having unique elements | C ++ program for the above approach ; Function to calculate required maximum subarray sum ; Initialize two pointers ; Stores the unique elements ; Insert the first element ; Current max sum ; Global maximum sum ; Update sum & increment j auto pos = s . find ( 3 ) ; ; Add the current element ; Update sum and increment i and remove arr [ i ] from set ; Remove the element from start position ; Return the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call ; Print the maximum sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumSubarray ( int arr [ ] , int n ) { int i = 0 , j = 1 ; set < int > set ; set . insert ( arr [ 0 ] ) ; int sum = arr [ 0 ] ; int maxsum = sum ; while ( i < n - 1 && j < n ) { const bool is_in = set . find ( arr [ j ] ) != set . end ( ) ; if ( ! is_in ) { sum = sum + arr [ j ] ; maxsum = max ( sum , maxsum ) ; set . insert ( arr [ j ++ ] ) ; } else { sum -= arr [ i ] ; set . erase ( arr [ i ++ ] ) ; } } return maxsum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 , 5 } ; int ans = maxSumSubarray ( arr , 5 ) ; cout << ( ans ) ; }
Count of numbers having only one unset bit in a range [ L , R ] | C ++ program for the above approach ; Function to count numbers in the range [ l , r ] having exactly one unset bit ; Stores the required count ; Iterate over the range ; Calculate number of bits ; Calculate number of set bits ; If count of unset bits is 1 ; Increment answer ; Return the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_numbers ( int L , int R ) { int ans = 0 ; for ( int n = L ; n <= R ; n ++ ) { int no_of_bits = log2 ( n ) + 1 ; int no_of_set_bits = __builtin_popcount ( n ) ; if ( no_of_bits - no_of_set_bits == 1 ) { ans ++ ; } } return ans ; } int main ( ) { int L = 4 , R = 9 ; cout << count_numbers ( L , R ) ; return 0 ; }
Count of numbers having only one unset bit in a range [ L , R ] | C ++ program for the above approach ; Function to count numbers in the range [ L , R ] having exactly one unset bit ; Stores the count elements having one zero in binary ; Stores the maximum number of bits needed to represent number ; Loop over for zero bit position ; Number having zero_bit as unset and remaining bits set ; Sets all bits before zero_bit ; Set the bit at position j ; Set the bit position at j ; If cur is in the range [ L , R ] , then increment ans ; Return ans ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_numbers ( int L , int R ) { int ans = 0 ; int LogR = log2 ( R ) + 1 ; for ( int zero_bit = 0 ; zero_bit < LogR ; zero_bit ++ ) { int cur = 0 ; for ( int j = 0 ; j < zero_bit ; j ++ ) { cur |= ( 1LL << j ) ; } for ( int j = zero_bit + 1 ; j < LogR ; j ++ ) { cur |= ( 1LL << j ) ; if ( cur >= L && cur <= R ) { ans ++ ; } } } return ans ; } int main ( ) { long long L = 4 , R = 9 ; cout << count_numbers ( L , R ) ; return 0 ; }
Sum of all distances between occurrences of same characters in a given string | C ++ program for the above approach ; Function to calculate the sum of distances between occurrences of same characters in a string ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( string s ) { int sum = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . size ( ) ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } int main ( ) { string s = " ttt " ; cout << findSum ( s ) << endl ; }
Count array elements that can be represented as sum of at least two consecutive array elements | C ++ program for above approach ; Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Driver Code ; Given array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElements ( int a [ ] , int n ) { int cnt [ n + 1 ] = { 0 } ; memset ( cnt , 0 , sizeof ( cnt ) ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ++ cnt [ a [ i ] ] ; } for ( int l = 0 ; l < n ; ++ l ) { int sum = 0 ; for ( int r = l ; r < n ; ++ r ) { sum += a [ r ] ; if ( l == r ) continue ; if ( sum <= n ) { ans += cnt [ sum ] ; cnt [ sum ] = 0 ; } } } return ans ; } int main ( ) { int a [ ] = { 1 , 1 , 1 , 1 , 1 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countElements ( a , N ) ; }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | C ++ program for the above approach ; Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int n ) { int maxHcf = INT_MIN ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { maxHcf = max ( maxHcf , __gcd ( i , j ) ) ; } } return maxHcf ; } int main ( ) { int n = 4 ; cout << maxGCD ( n ) ; return 0 ; }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | C ++ implementation of the above approach ; Function to find the maximum GCD among all the pairs from first n natural numbers ; Return max GCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int n ) { return ( n / 2 ) ; } int main ( ) { int n = 4 ; cout << maxGCD ( n ) ; return 0 ; }
Modify given array by incrementing first occurrence of every element by K | C ++ Program to implement the above approach ; Print the required final sequence ; Stores the array element - index pairs ; Stores the required sequence ; Insert all array elements ; If current element has not occurred previously ; Otherwise ; Iterator to the first index containing sol [ i ] ; Remove that occurrence ; Increment by K ; Insert the incremented element at that index ; Print the final sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSequence ( vector < int > & A , int n , int k ) { unordered_map < int , set < int > > mp ; vector < int > sol ; for ( int x : A ) sol . push_back ( x ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( sol [ i ] ) == mp . end ( ) || mp [ sol [ i ] ] . size ( ) == 0 ) { mp [ sol [ i ] ] . insert ( i ) ; } else { auto idxx = mp [ sol [ i ] ] . begin ( ) ; int idx = * idxx ; mp [ sol [ i ] ] . erase ( idxx ) ; sol [ idx ] += k ; mp [ sol [ idx ] ] . insert ( idx ) ; mp [ sol [ i ] ] . insert ( i ) ; } } for ( int x : sol ) { cout << x << " ▁ " ; } } int main ( ) { int N = 5 ; int K = 6 ; vector < int > A = { 1 , 4 , 1 , 1 , 4 } ; printSequence ( A , N , K ) ; }
Remove odd indexed characters from a given string | C ++ program to implement the above approach ; Function to remove the odd indexed characters from a given string ; Stores the resultant string ; If current index is odd ; Skip the character ; Otherwise , append the character ; Return the result ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeOddIndexCharacters ( string s ) { string new_string = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 1 ) { continue ; } new_string += s [ i ] ; } return new_string ; } int main ( ) { string str = " abcdef " ; cout << removeOddIndexCharacters ( str ) ; return 0 ; }