codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of jumps required to sort the given array in ascending order | C ++ program for the above approach ; Function to count minimum number of jumps required to sort the array ; Stores minimum number of jumps ; Stores distances of jumps ; Stores the array elements with their starting indices ; Push the pairs { arr [ i ] , i + 1 } into the vector of pairs vect ; Update vect ; Populate the array temp [ ] ; Update temp [ arr [ i ] ] ; Sort the vector in the ascending order ; Jump till the previous index <= current index ; Update vect [ i ] ; Increment the number of jumps ; Print the minimum number of jumps required ; Driver Code ; Input ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minJumps ( int arr [ ] , int jump [ ] , int N ) { int jumps = 0 ; int temp [ 1000 ] ; vector < pair < int , int > > vect ; for ( int i = 0 ; i < N ; i ++ ) { vect . push_back ( { arr [ i ] , i + 1 } ) ; } for ( int i = 0 ; i < N ; i ++ ) { temp [ arr [ i ] ] = jump [ i ] ; } sort ( vect . begin ( ) , vect . end ( ) ) ; for ( int i = 1 ; i < N ; i ++ ) { while ( vect [ i ] . second <= vect [ i - 1 ] . second ) { vect [ i ] = make_pair ( vect [ i ] . first , vect [ i ] . second + temp [ vect [ i ] . first ] ) ; jumps ++ ; } } cout << jumps << endl ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 } ; int jump [ ] = { 1 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minJumps ( arr , jump , N ) ; return 0 ; }
Minimum number of basic logic gates required to realize given Boolean expression | C ++ implementation of the above approach ; Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Driver Code ; Input ; Function call to count the total number of gates required
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numberOfGates ( string s ) { int N = s . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < ( int ) s . size ( ) ; i ++ ) { if ( s [ i ] == ' . ' s [ i ] == ' + ' s [ i ] == '1' ) { ans ++ ; } } cout << ans ; } int main ( ) { string S = " ( 1 - A ) . B + C " ; numberOfGates ( S ) ; }
Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | C ++ program for the above approach ; Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] = abs ( arr [ i ] ) ; sort ( arr , arr + N ) ; int left = 0 ; int ans = 0 ; for ( int right = 0 ; right < N ; right ++ ) { while ( 2 * arr [ left ] < arr [ right ] ) left ++ ; ans += ( right - left ) ; } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPairs ( arr , N ) ; return 0 ; }
Calculate absolute difference between minimum and maximum sum of pairs in an array | C ++ program for the above approach ; Function to find the difference between the maximum and minimum sum of a pair ( arr [ i ] , arr [ j ] ) from the array such that i < j and arr [ i ] < arr [ j ] ; Stores the maximum from the suffix of the array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Stores the maximum sum of any pair ; Calculate the maximum sum ; Stores the maximum sum of any pair ; Stores the minimum of suffixes from the given array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Calculate the minimum sum ; Return the resultant difference ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GetDiff ( int A [ ] , int N ) { int SuffMaxArr [ N ] ; SuffMaxArr [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMaxArr [ i ] = max ( SuffMaxArr [ i + 1 ] , A [ i + 1 ] ) ; } int MaximumSum = INT_MIN ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMaxArr [ i ] ) MaximumSum = max ( MaximumSum , A [ i ] + SuffMaxArr [ i ] ) ; } int MinimumSum = INT_MAX ; int SuffMinArr [ N ] ; SuffMinArr [ N - 1 ] = INT_MAX ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMinArr [ i ] = min ( SuffMinArr [ i + 1 ] , A [ i + 1 ] ) ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMinArr [ i ] ) { MinimumSum = min ( MinimumSum , A [ i ] + SuffMinArr [ i ] ) ; } } return abs ( MaximumSum - MinimumSum ) ; } int main ( ) { int arr [ ] = { 2 , 4 , 1 , 3 , 7 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << GetDiff ( arr , N ) ; return 0 ; }
Minimum number of swaps required to make parity of array elements same as their indices | C ++ program for the above approach ; Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSwaps ( int arr [ ] , int N ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { cout << -1 ; } else { cout << even ; } } int main ( ) { int arr [ ] = { 3 , 2 , 7 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumSwaps ( arr , N ) ; return 0 ; }
Smallest substring occurring only once in a given string | C ++ program for the above approach ; Function to find the smallest substring occurring only once ; Stores all occurences ; Generate all the substrings ; Avoid multiple occurences ; Append all substrings ; Take into account all the substrings ; Iterate over all unique substrings ; If frequency is 1 ; Append into fresh list ; Initialize a dictionary ; Append the keys ; Traverse the dictionary ; Print the minimum of dictionary ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestSubstring ( string a ) { vector < string > a1 ; for ( int i = 0 ; i < a . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < a . size ( ) ; j ++ ) { if ( i != j ) a1 . push_back ( a . substr ( i , j + 1 ) ) ; } } map < string , int > a2 ; for ( string i : a1 ) a2 [ i ] ++ ; vector < string > freshlist ; for ( auto i : a2 ) { if ( i . second == 1 ) freshlist . push_back ( i . first ) ; } map < string , int > dictionary ; for ( auto i : freshlist ) { dictionary [ i ] = i . size ( ) ; } vector < int > newlist ; for ( auto i : dictionary ) newlist . push_back ( i . second ) ; int ans = INT_MAX ; for ( int i : newlist ) ans = min ( ans , i ) ; return ans ; } int main ( ) { string S = " ababaabba " ; cout << smallestSubstring ( S ) ; return 0 ; }
Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | C ++ program to implement the above approach ; Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mininsert ( int arr [ ] , int K , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = min ( arr [ i ] , arr [ i + 1 ] ) ; int b = max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 2 , 10 , 25 , 21 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << mininsert ( arr , K , N ) ; return 0 ; }
Sum of numbers formed by consecutive digits present in a given string | C ++ Program to implement the above approach ; Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigits ( string s ) { int curr = 0 ; int ret = 0 ; for ( auto & ch : s ) { if ( isdigit ( ch ) ) { curr = curr * 10 + ch - '0' ; } else { ret += curr ; curr = 0 ; } } ret += curr ; return ret ; } int main ( ) { string S = "11aa32bbb5" ; cout << sumOfDigits ( S ) ; return 0 ; }
Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | C ++ program for the above approach ; Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_triplet ( int arr [ ] , int n ) { for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { cout << i - 1 << " ▁ " << i << " ▁ " << i + 1 ; return ; } } cout << -1 ; } int main ( ) { int arr [ ] = { 4 , 3 , 5 , 2 , 1 , 6 } ; int N = sizeof ( arr ) / sizeof ( int ) ; print_triplet ( arr , N ) ; return 0 ; }
Check if an array element is concatenation of two elements from another array | C ++ program for the above approach ; Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array a [ ] ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findConcatenatedNumbers ( vector < int > a , vector < int > b ) { bool ans = true ; int n1 = a . size ( ) ; int n2 = b . size ( ) ; unordered_map < int , int > cnt ; for ( int i = 0 ; i < n1 ; i ++ ) { cnt [ a [ i ] ] = 1 ; } for ( int i = 0 ; i < n2 ; i ++ ) { int left = b [ i ] ; int right = 0 ; int mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left /= 10 ; mul *= 10 ; if ( cnt [ left ] == 1 && cnt [ right ] == 1 ) { ans = false ; cout << b [ i ] << " ▁ " ; } } } if ( ans ) cout << " - 1" ; } int main ( ) { vector < int > a = { 2 , 34 , 4 , 5 } ; vector < int > b = { 26 , 24 , 345 , 4 , 22 } ; findConcatenatedNumbers ( a , b ) ; return 0 ; }
Number of subarrays having even product | CPP implementation of the above approach ; Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenproduct ( int arr [ ] , int length ) { int count = 0 ; for ( int i = 0 ; i < length + 1 ; i ++ ) { int product = 1 ; for ( int j = i ; j < length + 1 ; j ++ ) { product *= arr [ j ] ; if ( product % 2 == 0 ) ++ count ; } } cout << count ; } int main ( ) { int arr [ ] = { 7 , 5 , 4 , 9 } ; int length = ( sizeof ( arr ) / sizeof ( arr [ 0 ] ) ) - 1 ; evenproduct ( arr , length ) ; }
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | C ++ program for the above approach ; Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print the result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to adj [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long ans = 0 ; vector < int > adj [ int ( 1e5 ) + 1 ] ; void dfsUtil ( int u , int par , int depth ) { for ( auto it : adj [ u ] ) { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ans += depth ; } void dfs ( int u , int par , int depth ) { dfsUtil ( u , par , depth ) ; cout << ans << endl ; } void countPairs ( vector < vector < int > > edges ) { for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } dfs ( 1 , 1 , 1 ) ; } int main ( ) { vector < vector < int > > edges = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } } ; countPairs ( edges ) ; return 0 ; }
Number from a given range that requires Kth smallest number of steps to get reduced to 1 | C ++ program for the above approach ; Function to count the number of steps required to reduce val to 1 by the given operations ; Base Case ; If val is even , divide by 2 ; Otherwise , multiply it by 3 and increment by 1 ; Function to find Kth smallest count of steps required for numbers from the range [ L , R ] ; Stores numbers and their respective count of steps ; Count the number of steps for all numbers from the range [ L , R ] ; Insert in the vector ; Sort the vector in ascending order w . r . t . to power value ; Print the K - th smallest number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE vector < ll > v ( 1000000 , -1 ) ; ll power_value ( ll val ) { if ( val == 1 ) return 0 ; if ( val % 2 == 0 ) { v [ val ] = power_value ( val / 2 ) + 1 ; return v [ val ] ; } else { ll temp = val * 3 ; temp ++ ; v [ val ] = power_value ( temp ) + 1 ; return v [ val ] ; } } ll getKthNumber ( int l , int r , int k ) { vector < pair < ll , ll > > ans ; for ( ll i = l ; i <= r ; i ++ ) { if ( v [ i ] == -1 ) power_value ( i ) ; } ll j = 0 ; for ( ll i = l ; i <= r ; i ++ ) { ans . push_back ( make_pair ( v [ i ] , i ) ) ; j ++ ; } sort ( ans . begin ( ) , ans . end ( ) ) ; cout << ans [ k - 1 ] . second ; } int main ( ) { int L = 7 , R = 10 , K = 4 ; getKthNumber ( L , R , K ) ; return 0 ; }
Sum of decimal equivalents of binary node values in each level of a Binary Tree | CPP program for the above approach ; Structure of a Tree Node ; Function to convert binary number to its equivalent decimal value ; Function to calculate sum of decimal equivalent of binary numbers of node values present at each level ; Push root node into queue ; Connect nodes at the same level to form a binary number ; Append the value of the current node to eachLvl ; Insert the Left child to queue , if its not NULL ; Insert the Right child to queue , if its not NULL ; Decrement length by one ; Stores the front element of the queue ; Add decimal equivalent of the binary number formed on the current level to ans ; Finally print ans ; Driver Code ; Given Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class TreeNode { public : int val ; TreeNode * left , * right ; TreeNode ( int key ) { val = key ; left = right = NULL ; } } ; int convertBinaryToDecimal ( vector < int > arr ) { int ans = 0 ; for ( int i : arr ) ans = ( ans << 1 ) | i ; return ans ; } void decimalEquilvalentAtEachLevel ( TreeNode * root ) { int ans = 0 ; queue < TreeNode * > que ; que . push ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; vector < int > eachLvl ; while ( length > 0 ) { TreeNode * temp = que . front ( ) ; que . pop ( ) ; eachLvl . push_back ( temp -> val ) ; if ( temp -> left != NULL ) que . push ( temp -> left ) ; if ( temp -> right != NULL ) que . push ( temp -> right ) ; length -= 1 ; } ans += convertBinaryToDecimal ( eachLvl ) ; } cout << ans << endl ; } int main ( ) { TreeNode * root = new TreeNode ( 0 ) ; root -> left = new TreeNode ( 1 ) ; root -> right = new TreeNode ( 0 ) ; root -> left -> left = new TreeNode ( 0 ) ; root -> left -> right = new TreeNode ( 1 ) ; root -> right -> left = new TreeNode ( 1 ) ; root -> right -> right = new TreeNode ( 1 ) ; decimalEquilvalentAtEachLevel ( root ) ; return 0 ; }
Minimize increments required to make differences between all pairs of array elements even | C ++ program for the above approach ; Function to find the minimum increments required to difference between all pairs of array elements even ; Store the count of odd and even numbers ; Traverse the array ; Increment evenCnt by 1 ; Increment eveCnt by 1 ; Print the minimum of oddCnt and eveCnt ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumOperations ( int arr [ ] , int N ) { int oddCnt = 0 , evenCnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCnt ++ ; } else { oddCnt ++ ; } } cout << min ( oddCnt , evenCnt ) ; } int main ( ) { int arr [ ] = { 4 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumOperations ( arr , N ) ; return 0 ; }
Minimum increments required to make array elements alternately even and odd | C ++ program for the above approach ; Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array arr [ ] ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int minIncr ( int * arr , int n ) { int forEven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 ) forEven += 1 ; } } return min ( forEven , n - forEven ) ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 8 , 9 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minIncr ( arr , n ) ; return 0 ; }
Queries to find the minimum array sum possible by removing elements from either end | C ++ program for the above approach ; Function to find the minimum sum for each query after removing element from either ends till each value Q [ i ] ; Stores the prefix sum from both the ends of the array ; Traverse the array from front ; Insert it into the map m1 ; Traverse the array in reverse ; Insert it into the map m2 ; Traverse the query array ; Print the minimum of the two values as the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( int arr [ ] , int N , int Q [ ] , int M ) { map < int , int > m1 , m2 ; int front = 0 , rear = 0 ; for ( int i = 0 ; i < N ; i ++ ) { front += arr [ i ] ; m1 . insert ( { arr [ i ] , front } ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { rear += arr [ i ] ; m2 . insert ( { arr [ i ] , rear } ) ; } for ( int i = 0 ; i < M ; i ++ ) { cout << min ( m1 [ Q [ i ] ] , m2 [ Q [ i ] ] ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int Q [ ] = { 7 , 6 } ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; minOperations ( arr , N , Q , M ) ; return 0 ; }
Check if a string contains an anagram of another string as its substring | C ++ Program to implement the above approach ; Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkAnagram ( string s1 , string s2 ) { vector < int > s2hash ( 26 , 0 ) ; vector < int > s1hash ( 26 , 0 ) ; int s1len = s1 . size ( ) ; int s2len = s2 . size ( ) ; if ( s1len > s2len ) return false ; int left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 [ right ] - ' a ' ] += 1 ; s2hash [ s2 [ right ] - ' a ' ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { if ( s1hash == s2hash ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 [ right ] - ' a ' ] += 1 ; s2hash [ s2 [ left ] - ' a ' ] -= 1 ; left ++ ; } return false ; } int main ( ) { string s1 = " ab " ; string s2 = " bbpobac " ; if ( checkAnagram ( s1 , s2 ) ) cout << " YES STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | C ++ program for the above approach ; Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canPlace ( string s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s [ i ] == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s [ i + 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s [ i + 1 ] == '0' && s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string S = "10001" ; int K = 1 ; int N = S . size ( ) ; canPlace ( S , N , K ) ; return 0 ; }
Maximum number of intervals that an interval can intersect | C ++ program for the above approach ; Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxIntervals ( vector < pair < int , int > > v , int n ) { int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = n ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ i ] . second < v [ j ] . first v [ i ] . first > v [ j ] . second ) { c -- ; } } maxi = max ( c , maxi ) ; } cout << maxi ; } int main ( ) { vector < pair < int , int > > arr = { { 1 , 2 } , { 3 , 4 } , { 2 , 5 } } ; int N = arr . size ( ) ; findMaxIntervals ( arr , N ) ; return 0 ; }
Check if all disks can be placed at a single rod based on given conditions | C ++ implementation of above approach ; Function to check if it is possible to move all disks to a single rod ; Stores if it is possible to move all disks to a single rod ; Traverse the array ; If i - th element is smaller than both its adjacent elements ; If flag is true ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { bool flag = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] && a [ i ] < a [ i - 1 ] ) flag = 1 ; } if ( flag ) return false ; else return true ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( check ( arr , N ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }