codeparrot/xlcost-text-to-code huggingface dataset:
Program to generate CAPTCHA and verify user | C ++ program to automatically generate CAPTCHA and verify user ; Returns true if given two strings are same ; Generates a CAPTCHA of given length ; Characters to be included ; Generate n characters from above set and add these characters to captcha . ; Driver code ; Generate a random CAPTCHA ; Ask user to enter a CAPTCHA ; Notify user about matching status
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkCaptcha ( string & captcha , string & user_captcha ) { return captcha . compare ( user_captcha ) == 0 ; } string generateCaptcha ( int n ) { time_t t ; srand ( ( unsigned ) time ( & t ) ) ; char * chrs = " abcdefghijklmnopqrstuvwxyzABCDEFGHI " " JKLMNOPQRSTUVWXYZ0123456789" ; string captcha = " " ; while ( n -- ) captcha . push_back ( chrs [ rand ( ) % 62 ] ) ; return captcha ; } int main ( ) { string captcha = generateCaptcha ( 9 ) ; cout << captcha ; string usr_captcha ; cout << " Enter above CAPTCHA : " ; cin >> usr_captcha ; if ( checkCaptcha ( captcha , usr_captcha ) ) printf ( " CAPTCHA Matched " else printf ( " CAPTCHA Not Matched " return 0 ; }
Sum of N | C ++ implementation to illustrate the program ; Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to print the value of Sum ; Driver Code ; First element ; Common difference ; Number of elements ; Mod value
#include <iostream> NEW_LINE using namespace std ; int SumGPUtil ( long long int r , long long int n , long long int m ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return ( 1 + r ) % m ; long long int ans ; if ( n % 2 == 1 ) { ans = ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n - 1 ) / 2 , m ) ; } else { ans = 1 + ( r * ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n / 2 ) - 1 , m ) ) ; } return ( ans % m ) ; } void SumGP ( long long int a , long long int r , long long int N , long long int M ) { long long int answer ; answer = a * SumGPUtil ( r , N , M ) ; answer = answer % M ; cout << answer << endl ; } int main ( ) { long long int a = 1 ; long long int r = 4 ; long long int N = 10000 ; long long int M = 100000 ; SumGP ( a , r , N , M ) ; return 0 ; }
Length of longest subarray in which elements greater than K are more than elements not greater than K | C ++ implementation of above approach ; Function to find the length of a longest subarray in which elements greater than K are more than elements not greater than K ; Create a new array in which we store 1 if a [ i ] > k otherwise we store - 1. ; Taking prefix sum over it ; len will store maximum length of subarray ; This indicate there is at least one subarray of length mid that has sum > 0 ; Check every subarray of length mid if it has sum > 0 or not if sum > 0 then it will satisfy our required condition ; x will store the sum of subarray of length mid ; Satisfy our given condition ; Check for higher length as we get length mid ; Check for lower length as we did not get length mid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestSubarray ( int a [ ] , int n , int k ) { int pre [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > k ) pre [ i ] = 1 ; else pre [ i ] = -1 ; } for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + pre [ i ] ; int len = 0 ; int lo = 1 , hi = n ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; bool ok = false ; for ( int i = mid - 1 ; i < n ; i ++ ) { int x = pre [ i ] ; if ( i - mid >= 0 ) x -= pre [ i - mid ] ; if ( x > 0 ) { ok = true ; break ; } } if ( ok == true ) { len = mid ; lo = mid + 1 ; } else hi = mid - 1 ; } return len ; } int main ( ) { int a [ ] = { 2 , 3 , 4 , 5 , 3 , 7 } ; int k = 3 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << LongestSubarray ( a , n , k ) ; return 0 ; }
Choose points from two ranges such that no point lies in both the ranges | C ++ implementation of the approach ; Function to find the required points ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPoints ( int l1 , int r1 , int l2 , int r2 ) { int x = ( l1 != l2 ) ? min ( l1 , l2 ) : -1 ; int y = ( r1 != r2 ) ? max ( r1 , r2 ) : -1 ; cout << x << " ▁ " << y ; } int main ( ) { int l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ; }
Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned int fact ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } int main ( ) { cout << fact ( 5 ) ; return 0 ; }
Minimum number of working days required to achieve each of the given scores | C ++ program for the above approach ; Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySeach ( vector < int > P , int N ) { int i = 0 ; int j = P . size ( ) - 1 ; int index = -1 ; while ( i <= j ) { int mid = i + ( j - i ) / 2 ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } void minDays ( vector < int > P , vector < int > arr ) { for ( int i = 1 ; i < P . size ( ) ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int index = binarySeach ( P , arr [ i ] ) ; if ( index != -1 ) { cout << index + 1 << " ▁ " ; } else { cout << -1 << " ▁ " ; } } } int main ( ) { vector < int > arr = { 400 , 200 , 700 , 900 , 1400 } ; vector < int > P = { 100 , 300 , 400 , 500 , 600 } ; minDays ( P , arr ) ; return 0 ; }
Maximize profit that can be earned by selling an item among N buyers | C ++ program for the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver code
#include <iostream> NEW_LINE #include <climits> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; int maximumProfit ( int arr [ ] , int N ) { int ans = INT_MIN ; int price = 0 ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N ; i ++ ) { int count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } int main ( ) { int arr [ ] = { 22 , 87 , 9 , 50 , 56 , 43 } ; cout << maximumProfit ( arr , 6 ) ; return 0 ; }
Maximum length palindromic substring for every index such that it starts and ends at that index | C ++ program for the above approach ; Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string S , int i , int j ) { while ( i < j ) { if ( S [ i ] != S [ j ] ) return false ; i ++ ; j -- ; } return true ; } void printLongestPalindrome ( string S , int N ) { int palLength [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int maxlength = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( S [ j ] == S [ i ] ) { if ( isPalindrome ( S , j , i ) ) { maxlength = i - j + 1 ; break ; } } } for ( int j = N - 1 ; j > i ; j -- ) { if ( S [ j ] == S [ i ] ) { if ( isPalindrome ( S , i , j ) ) { maxlength = max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( int i = 0 ; i < N ; i ++ ) { cout << palLength [ i ] << " ▁ " ; } } int main ( ) { string S = " bababa " ; int N = S . length ( ) ; printLongestPalindrome ( S , N ) ; return 0 ; }
Sum of array elements which are multiples of a given number | C ++ program for the above approach ; Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the given array ; If current element is a multiple of N ; Print total sum ; Driver Code ; Given arr [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mulsum ( int arr [ ] , int n , int N ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } cout << sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int N = 3 ; mulsum ( arr , n , N ) ; return 0 ; }
Lexicographically largest string possible consisting of at most K consecutive similar characters | C ++ program for the above approach ; Function to return nearest lower character ; Traverse charset from start - 1 ; If no character can be appended ; Function to find largest string ; Stores the frequency of characters ; Traverse the string ; Append larger character ; Decrease count in charset ; Increase count ; Check if count reached to charLimit ; Find nearest lower char ; If no character can be appended ; Append nearest lower character ; Reset count for next calculation ; Return new largest string ; Driver code ; Given string s
#include <bits/stdc++.h> NEW_LINE using namespace std ; char nextAvailableChar ( vector < int > charset , int start ) { for ( int i = start - 1 ; i >= 0 ; i -- ) { if ( charset [ i ] > 0 ) { charset [ i ] -- ; return char ( i + ' a ' ) ; } } return ' \0' ; } string newString ( string originalLabel , int limit ) { int n = originalLabel . length ( ) ; vector < int > charset ( 26 , 0 ) ; string newStrings = " " ; for ( char i : originalLabel ) charset [ i - ' a ' ] ++ ; for ( int i = 25 ; i >= 0 ; i -- ) { int count = 0 ; while ( charset [ i ] > 0 ) { newStrings += char ( i + ' a ' ) ; charset [ i ] -- ; count ++ ; if ( charset [ i ] > 0 && count == limit ) { char next = nextAvailableChar ( charset , i ) ; if ( next == ' \0' ) return newStrings ; newStrings += next ; count = 0 ; } } } return newStrings ; } int main ( ) { string S = " ccbbb " ; int K = 2 ; cout << ( newString ( S , K ) ) ; }
Rearrange array by interchanging positions of even and odd elements in the given array | C ++ program for the above approach ; Function to replace odd elements with even elements and vice versa ; Traverse the given array ; If arr [ i ] is visited ; Find the next odd element ; Find next even element ; Mark them visited ; Swap them ; Print the final array ; Driver Code ; Given array arr [ ] ; Length of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  4 NEW_LINE void swapEvenOdd ( int arr [ ] , int n ) { int o = -1 , e = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) continue ; int r = -1 ; if ( arr [ i ] % 2 == 0 ) { o ++ ; while ( arr [ o ] % 2 == 0 arr [ o ] < 0 ) o ++ ; r = o ; } else { e ++ ; while ( arr [ e ] % 2 == 1 arr [ e ] < 0 ) e ++ ; r = e ; } arr [ i ] *= -1 ; arr [ r ] *= -1 ; int tmp = arr [ i ] ; arr [ i ] = arr [ r ] ; arr [ r ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ( -1 * arr [ i ] ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; swapEvenOdd ( arr , n ) ; }
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | C ++ program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string rev ( string str ) { int st = 0 ; int ed = str . length ( ) - 1 ; string s = str ; while ( st < ed ) { swap ( s [ st ] , s [ ed ] ) ; st ++ ; ed -- ; } return s ; } bool check ( string a , string b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a [ i ] != b [ j ] ) break ; i += 1 ; j -= 1 ; string xa = a . substr ( i , j + 1 - i ) ; string xb = b . substr ( i , j + 1 - i ) ; if ( ( xa == rev ( xa ) ) or ( xb == rev ( xb ) ) ) return true ; } return false ; } int main ( ) { string a = " xbdef " ; string b = " cabex " ; if ( check ( a , b , a . length ( ) ) == true or check ( b , a , a . length ( ) ) == true ) cout << " True " ; else cout << " False " ; }
Maximize length of subarray having equal elements by adding at most K | C ++ 14 program for above approach ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < int > pSum , int len , int k , vector < int > a ) { int i = 0 ; int j = len ; while ( j <= a . size ( ) ) { int maxSize = a [ j - 1 ] ; int totalNumbers = maxSize * len ; int currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } int maxEqualIdx ( vector < int > arr , int k ) { sort ( arr . begin ( ) , arr . end ( ) ) ; vector < int > prefixSum ( arr . size ( ) ) ; prefixSum [ 1 ] = arr [ 0 ] ; for ( int i = 1 ; i < prefixSum . size ( ) - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } int max = arr . size ( ) ; int min = 1 ; int ans = 1 ; while ( min <= max ) { int mid = ( max + min ) / 2 ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } int main ( ) { vector < int > arr = { 1 , 1 , 1 } ; int k = 7 ; cout << ( maxEqualIdx ( arr , k ) ) ; }
Sum of product of all pairs of a Binary Array | C ++ program to implement the above approach ; Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code ; Stores the size of the given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int productSum ( int arr [ ] , int N ) { int cntOne = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << productSum ( arr , n ) << endl ; }
Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | C ++ program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Find the number of digits ; Calculate sum of the digits ; Add the digits of num2 ; If the sum of the digits of N is less than or equal to K ; No number needs to be added ; Otherwise ; Calculate the sum of digits from least significant digit ; If sum exceeds K ; Increase previous digit by 1 ; Add zeros to the end ; Calculate difference between the result and N ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDigits ( int N , int K ) { int digits_num = floor ( log10 ( N ) + 1 ) ; int temp_sum = 0 ; int temp = digits_num ; int result ; int X , var ; int sum = 0 ; int num2 = N ; while ( num2 != 0 ) { sum += num2 % 10 ; num2 /= 10 ; } if ( sum <= K ) { X = 0 ; } else { while ( temp > 0 ) { var = ( N / ( pow ( 10 , temp - 1 ) ) ) ; temp_sum += var % 10 ; if ( temp_sum >= K ) { var /= 10 ; var ++ ; result = var * pow ( 10 , temp ) ; break ; } temp -- ; } X = result - N ; return X ; } } int main ( ) { int N = 11 , K = 1 ; cout << minDigits ( N , K ) ; return 0 ; }
Count of Ways to obtain given Sum from the given Array elements | C ++ Program to implement the above approach ; Function to perform the DFS to calculate the number of ways ; Base case : Reached the end of array ; If current sum is obtained ; Otherwise ; If previously calculated subproblem occurred ; Check if the required sum can be obtained by adding current element or by subtracting the current index element ; Store the count of ways ; Function to call dfs to calculate the number of ways ; Iterate till the length of array ; Initialize the memorization table ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dfs ( vector < vector < int > > memo , int nums [ ] , int S , int curr_sum , int index , int sum , int N ) { if ( index == N ) { if ( S == curr_sum ) return 1 ; else return 0 ; } if ( memo [ index ] [ curr_sum + sum ] != INT_MIN ) { return memo [ index ] [ curr_sum + sum ] ; } int ans = dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum , N ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum , N ) ; memo [ index ] [ curr_sum + sum ] = ans ; return ans ; } int findWays ( int nums [ ] , int S , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += nums [ i ] ; vector < vector < int > > memo ( N + 1 , vector < int > ( 2 * sum + 1 , INT_MIN ) ) ; return dfs ( memo , nums , S , 0 , 0 , sum , N ) ; } int main ( ) { int S = 3 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int answer = findWays ( arr , S , N ) ; cout << answer << endl ; return 0 ; }
Longest subarray with sum not divisible by X | C ++ Program to implement the above approach ; Function to print the longest subarray with sum of elements not divisible by X ; Pref [ ] stores the prefix sum Suff [ ] stores the suffix sum ; If array element is divisibile by x ; Increase count ; If all the array elements are divisible by x ; No subarray possible ; Reverse v to calculate the suffix sum ; Calculate the suffix sum ; Reverse to original form ; Reverse the suffix sum array ; Calculate the prefix sum ; Stores the starting index of required subarray ; Stores the ending index of required subarray ; If suffix sum till i - th index is not divisible by x ; Update the answer ; If prefix sum till i - th index is not divisible by x ; Update the answer ; Print the longest subarray ; Driver Code
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; void max_length ( int N , int x , vector < int > & v ) { int i , a ; vector < int > preff , suff ; int ct = 0 ; for ( i = 0 ; i < N ; i ++ ) { a = v [ i ] ; if ( a % x == 0 ) { ct += 1 ; } } if ( ct == N ) { cout << -1 << endl ; return ; } reverse ( v . begin ( ) , v . end ( ) ) ; suff . push_back ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { suff . push_back ( v [ i ] + suff [ i - 1 ] ) ; } reverse ( v . begin ( ) , v . end ( ) ) ; reverse ( suff . begin ( ) , suff . end ( ) ) ; preff . push_back ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { preff . push_back ( v [ i ] + preff [ i - 1 ] ) ; } int ans = 0 ; int lp = 0 ; int rp = N - 1 ; for ( i = 0 ; i < N ; i ++ ) { if ( suff [ i ] % x != 0 && ( ans < ( N - 1 ) ) ) { lp = i ; rp = N - 1 ; ans = max ( ans , N - i ) ; } if ( preff [ i ] % x != 0 && ( ans < ( i + 1 ) ) ) { lp = 0 ; rp = i ; ans = max ( ans , i + 1 ) ; } } for ( i = lp ; i <= rp ; i ++ ) { cout << v [ i ] << " ▁ " ; } } int main ( ) { int x = 3 ; vector < int > v = { D2 : V21 , 3 , 2 , 6 } ; int N = v . size ( ) ; max_length ( N , x , v ) ; return 0 ; }
Number of times Maximum and minimum value updated during traversal of array | C ++ implementation to find the number of times minimum and maximum value updated during the traversal of the array ; Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxUpdated ( vector < int > arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; for ( auto n : arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } cout << " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ; cout << " updated ▁ = ▁ " << i << endl ; cout << " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ; cout << " updated ▁ = ▁ " << j << endl ; } int main ( ) { vector < int > arr ( { 10 , 5 , 20 , 22 } ) ; maxUpdated ( arr ) ; }
Find the farthest smaller number in the right side | C ++ implementation of the approach ; Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void farthest_min ( int a [ ] , int n ) { int suffix_min [ n ] ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int low = i + 1 , high = n - 1 , ans = -1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } cout << ans << " ▁ " ; } } int main ( ) { int a [ ] = { 3 , 1 , 5 , 2 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; farthest_min ( a , n ) ; return 0 ; }
Print numbers in descending order along with their frequencies | C ++ program to print the elements in descending along with their frequencies ; Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printElements ( int a [ ] , int n ) { sort ( a , a + n , greater < int > ( ) ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { cout << a [ i ] << " ▁ occurs ▁ " << cnt << " ▁ times STRNEWLINE " ; cnt = 1 ; } else cnt += 1 ; } cout << a [ n - 1 ] << " ▁ occurs ▁ " << cnt << " ▁ times STRNEWLINE " ; } int main ( ) { int a [ ] = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printElements ( a , n ) ; return 0 ; }