codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange an array to make similar indexed elements different from that of another array | C ++ program for the above approach ; Function to find the arrangement of array B [ ] such that element at each index of A [ ] and B [ ] are not equal ; Print not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Print required arrangement of array B ; Driver Code ; Given arrays A [ ] and B [ ] ; Length of array A [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void RearrangeB ( int A [ ] , vector < int > B , int n ) { if ( n == 1 && A [ 0 ] == B [ 0 ] ) { cout << " - 1" << endl ; return ; } for ( int i = 0 ; i < n / 2 ; i ++ ) { int t = B [ i ] ; B [ i ] = B [ n - i - 1 ] ; B [ n - i - 1 ] = t ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] == A [ i ] ) { int t = B [ i + 1 ] ; B [ i + 1 ] = B [ i ] ; B [ i ] = t ; break ; } } for ( int k : B ) cout << k << " ▁ " ; } int main ( ) { int A [ ] = { 2 , 4 , 5 , 8 } ; vector < int > B = { 2 , 4 , 5 , 8 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; RearrangeB ( A , B , n ) ; }
Swap the elements between any two given quadrants of a Matrix | C ++ program for the above approach ; Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Driver Code ; Given matrix ; Given quadrants ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  6 NEW_LINE #define M  6 NEW_LINE void swap ( int mat [ N ] [ M ] , int startx_X , int starty_X , int startx_Y , int starty_Y ) { int row = 0 ; int col = 0 ; for ( int i = startx_X ; ; i ++ ) { col = 0 ; for ( int j = startx_X ; ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] ; mat [ startx_Y + row ] [ starty_Y + col ] = temp ; col ++ ; if ( col >= M / 2 ) break ; } row ++ ; if ( row >= N / 2 ) break ; } } static void swapQuadOfMatrix ( int mat [ N ] [ M ] , int X , int Y ) { if ( X == 1 && Y == 2 ) { swap ( mat , 0 , 0 , 0 , M / 2 ) ; } else if ( X == 1 && Y == 3 ) { swap ( mat , 0 , 0 , N / 2 , 0 ) ; } else if ( X == 1 && Y == 4 ) { swap ( mat , 0 , 0 , N / 2 , M / 2 ) ; } else if ( X == 2 && Y == 3 ) { swap ( mat , 0 , M / 2 , N / 2 , 0 ) ; } else if ( X == 2 && Y == 4 ) { swap ( mat , 0 , M / 2 , N / 2 , M / 2 ) ; } else if ( X == 3 && Y == 4 ) { swap ( mat , N / 2 , 0 , N / 2 , M / 2 ) ; } printMat ( mat ) ; } void printMat ( int mat [ N ] [ M ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int mat [ ] [ M ] = { { 1 , 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 , 17 , 18 } , { 19 , 20 , 21 , 22 , 23 , 24 } , { 25 , 26 , 27 , 28 , 29 , 30 } , { 31 , 32 , 33 , 34 , 35 , 36 } } ; int X = 1 , Y = 4 ; swapQuadOfMatrix ( mat , X , Y ) ; }
Bottom View of a Binary Tree | C ++ Program to print Bottom View of Binary Tree ; Tree node class ; data of the node ; horizontal distance of the node ; left and right references ; Constructor of tree node ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , push ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; In STL , pop ( ) is used dequeue an item ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child , add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child , add it to the queue with a horizontal distance hd + 1. ; Traverse the map elements using the iterator . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; int hd ; Node * left , * right ; Node ( int key ) { data = key ; hd = INT_MAX ; left = right = NULL ; } } ; void bottomView ( Node * root ) { if ( root == NULL ) return ; int hd = 0 ; map < int , int > m ; queue < Node * > q ; root -> hd = hd ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; q . pop ( ) ; hd = temp -> hd ; m [ hd ] = temp -> data ; if ( temp -> left != NULL ) { temp -> left -> hd = hd - 1 ; q . push ( temp -> left ) ; } if ( temp -> right != NULL ) { temp -> right -> hd = hd + 1 ; q . push ( temp -> right ) ; } } for ( auto i = m . begin ( ) ; i != m . end ( ) ; ++ i ) cout << i -> second << " ▁ " ; } int main ( ) { Node * root = new Node ( 20 ) ; root -> left = new Node ( 8 ) ; root -> right = new Node ( 22 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 3 ) ; root -> right -> left = new Node ( 4 ) ; root -> right -> right = new Node ( 25 ) ; root -> left -> right -> left = new Node ( 10 ) ; root -> left -> right -> right = new Node ( 14 ) ; cout << " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : STRNEWLINE " ; bottomView ( root ) ; return 0 ; }
Minimum increments by index value required to obtain at least two equal Array elements | C ++ Program to implement the above approach ; Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void incrementCount ( int arr [ ] , int N ) { int mini = arr [ 0 ] - arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { mini = min ( mini , arr [ i - 1 ] - arr [ i ] ) ; } cout << mini ; } int main ( ) { int N = 3 ; int arr [ N ] = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; return 0 ; }
Minimum operations required to convert all characters of a String to a given Character | C ++ program to implement the above approach ; Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the string less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the string is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOperations ( int n , int k ) { int div = 2 * k + 1 ; if ( n / 2 <= k ) { cout << 1 << " STRNEWLINE " ; if ( n > k ) cout << k + 1 ; else cout << n ; } else { if ( n % div == 0 ) { int oprn = n / div ; cout << oprn << " STRNEWLINE " ; int pos = k + 1 ; cout << pos << " ▁ " ; for ( int i = 1 ; i <= oprn ; i ++ ) { cout << pos << " ▁ " ; pos += div ; } } else { int oprn = n / div + 1 ; cout << oprn << " STRNEWLINE " ; int pos = n % div ; if ( n % div > k ) pos -= k ; for ( int i = 1 ; i <= oprn ; i ++ ) { cout << pos << " ▁ " ; pos += div ; } } } } int main ( ) { string str = " edfreqwsazxet " ; char ch = ' $ ' ; int n = str . size ( ) ; int k = 4 ; countOperations ( n , k ) ; return 0 ; }
Length of largest subsequence consisting of a pair of alternating digits | C ++ program for the above approach ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Driver Code ; Given string ; Function call
#include <iostream> NEW_LINE using namespace std ; void largestSubsequence ( string s ) { int maxi = 0 ; char prev1 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { int len = 0 ; prev1 = j + '0' ; for ( int k = 0 ; k < s . size ( ) ; k ++ ) { if ( s [ k ] == i + '0' && prev1 == j + '0' ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == j + '0' && prev1 == i + '0' ) { prev1 = s [ k ] ; len ++ ; } } maxi = max ( len , maxi ) ; } } } if ( maxi != 1 ) cout << maxi << endl ; else cout << 0 << endl ; } int main ( ) { string s = "1542745249842" ; largestSubsequence ( s ) ; return 0 ; }
Bottom View of a Binary Tree | C ++ Program to print Bottom View of Binary Tree ; Tree node class ; data of the node ; horizontal distance of the node ; left and right references ; Constructor of tree node ; printBottomViewUtil function ; Base case ; If node for a particular horizontal distance is not present , add to the map . ; Compare height for already present node at similar horizontal distance ; Recur for left subtree ; Recur for right subtree ; printBottomView function ; Map to store Horizontal Distance , Height and Data . ; Prints the values stored by printBottomViewUtil ( ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <map> NEW_LINE using namespace std ; struct Node { int data ; int hd ; Node * left , * right ; Node ( int key ) { data = key ; hd = INT_MAX ; left = right = NULL ; } } ; void printBottomViewUtil ( Node * root , int curr , int hd , map < int , pair < int , int > > & m ) { if ( root == NULL ) return ; if ( m . find ( hd ) == m . end ( ) ) { m [ hd ] = make_pair ( root -> data , curr ) ; } else { pair < int , int > p = m [ hd ] ; if ( p . second <= curr ) { m [ hd ] . second = curr ; m [ hd ] . first = root -> data ; } } printBottomViewUtil ( root -> left , curr + 1 , hd - 1 , m ) ; printBottomViewUtil ( root -> right , curr + 1 , hd + 1 , m ) ; } void printBottomView ( Node * root ) { map < int , pair < int , int > > m ; printBottomViewUtil ( root , 0 , 0 , m ) ; map < int , pair < int , int > > :: iterator it ; for ( it = m . begin ( ) ; it != m . end ( ) ; ++ it ) { pair < int , int > p = it -> second ; cout << p . first << " ▁ " ; } } int main ( ) { Node * root = new Node ( 20 ) ; root -> left = new Node ( 8 ) ; root -> right = new Node ( 22 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 3 ) ; root -> right -> left = new Node ( 4 ) ; root -> right -> right = new Node ( 25 ) ; root -> left -> right -> left = new Node ( 10 ) ; root -> left -> right -> right = new Node ( 14 ) ; cout << " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : STRNEWLINE " ; printBottomView ( root ) ; return 0 ; }
Find maximum GCD value from root to leaf in a Binary tree | C ++ program for the above approach ; Initialise to update the maximum gcd value from all the path ; Node structure ; Left & right child of the node ; Initialize constructor ; Function to find gcd of a and b ; function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code ; Given Tree ; Function Call ; Print the maximum AND value
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxm = 0 ; struct Node { int val ; Node * left , * right ; Node ( int x ) { val = x ; left = NULL ; right = NULL ; } } ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int find_gcd ( vector < int > arr ) { if ( arr . size ( ) == 1 ) return arr [ 0 ] ; int g = arr [ 0 ] ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { g = gcd ( g , arr [ i ] ) ; } return g ; } void maxm_gcd ( Node * root , vector < int > ans ) { if ( ! root ) return ; if ( root -> left == NULL and root -> right == NULL ) { ans . push_back ( root -> val ) ; maxm = max ( find_gcd ( ans ) , maxm ) ; return ; } ans . push_back ( root -> val ) ; maxm_gcd ( root -> left , ans ) ; maxm_gcd ( root -> right , ans ) ; } int main ( ) { Node * root = new Node ( 15 ) ; root -> left = new Node ( 3 ) ; root -> right = new Node ( 7 ) ; root -> left -> left = new Node ( 15 ) ; root -> left -> right = new Node ( 1 ) ; root -> right -> left = new Node ( 31 ) ; root -> right -> right = new Node ( 9 ) ; maxm_gcd ( root , { } ) ; cout << maxm << endl ; return 0 ; }
Count of pairs with sum N from first N natural numbers | C ++ Program to implement the above approach ; Function to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pairs is equal to N ; Increase the count of pairs ; Move to the next pair ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int numberOfPairs ( int n ) { int count = 0 ; int i = 1 , j = n - 1 ; while ( i < j ) { if ( i + j == n ) { count ++ ; } i ++ ; j -- ; } return count ; } int main ( ) { int n = 8 ; cout << numberOfPairs ( n ) ; return 0 ; }
Count of ways to generate a Matrix with product of each row and column as 1 or | C ++ implementation of the above approach ; Function to return the number of possible ways ; Check if product can be - 1 ; Driver Code
#include using namespace std ; void Solve ( int N , int M ) { int temp = ( N - 1 ) * ( M - 1 ) ; int ans = pow ( 2 , temp ) ; if ( ( N + M ) % 2 != 0 ) cout << ans ; else cout << 2 * ans ; cout << endl ; } int main ( ) { int N = 3 ; int M = 3 ; Solve ( N , M ) ; return 0 ; }
Program to count leaf nodes in a binary tree | C ++ implementation to find leaf count of a given Binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf nodes in a binary tree ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver code ; create a tree ; get leaf count of the above created tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; unsigned int getLeafCount ( struct node * node ) { if ( node == NULL ) return 0 ; if ( node -> left == NULL && node -> right == NULL ) return 1 ; else return getLeafCount ( node -> left ) + getLeafCount ( node -> right ) ; } struct node * newNode ( int data ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int main ( ) { struct node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Leaf ▁ count ▁ of ▁ the ▁ tree ▁ is ▁ : ▁ " << getLeafCount ( root ) << endl ; return 0 ; }
Make a palindromic string from given string | C ++ Implementation to find which player can form a palindromic string first in a game ; Function to find winner of the game ; Array to Maintain frequency of the characters in S ; Initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driven Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int palindromeWinner ( string & S ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; int count = 0 ; for ( int i = 0 ; i < ( int ) S . length ( ) ; ++ i ) { if ( freq [ S [ i ] - ' a ' ] == 0 ) count ++ ; freq [ S [ i ] - ' a ' ] ++ ; } int unique = 0 ; int duplicate = 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } int main ( ) { string S = " abcbc " ; cout << " Player - " << palindromeWinner ( S ) << endl ; return 0 ; }
Number of substrings with length divisible by the number of 1 's in it | C ++ program to count number of substring under given condition ; Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during string traversal ; Iterate on string and modify the totCount ; If this character is 1 ; Add to the final sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over string considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfSubstrings ( string s ) { int n = s . length ( ) ; int x = sqrt ( n ) ; vector < int > ones ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) ones . push_back ( i ) ; } if ( ones . size ( ) == 0 ) return 0 ; ones . push_back ( n ) ; vector < int > totCount ( n * x + n ) ; int sum = 0 ; for ( int k = 0 ; k <= x ; k ++ ) { int now = 0 ; totCount [ k * n ] ++ ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; sum += totCount [ index + k * n ] ; totCount [ index + k * n ] ++ ; } now = 0 ; totCount [ k * n ] -- ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; totCount [ index + k * n ] -- ; } } int prefix_sum [ n ] ; memset ( prefix_sum , -1 , sizeof ( prefix_sum ) ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ i ] = cnt ; if ( s [ i ] == '1' ) cnt ++ ; } for ( int k = 0 ; k < n ; k ++ ) { for ( int j = 1 ; j <= ( n / x ) && prefix_sum [ k ] + j <= cnt ; j ++ ) { int l = ones [ prefix_sum [ k ] + j - 1 ] - k + 1 ; int r = ones [ prefix_sum [ k ] + j ] - k ; l = max ( l , j * ( x + 1 ) ) ; if ( l <= r ) { sum += r / j - ( l - 1 ) / j ; } } } return sum ; } int main ( ) { string S = "1111100000" ; cout << countOfSubstrings ( S ) ; return 0 ; }
Find the largest number smaller than integer N with maximum number of set bits | C ++ implementation to Find the largest number smaller than integer N with maximum number of set bits ; Function to return the largest number less than N ; Iterate through all the numbers ; Find the number of set bits for the current number ; Check if this number has the highest set bits ; Return the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNum ( int n ) { int num = 0 ; int max_setBits = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { int setBits = __builtin_popcount ( i ) ; if ( setBits >= max_setBits ) { num = i ; max_setBits = setBits ; } } return num ; } int main ( ) { int N = 345 ; cout << largestNum ( N ) ; return 0 ; }
Iterative program to count leaf nodes in a Binary Tree | C ++ program to count leaf nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program to test above functions ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; unsigned int getLeafCount ( struct Node * node ) { if ( ! node ) return 0 ; queue < Node * > q ; int count = 0 ; q . push ( node ) ; while ( ! q . empty ( ) ) { struct Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; if ( temp -> left == NULL && temp -> right == NULL ) count ++ ; } return count ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << getLeafCount ( root ) ; return 0 ; }
Find the Kth smallest element in the sorted generated array | C ++ implementation of the approach ; Function to return the Kth element in B [ ] ; Initialize the count Array ; Reduce N repeatedly to half its value ; Add count to start ; Subtract same count after end index ; Store each element of Array [ ] with their count ; Sort the elements wrt value ; If Kth element is in range of element [ i ] return element [ i ] ; If K is out of bound ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int Array [ ] , int N , int K ) { int count_Arr [ N + 1 ] = { 0 } ; int factor = 1 ; int size = N ; while ( size ) { int start = 1 ; int end = size ; count_Arr [ 1 ] += factor * N ; count_Arr [ end + 1 ] -= factor * N ; factor ++ ; size /= 2 ; } for ( int i = 2 ; i <= N ; i ++ ) count_Arr [ i ] += count_Arr [ i - 1 ] ; vector < pair < int , int > > element ; for ( int i = 0 ; i < N ; i ++ ) { element . push_back ( { Array [ i ] , count_Arr [ i + 1 ] } ) ; } sort ( element . begin ( ) , element . end ( ) ) ; int start = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int end = start + element [ i ] . second - 1 ; if ( K >= start && K <= end ) { return element [ i ] . first ; } start += element [ i ] . second ; } return -1 ; } int main ( ) { int arr [ ] = { 2 , 4 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 13 ; cout << solve ( arr , N , K ) ; return 0 ; }
Find the lexicographical next balanced bracket sequence | C ++ implementation of the approach ; Function to find the lexicographically next balanced bracket expression if possible ; Decrement the depth for every opening bracket ; Increment for the closing brackets ; Last opening bracket ; Generate the required string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string next_balanced_sequence ( string & s ) { string next = " - 1" ; int length = s . size ( ) ; int depth = 0 ; for ( int i = length - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == ' ( ' ) depth -- ; else depth ++ ; if ( s [ i ] == ' ( ' && depth > 0 ) { depth -- ; int open = ( length - i - 1 - depth ) / 2 ; int close = length - i - 1 - open ; next = s . substr ( 0 , i ) + ' ) ' + string ( open , ' ( ' ) + string ( close , ' ) ' ) ; break ; } } return next ; } int main ( ) { string s = " ( ( ( ) ) ) " ; cout << next_balanced_sequence ( s ) ; return 0 ; }
Maximum number that can be display on Seven Segment Display using N segments |  ; Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMaxNumber ( int n ) { if ( n & 1 ) { cout << "7" ; for ( int i = 0 ; i < ( n - 3 ) / 2 ; i ++ ) cout << "1" ; } else { for ( int i = 0 ; i < n / 2 ; i ++ ) cout << "1" ; } } int main ( ) { int n = 5 ; printMaxNumber ( n ) ; return 0 ; }