codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all second largest divisors after splitting a number into one or more parts | CPP program to find sum of all second largest divisor after splitting a number into one or more parts ; Function to find a number is prime or not ; If there is any divisor ; Function to find the sum of all second largest divisor after splitting a number into one or more parts ; If number is prime ; If n is even ; If the number is odd ; If N - 2 is prime ; There exists 3 primes x1 , x2 , x3 such that x1 + x2 + x3 = n ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } int main ( ) { int n = 27 ; cout << Min_Sum ( n ) ; return 0 ; }
Count pairs of strings that satisfy the given conditions | C ++ implementation of the approach ; Function that returns true if c is vowel ; Function to return the count of required pairs ; For every string of the array ; Vector to store the vowels of the current string ; If current character is a vowel ; If current string contains vowels ; Create tuple ( first vowel , last vowel , total vowels ) ; v stores the indices for which the given condition satisfies Total valid pairs will be half the size ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_vowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } int count ( string s [ ] , int n ) { map < tuple < char , char , int > , vector < int > > map ; for ( int i = 0 ; i < n ; i ++ ) { vector < char > vowel ; for ( int j = 0 ; j < s [ i ] . size ( ) ; j ++ ) { if ( is_vowel ( s [ i ] [ j ] ) ) vowel . push_back ( s [ i ] [ j ] ) ; } if ( vowel . size ( ) > 0 ) { int len = vowel . size ( ) ; map [ make_tuple ( vowel [ 0 ] , vowel [ len - 1 ] , len ) ] . push_back ( i ) ; } } int count = 0 ; for ( auto i : map ) { vector < int > v = i . second ; count += v . size ( ) / 2 ; } return count ; } int main ( ) { string s [ ] = { " geeks " , " for " , " geeks " } ; int n = sizeof ( s ) / sizeof ( string ) ; cout << count ( s , n ) ; return 0 ; }
Find maximum value of the last element after reducing the array with given operations | C ++ implementation of the approach ; Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_maximum_value ( int a [ ] , int n ) { int sum = 0 ; int minimum = INT_MAX ; int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { minimum = min ( minimum , abs ( a [ i ] ) ) ; sum += abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } int main ( ) { int a [ ] = { 5 , 4 , 6 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << find_maximum_value ( a , n ) ; return 0 ; }
Find Nth smallest number that is divisible by 100 exactly K times | C ++ implementation of above approach ; Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string find_number ( int N , int K ) { string r ; if ( N % 100 == 0 ) { N += 1 ; r = to_string ( N ) ; } else { r = to_string ( N ) ; } for ( int i = 1 ; i <= K ; i ++ ) r += "00" ; return r ; } int main ( ) { int N = 1000 , K = 2 ; string ans = find_number ( N , K ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Count of adjacent Vowel Consonant Pairs | C ++ Program to implement the above approach ; Function to count the adjacent pairs of consonant and vowels in the string ; Using a set to store the vowels so that checking each character becomes easier ; Variable to store number of consonant - vowel pairs ; If the ith character is not found in the set , means it is a consonant And if the ( i + 1 ) th character is found in the set , means it is a vowel We increment the count of such pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( string s ) { set < char > st ; st . insert ( ' a ' ) ; st . insert ( ' e ' ) ; st . insert ( ' i ' ) ; st . insert ( ' o ' ) ; st . insert ( ' u ' ) ; int count = 0 ; int n = s . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( st . find ( s [ i ] ) == st . end ( ) && st . find ( s [ i + 1 ] ) != st . end ( ) ) count ++ ; } return count ; } int main ( ) { string s = " geeksforgeeks " ; cout << countPairs ( s ) ; return 0 ; }
Find the original coordinates whose Manhattan distances are given | C ++ implementation of the approach ; Function to find the original coordinated ; Maximum of the given distances ; Sum of the given distances ; Conditions when the solution doesn 't exist ; First coordinate ; Second coordinate ; Third coordinate ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int d1 , int d2 , int d3 ) { int maxx = max ( d1 , max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum or sum % 2 == 1 ) { cout << " - 1" ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; cout << " ( " << x1 << " , ▁ " << y1 << " ) , ▁ ( " << x2 << " , ▁ " << y2 << " ) ▁ and ▁ ( " << x3 << " , ▁ " << y3 << " ) " ; } int main ( ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; return 0 ; }
Find the largest interval that contains exactly one of the given N integers . | C ++ implementation of the approach ; Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSize ( vector < int > & v , int n ) { v . push_back ( 0 ) ; v . push_back ( 100001 ) ; n += 2 ; sort ( v . begin ( ) , v . end ( ) ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v [ i - 1 ] + 1 ; int R = v [ i + 1 ] - 1 ; int cnt = R - L + 1 ; mx = max ( mx , cnt ) ; } return mx ; } int main ( ) { vector < int > v = { 200 , 10 , 5 } ; int n = v . size ( ) ; cout << maxSize ( v , n ) ; return 0 ; }
Maximum length of subarray such that sum of the subarray is even | C ++ implementation of the above approach ; Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( int a [ ] , int n ) { int sum = 0 , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return n ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) len = max ( len , max ( n - i - 1 , i ) ) ; } return len ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxLength ( a , n ) << " STRNEWLINE " ; return 0 ; }
Find a Symmetric matrix of order N that contain integers from 0 to N | C ++ implementation of the approach ; Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( long long n ) { long long initial_array [ n - 1 ] [ n - 1 ] , final_array [ n ] [ n ] ; for ( long long i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ i ] = i + 1 ; for ( long long i = 1 ; i < n - 1 ; ++ i ) for ( long long j = 0 ; j < n - 1 ; ++ j ) initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] ; for ( long long i = 0 ; i < n - 1 ; ++ i ) for ( long long j = 0 ; j < n - 1 ; ++ j ) final_array [ i ] [ j ] = initial_array [ i ] [ j ] ; for ( long long i = 0 ; i < n ; ++ i ) final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 ; for ( long long i = 0 ; i < n ; ++ i ) { long long t0 = final_array [ i ] [ i ] ; long long t1 = final_array [ i ] [ n - 1 ] ; swap ( final_array [ i ] [ i ] , final_array [ i ] [ n - 1 ] ) ; final_array [ n - 1 ] [ i ] = t0 ; } for ( long long i = 0 ; i < n ; ++ i ) { for ( long long j = 0 ; j < n ; ++ j ) cout << final_array [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { long long n = 5 ; solve ( n ) ; return 0 ; }
Generate original array from difference between every two consecutive elements | C ++ implementation of the approach ; Function to print the required permutation ; Take x = 0 for simplicity ; Calculate aint the differences and store it in a vector ; Preserve the original array ; Check if aint the consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPerm ( int n , vector < int > & differences ) { vector < int > ans ; ans . clear ( ) ; ans . push_back ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences [ i ] ; x = x + diff ; ans . push_back ( x ) ; } vector < int > anss = ans ; sort ( ans . begin ( ) , ans . end ( ) ) ; int flag = -1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { cout << -1 ; return ; } else { unordered_map < int , int > mpp ; mpp . clear ( ) ; int j = 1 ; vector < int > value_at_index ; for ( auto & x : ans ) { mpp [ x ] = j ; ++ j ; } for ( auto & x : anss ) { value_at_index . push_back ( mpp [ x ] ) ; } for ( auto & x : value_at_index ) { cout << x << " ▁ " ; } cout << endl ; } } int main ( ) { vector < int > differences ; differences . push_back ( 2 ) ; differences . push_back ( -3 ) ; differences . push_back ( 2 ) ; int n = differences . size ( ) + 1 ; findPerm ( n , differences ) ; return 0 ; }
Find smallest number K such that K % p = 0 and q % K = 0 | C ++ implementation of the approach ; Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinVal ( int p , int q ) { if ( q % p == 0 ) return p ; return -1 ; } int main ( ) { int p = 24 , q = 48 ; cout << getMinVal ( p , q ) ; return 0 ; }
Sort the numbers according to their sum of digits | C ++ implementation of the approach ; Function to return the sum of the digits of n ; Function to sort the array according to the sum of the digits of elements ; Vector to store digit sum with respective element ; Inserting digit sum with element in the vector pair ; Quick sort the vector , this will sort the pair according to sum of the digits of elements ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigit ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n = n / 10 ; } return sum ; } void sortArr ( int arr [ ] , int n ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( sumOfDigit ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { int arr [ ] = { 14 , 1101 , 10 , 35 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Count all Prime Length Palindromic Substrings | C ++ implementation of the approach ; Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } int countPrimePalindrome ( string str , int len ) { bool prime [ len + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( int i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } int main ( ) { string s = " geeksforgeeks " ; int len = s . length ( ) ; cout << countPrimePalindrome ( s , len ) ; return 0 ; }
Minimum operations of the given type required to make a complete graph | C ++ implementation of the approach ; Function to return the minimum number of steps required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int N ) { double x = log2 ( N ) ; int ans = ceil ( x ) ; return ans ; } int main ( ) { int N = 10 ; cout << minOperations ( N ) ; return 0 ; }
Greatest divisor which divides all natural number in range [ L , R ] | C ++ implementation of the approach ; Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_greatest_divisor ( int l , int r ) { if ( l == r ) return l ; return 1 ; } int main ( ) { int l = 2 , r = 12 ; cout << find_greatest_divisor ( l , r ) ; }
Probability of getting two consecutive heads after choosing a random coin among two different types of coins | C ++ program to get the probability of getting two consecutive heads ; Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; Driver code ; given the probability of getting a head for both the coins
#include <bits/stdc++.h> NEW_LINE using namespace std ; double getProbability ( double p , double q ) { p /= 100 ; q /= 100 ; double probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } int main ( ) { double p , q ; p = 80 ; q = 40 ; cout << fixed << setprecision ( 15 ) << getProbability ( p , q ) << endl ; return 0 ; }
Check whether bitwise OR of N numbers is Even or Odd | C ++ implementation to check whether bitwise OR of n numbers is even or odd ; Function to check if bitwise OR of n numbers is even or odd ; if at least one odd number is found , then the bitwise OR of all numbers will be odd ; Bitwise OR is an odd number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) return true ; } return false ; } int main ( ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( check ( arr , n ) ) cout << " Odd ▁ Bit - wise ▁ OR " ; else cout << " Even ▁ Bit - wise ▁ OR " ; return 0 ; }
Amortized analysis for increment in counter |
#include <bits / stdc++.h> NEW_LINE using namespace std ; int main ( ) { char str [ ] = "10010111" ; int length = strlen ( str ) ; int i = length - 1 ; while ( str [ i ] == '1' ) { str [ i ] = '0' ; i -- ; } if ( i >= 0 ) str [ i ] = '1' ; printf ( " % ▁ s " , str ) ; }
Time taken by Loop unrolling vs Normal loop | CPP program to compare normal loops and loops with unrolling technique ; n is 8 lakhs ; t to note start time ; to store the sum ; Normal loop ; to mark end time ; to mark start time of unrolling ; Unrolling technique ( assuming that n is a multiple of 8 ) . ; to mark the end of loop
#include <iostream> NEW_LINE #include <time.h> NEW_LINE using namespace std ; int main ( ) { int n = 800000 ; clock_t t = clock ( ) ; long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += i ; t = clock ( ) - t ; cout << " sum ▁ is : ▁ " << sum << endl ; cout << " time ▁ taken ▁ by ▁ normal ▁ loops : " " ( float ) t ▁ / ▁ CLOCKS _ PER _ SEC ▁ < < ▁ " seconds " << endl ; t = clock ( ) ; sum = 0 ; for ( int i = 1 ; i <= n ; i += 8 ) { sum += i sum += ( i + 1 ) ; sum += ( i + 2 ) ; sum += ( i + 3 ) ; sum += ( i + 4 ) ; sum += ( i + 5 ) ; sum += ( i + 6 ) ; sum += ( i + 7 ) ; } t = clock ( ) - t ; cout << " Sum ▁ is : ▁ " << sum << endl ; cout << " Time ▁ taken ▁ by ▁ unrolling : ▁ " " ( float ) t ▁ / ▁ CLOCKS _ PER _ SEC ▁ < < ▁ " seconds " return 0 ; }
Iterated Logarithm log * ( n ) | Recursive CPP program to find value of Iterated Logarithm ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int _log ( double x , double base ) { return ( int ) ( log ( x ) / log ( base ) ) ; } double recursiveLogStar ( double n , double b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } int main ( ) { int n = 100 , base = 5 ; cout << " Log * ( " << n << " ) ▁ = ▁ " << recursiveLogStar ( n , base ) << " STRNEWLINE " ; return 0 ; }
Iterated Logarithm log * ( n ) | Iterative CPP function to find value of Iterated Logarithm
int iterativeLogStar ( double n , double b ) { int count = 0 ; while ( n >= 1 ) { n = _log ( n , b ) ; count ++ ; } return count ; }
Analysis of Algorithms | Set 5 ( Practice Problems ) |
void function ( int n ) { int count = 0 ; for ( int i = n / 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j = 2 * j ) for ( int k = 1 ; k <= n ; k = k * 2 ) count ++ ; }
Auxiliary Space with Recursive Functions |
int sum ( int sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }
Find the maximum among the count of positive or negative integers in the array | C ++ program for the above approach ; Function to find the maximum of the count of positive or negative elements ; Initialize the pointers ; Find the value of mid ; If element is negative then ignore the left half ; If element is positive then ignore the right half ; Return maximum among the count of positive & negative element ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int findMaximum ( int arr [ ] , int size ) { int i = 0 , j = size - 1 , mid ; while ( i <= j ) { mid = i + ( j - i ) / 2 ; if ( arr [ mid ] < 0 ) i = mid + 1 ; else if ( arr [ mid ] > 0 ) j = mid - 1 ; } return max ( i , size - i ) ; } int main ( ) { int arr [ ] = { -9 , -7 , -4 , 1 , 5 , 8 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaximum ( arr , N ) ; return 0 ; }
Perform given queries on Queue according to the given rules | C ++ program for the above approach ; Function to perform all the queries operations on the given queue ; Stores the count query of type 1 ; Event E1 : increase countE1 ; Event E2 : add the X in set ; Event E3 : Find position of X ; Initial position is ( position - countE1 ) ; If X is already removed or popped out ; Finding the position of X in queue ; Traverse set to decrease position of X for all the number removed in front ; Print the position of X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n , int m , int * * queries ) { int countE1 = 0 ; set < int > removed ; for ( int i = 0 ; i < m ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) ++ countE1 ; else if ( queries [ i ] [ 0 ] == 2 ) removed . insert ( queries [ i ] [ 1 ] ) ; else { int position = queries [ i ] [ 1 ] - countE1 ; if ( removed . find ( queries [ i ] [ 1 ] ) != removed . end ( ) position <= 0 ) cout << " - 1 STRNEWLINE " ; else { for ( int it : removed ) { if ( it > queries [ i ] [ 1 ] ) break ; position -- ; } cout << position << " STRNEWLINE " ; } } } } int main ( ) { int N = 5 , Q = 3 ; int * * queries = new int * [ Q ] ; for ( int i = 0 ; i < Q ; i ++ ) { queries [ i ] = new int [ 2 ] ; } queries [ 0 ] [ 0 ] = 1 ; queries [ 0 ] [ 1 ] = 0 ; queries [ 1 ] [ 0 ] = 3 ; queries [ 1 ] [ 1 ] = 3 ; queries [ 2 ] [ 0 ] = 2 ; queries [ 2 ] [ 1 ] = 2 ; solve ( N , Q , queries ) ; return 0 ; }
Maximum index a pointer can reach in N steps by avoiding a given index B | C ++ program for the above approach ; Function to find the maximum index reachable ; Store the answer ; Store the maximum index possible i . e , N * ( N - 1 ) ; Add bthe previous elements ; Run a loop ; Binary Search ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxIndex ( int N , int B ) { int maximumIndexReached = 0 ; vector < int > Ans ; for ( int i = 1 ; i <= N ; i ++ ) { maximumIndexReached += i ; Ans . push_back ( i ) ; } reverse ( Ans . begin ( ) , Ans . end ( ) ) ; for ( int i = 1 ; i < ( int ) Ans . size ( ) ; i ++ ) { Ans [ i ] += Ans [ i - 1 ] ; } while ( maximumIndexReached ) { auto it = lower_bound ( Ans . begin ( ) , Ans . end ( ) , maximumIndexReached - B ) ; if ( it == Ans . end ( ) ) { break ; } if ( * it == maximumIndexReached - B ) { maximumIndexReached -- ; } else { break ; } } return maximumIndexReached ; } int main ( ) { int N = 3 , B = 2 ; cout << maxIndex ( N , B ) ; return 0 ; }
Maximize Kth largest element after splitting the given Array at most C times | C ++ program for the above approach ; Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double maxKth ( int arr [ ] , int N , int C , int K ) { if ( N + C < K ) { return -1 ; } int iter = 300 ; double l = 0 , r = 1000000000.0 ; while ( iter -- ) { double mid = ( l + r ) * 0.5 ; double a = 0 ; double b = 0 ; for ( int i = 0 ; i < N ; i ++ ) { a += int ( ( double ) arr [ i ] / mid ) ; if ( ( double ) arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } int main ( ) { int arr [ ] = { 5 , 8 } ; int K = 1 , C = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxKth ( arr , N , C , K ) ; return 0 ; }