codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find the minimum index in given array having at least value X | C ++ program to implement the above approach ; Function to find the smallest index of an array element whose value is less than or equal to Q [ i ] ; Stores size of array ; Stores coun of queries ; Store array elements along with the index ; Store smallest index of an array element whose value is greater than or equal to i ; Traverse the array ; Insert { arr [ i ] , i } into storeArrIdx [ ] ; Sort the array ; Sort the storeArrIdx ; Stores index of arr [ N - 1 ] in sorted order ; Traverse the array storeArrIdx [ ] ; Update minIdx [ i ] ; Traverse the array Q [ ] ; Store the index of lower_bound of Q [ i ] ; If no index found whose value greater than or equal to arr [ i ] ; Print smallest index whose value greater than or equal to Q [ i ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumIndex ( vector < int > & arr , vector < int > & Q ) { int N = arr . size ( ) ; int M = Q . size ( ) ; vector < pair < int , int > > storeArrIdx ; vector < int > minIdx ( N ) ; for ( int i = 0 ; i < N ; ++ i ) { storeArrIdx . push_back ( { arr [ i ] , i } ) ; } sort ( arr . begin ( ) , arr . end ( ) ) ; sort ( storeArrIdx . begin ( ) , storeArrIdx . end ( ) ) ; minIdx [ N - 1 ] = storeArrIdx [ N - 1 ] . second ; for ( int i = N - 2 ; i >= 0 ; i -- ) { minIdx [ i ] = min ( minIdx [ i + 1 ] , storeArrIdx [ i ] . second ) ; } for ( int i = 0 ; i < M ; i ++ ) { int pos = lower_bound ( arr . begin ( ) , arr . end ( ) , Q [ i ] ) - arr . begin ( ) ; if ( pos == N ) { cout << -1 << " ▁ " ; continue ; } cout << minIdx [ pos ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 1 , 9 } ; vector < int > Q = { 7 , 10 , 0 } ; minimumIndex ( arr , Q ) ; return 0 ; }
Find a K | C ++ program to implement the above approach ; Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Driver Code ; Given array ; Size of the array ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSubarrayExistUtil ( int arr [ ] , int K , int N ) { int totalXOR = 0 ; int SubarrayXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) totalXOR ^= arr [ i ] ; for ( int i = 0 ; i < K ; i ++ ) SubarrayXOR ^= arr [ i ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; for ( int i = K ; i < N ; i ++ ) { SubarrayXOR ^= arr [ i ] ; SubarrayXOR ^= arr [ i - 1 ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; } return false ; } void isSubarrayExist ( int arr [ ] , int K , int N ) { if ( isSubarrayExistUtil ( arr , K , N ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; } int32_t main ( ) { int arr [ ] = { 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; isSubarrayExist ( arr , K , N ) ; }
Count pairs from a given range having even sum | C ++ program for the above approach ; Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Driver Code ; Given L & R ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int L , int R ) { int count_even ; if ( L % 2 == 0 ) { count_even = ( R / 2 ) - ( L / 2 ) + 1 ; } else { count_even = ( R / 2 ) - ( L / 2 ) ; } int count_odd ; if ( L % 2 == 0 ) { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) ; } else { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) + 1 ; } count_even *= count_even ; count_odd *= count_odd ; cout << count_even + count_odd ; } int main ( ) { int L = 1 , R = 3 ; countPairs ( L , R ) ; return 0 ; }
Count of array elements that can be found using Randomized Binary Search on every array element | C ++ program for the above approach ; Function to find minimum count of array elements found by repeatedly applying Randomized Binary Search ; Stores count of array elements ; smallestRight [ i ] : Stores the smallest array element on the right side of i ; Update smallestRight [ 0 ] ; Traverse the array from right to left ; Update smallestRight [ i ] ; Stores the largest element upto i - th index ; Stores the minimum count of elements found by repeatedly applying Randomized Binary Search ; If largest element on left side is less than smallest element on right side ; Update ans ; Update mn ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getDefiniteFinds ( vector < int > & arr ) { int n = arr . size ( ) ; vector < int > smallestRight ( n + 1 ) ; smallestRight [ n ] = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smallestRight [ i ] = min ( smallestRight [ i + 1 ] , arr [ i ] ) ; } int mn = INT_MIN ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mn < arr [ i ] and arr [ i ] < smallestRight [ i + 1 ] ) { ans ++ ; } mn = max ( arr [ i ] , mn ) ; } return ans ; } int main ( ) { vector < int > arr = { 5 , 4 , 9 } ; cout << getDefiniteFinds ( arr ) << endl ; }
Count numbers from a given range having same first and last digits in their binary representation | C ++ program for the above approach ; Function to count numbers in range [ L , R ] having same first and last digit in the binary representation ; Drivers code ; Given range [ L , R ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Count_numbers ( int L , int R ) { int count = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) count += 1 ; cout << count << endl ; } int main ( ) { int L = 6 , R = 30 ; Count_numbers ( L , R ) ; }
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | C ++ Program to implement the above approach ; Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkReverse ( int leftSum , int rightSum ) { int rev = 0 ; int temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp /= 10 ; } if ( rev == leftSum ) { return true ; } return false ; } int findIndex ( int arr [ ] , int N ) { int rightSum = 0 ; int leftSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return -1 ; } int main ( ) { int arr [ ] = { 5 , 7 , 3 , 6 , 4 , 9 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findIndex ( arr , N ) ; return 0 ; }
Print distinct absolute differences of all possible pairs from a given array | C ++ program for the above approach ; Function to find all distinct absolute difference of all possible pairs of the array ; bset [ i ] : Check if i is present in the array or not ; diff [ i ] : Check if there exists a pair whose absolute difference is i ; Traverse the array , arr [ ] ; Add in bitset ; Iterate over the range [ 0 , Max ] ; If i - th bit is set ; Insert the absolute difference of all possible pairs whose first element is arr [ i ] ; Stores count of set bits ; If there is at least one duplicate element in arr [ ] ; Printing the distinct absolute differences of all possible pairs ; If i - th bit is set ; Driver Code ; Given array ; Given size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define Max  100005 NEW_LINE void printUniqDif ( int n , int a [ ] ) { bitset < Max > bset ; bitset < Max > diff ; for ( int i = 0 ; i < n ; i ++ ) { bset . set ( a [ i ] ) ; } for ( int i = 0 ; i <= Max ; i ++ ) { if ( bset [ i ] ) { diff = diff | ( bset >> i ) ; } } int X = bset . count ( ) ; if ( X != n ) { cout << 0 << " ▁ " ; } for ( int i = 1 ; i <= Max ; i ++ ) { if ( diff [ i ] ) { cout << i << " ▁ " ; } } } int main ( ) { int a [ ] = { 1 , 4 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printUniqDif ( n , a ) ; return 0 ; }
Longest Common Subsequence of two arrays out of which one array consists of distinct elements only | C ++ program to to implement the above approach ; Function to find the Longest Common Subsequence between the two arrays ; Maps elements of firstArr [ ] to their respective indices ; Traverse the array firstArr [ ] ; Stores the indices of common elements between firstArr [ ] and secondArr [ ] ; Traverse the array secondArr [ ] ; If current element exists in the Map ; Stores lIS from tempArr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCS ( vector < int > & firstArr , vector < int > & secondArr ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < firstArr . size ( ) ; i ++ ) { mp [ firstArr [ i ] ] = i + 1 ; } vector < int > tempArr ; for ( int i = 0 ; i < secondArr . size ( ) ; i ++ ) { if ( mp . find ( secondArr [ i ] ) != mp . end ( ) ) { tempArr . push_back ( mp [ secondArr [ i ] ] ) ; } } vector < int > tail ; tail . push_back ( tempArr [ 0 ] ) ; for ( int i = 1 ; i < tempArr . size ( ) ; i ++ ) { if ( tempArr [ i ] > tail . back ( ) ) tail . push_back ( tempArr [ i ] ) ; else if ( tempArr [ i ] < tail [ 0 ] ) tail [ 0 ] = tempArr [ i ] ; else { auto it = lower_bound ( tail . begin ( ) , tail . end ( ) , tempArr [ i ] ) ; * it = tempArr [ i ] ; } } return ( int ) tail . size ( ) ; } int main ( ) { vector < int > firstArr = { 3 , 5 , 1 , 8 } ; vector < int > secondArr = { 3 , 3 , 5 , 3 , 8 } ; cout << LCS ( firstArr , secondArr ) ; return 0 ; }
Count ways to partition a Binary String such that each substring contains exactly two 0 s | C ++ program for the above approach ; Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalWays ( int n , string str ) { vector < int > IdxOf0s ; int cntWays = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { IdxOf0s . push_back ( i ) ; } } int M = IdxOf0s . size ( ) ; if ( M == 0 or M % 2 ) { return 0 ; } for ( int i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( IdxOf0s [ i ] - IdxOf0s [ i - 1 ] ) ; } return cntWays ; } int main ( ) { string str = "00100" ; int n = str . length ( ) ; cout << totalWays ( n , str ) ; return 0 ; }
Count non | C ++ program to implement the above approach ; Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int * arr , int N ) { vector < int > zero_i ; vector < int > one_i ; unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . push_back ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . push_back ( i + 1 ) ; else mp [ i + 1 ] = 1 ; } int total = zero_i . size ( ) * one_i . size ( ) * mp . size ( ) ; for ( int i = 0 ; i < zero_i . size ( ) ; i ++ ) { for ( int j = 0 ; j < one_i . size ( ) ; j ++ ) { int p = zero_i [ i ] ; int q = one_i [ j ] ; int r = 2 * p - q ; if ( mp [ r ] > 0 ) total -- ; r = 2 * q - p ; if ( mp [ r ] > 0 ) total -- ; r = ( p + q ) / 2 ; if ( mp [ r ] > 0 && abs ( r - p ) == abs ( r - q ) ) total -- ; } } cout << total ; } int main ( ) { int arr [ ] = { 0 , 1 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countTriplets ( arr , N ) ; return 0 ; }
Print prime factors of a given integer in decreasing order using Stack | C ++ program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrimeFactors ( int N ) { stack < int > st ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( ! st . empty ( ) ) { printf ( " % d ▁ " , st . top ( ) ) ; st . pop ( ) ; } } int main ( ) { int N = 8 ; PrimeFactors ( N ) ; return 0 ; }
Count subarrays consisting of first K natural numbers in descending order | C ++ program for the above approach ; Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountSubarray ( int arr [ ] , int n , int k ) { int temp = k , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == temp ) { if ( temp == 1 ) { count ++ ; temp = k ; } else temp -- ; } else { temp = k ; if ( arr [ i ] == k ) i -- ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << CountSubarray ( arr , N , K ) ; return 0 ; }
Smallest number not less than N which is divisible by all digits of N | C ++ program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int LCM ( int A , int B ) { return ( A * B / __gcd ( A , B ) ) ; } int findSmallestNumber ( int X ) { int lcm = 1 ; int temp = X ; while ( temp ) { int last = temp % 10 ; temp /= 10 ; if ( ! last ) continue ; lcm = LCM ( lcm , last ) ; } int answer = ( ( X + lcm - 1 ) / lcm ) * lcm ; cout << answer ; } int main ( ) { int X = 280 ; findSmallestNumber ( X ) ; return 0 ; }
Minimum sum of two integers whose product is strictly greater than N | C ++ program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Initialise low as 0 and high as 1e9 ; Iterate to find the first number ; Find the middle value ; If mid ^ 2 is greater than equal to A , then update high to mid ; Otherwise update low ; Store the first number ; Again , set low as 0 and high as 1e9 ; Iterate to find the second number ; Find the middle value ; If first number * mid is greater than N then update high to mid ; Else , update low to mid ; Store the second number ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE void minSum ( int N ) { ll low = 0 , high = 1e9 ; while ( low + 1 < high ) { ll mid = low + ( high - low ) / 2 ; if ( mid * mid >= N ) { high = mid ; } else { low = mid ; } } ll first = high ; low = 0 ; high = 1e9 ; while ( low + 1 < high ) { ll mid = low + ( high - low ) / 2 ; if ( first * mid > N ) { high = mid ; } else { low = mid ; } } ll second = high ; cout << first + second ; } int main ( ) { int N = 10 ; minSum ( N ) ; return 0 ; }
Minimum sum of two integers whose product is strictly greater than N | C ++ program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Store the answer using the AP - GP inequality ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minSum ( int N ) { int ans = ceil ( 2 * sqrt ( N + 1 ) ) ; cout << ans ; } int main ( ) { int N = 10 ; minSum ( N ) ; return 0 ; }
Query to find length of the longest subarray consisting only of 1 s | C ++ Program for the above approach ; Arrays to store prefix sums , suffix and MAX 's node respectively ; Function to construct Segment Tree ; MAX array for node MAX ; Array for prefix sum node ; Array for suffix sum node ; Calculate MAX node ; Function to update Segment Tree ; Update at position ; Update sums from bottom to the top of Segment Tree ; Update MAX tree ; Update pref tree ; Update suf tree ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Print longest length of subarray in [ 1 , N ] ; Flip the character ; Driver Code ; Size of array ; Given array ; Given queries ; Number of queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  1000000 NEW_LINE int pref [ 500005 ] ; int suf [ 500005 ] ; int MAX [ 500005 ] ; void build ( int a [ ] , int tl , int tr , int v ) { if ( tl == tr ) { MAX [ v ] = a [ tl ] ; pref [ v ] = a [ tl ] ; suf [ v ] = a [ tl ] ; } else { int tm = ( tl + tr ) / 2 ; build ( a , tl , tm , v * 2 ) ; build ( a , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = max ( MAX [ v * 2 ] , max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == tm - tl + 1 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == tr - tm ) ) ; } } void update ( int a [ ] , int pos , int tl , int tr , int v ) { if ( tl > pos tr < pos ) { return ; } if ( tl == tr && tl == pos ) { MAX [ v ] = a [ pos ] ; pref [ v ] = a [ pos ] ; suf [ v ] = a [ pos ] ; } else { int tm = ( tl + tr ) / 2 ; update ( a , pos , tl , tm , v * 2 ) ; update ( a , pos , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = max ( MAX [ v * 2 ] , max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == tm - tl + 1 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + ( suf [ 2 * v + 1 ] == tr - tm ) * suf [ v * 2 ] ) ; } } void solveQueries ( int arr [ ] , int n , vector < vector < int > > Q , int k ) { int cntQuery = Q . size ( ) ; build ( arr , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { cout << MAX [ 1 ] << " ▁ " ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; update ( arr , Q [ i ] [ 1 ] - 1 , 0 , n - 1 , 1 ) ; } } } int main ( ) { int N = 10 ; int arr [ ] = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; vector < vector < int > > Q = { { 1 } , { 2 , 3 } , { 1 } } ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; }
Find the array element having maximum frequency of the digit K | C ++ program for the above approach ; Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Initialize max ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Print the element having max frequency of digit k ; Driver Code ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFreq ( int N , int K ) { int count = 0 ; while ( N > 0 ) { if ( N % 10 == K ) { count ++ ; } N = N / 10 ; } return count ; } int findElementUtil ( int arr [ ] , int N , int K ) { int c ; int max ; int ele ; max = 0 ; for ( int i = 0 ; i < N ; i ++ ) { c = countFreq ( arr [ i ] , K ) ; if ( c > max ) { max = c ; ele = arr [ i ] ; } } if ( max == 0 ) return -1 ; else return ele ; } void findElement ( int arr [ ] , int N , int K ) { int ele = findElementUtil ( arr , N , K ) ; if ( ele == -1 ) cout << " - 1" ; else cout << ele ; } int main ( ) { int K = 3 ; int arr [ ] = { 3 , 77 , 343 , 456 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findElement ( arr , K , N ) ; return 0 ; }
Minimum removals required to make any interval equal to the union of the given Set | C ++ implementation of the above approach ; Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDeletions ( vector < pair < int , int > > & v , int n ) { int minDel = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int L = v [ i ] . first ; int R = v [ i ] . second ; int Count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] . first >= L && v [ j ] . second <= R ) { Count += 1 ; } } minDel = min ( minDel , n - Count ) ; } return minDel ; } int main ( ) { vector < pair < int , int > > v ; v . push_back ( { 1 , 3 } ) ; v . push_back ( { 4 , 12 } ) ; v . push_back ( { 5 , 8 } ) ; v . push_back ( { 13 , 20 } ) ; int N = v . size ( ) ; cout << findMinDeletions ( v , N ) ; return 0 ; }
Maximum even numbers present in any subarray of size K | C ++ program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the count of even numbers in a subarray of size K ; Calculate the count of even numbers in the current subarray ; If current element is an even number ; Stores the maximum count of even numbers from all the subarrays of size K ; Traverse remaining subarrays of size K using sliding window technique ; If the first element of the subarray is even ; Update curr ; If i - th element is even increment the count ; Update the answer ; Return answer ; Driver Code ; Size of the input array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEvenIntegers ( int arr [ ] , int N , int M ) { int curr = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( arr [ i ] % 2 == 0 ) curr ++ ; } int ans = curr ; for ( int i = M ; i < N ; i ++ ) { if ( arr [ i - M ] % 2 == 0 ) { curr -- ; } if ( arr [ i ] % 2 == 0 ) curr ++ ; ans = max ( ans , curr ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 , 6 } ; int M = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxEvenIntegers ( arr , N , M ) << endl ; return 0 ; }