codeparrot/xlcost-text-to-code huggingface dataset:
Count quadruples from four sorted arrays whose sum is equal to a given value x | C ++ implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; unordered_map ' um ' implemented as hash table for < sum , frequency > tuples ; count frequency of each sum obtained from the pairs of arr1 [ ] and arr2 [ ] and store them in ' um ' ; generate pair from arr3 [ ] and arr4 [ ] ; calculate the sum of elements in the pair so generated ; if ' x - p _ sum ' is present in ' um ' then add frequency of ' x - p _ sum ' to ' count ' ; required count of quadruples ; Driver program to test above ; four sorted arrays each of size ' n '
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countQuadruples ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] , int arr4 [ ] , int n , int x ) { int count = 0 ; unordered_map < int , int > um ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) um [ arr1 [ i ] + arr2 [ j ] ] ++ ; for ( int k = 0 ; k < n ; k ++ ) for ( int l = 0 ; l < n ; l ++ ) { int p_sum = arr3 [ k ] + arr4 [ l ] ; if ( um . find ( x - p_sum ) != um . end ( ) ) count += um [ x - p_sum ] ; } return count ; } int main ( ) { int arr1 [ ] = { 1 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 7 , 8 } ; int arr3 [ ] = { 1 , 4 , 6 , 10 } ; int arr4 [ ] = { 2 , 4 , 7 , 8 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int x = 30 ; cout << " Count ▁ = ▁ " << countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ; return 0 ; }
How to learn Pattern printing easily ? |  ; Driver code
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int N = 4 , i , j , min ; cout << " Value ▁ of ▁ N : ▁ " << N << endl ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; cout << N - min + 1 ; } cout << endl ; } return 0 ; }
Number of subarrays having sum exactly equal to k | C ++ program for the above approach ; Calculate all subarrays ; Calculate required sum ; Check if sum is equal to required sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 10 , 2 , -2 , -20 , 10 } ; int k = -10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( sum == k ) res ++ ; } } cout << ( res ) << endl ; }
Number of subarrays having sum exactly equal to k | C ++ program to find number of subarrays with sum exactly equal to k . ; Function to find number of subarrays with sum exactly equal to k . ; STL map to store number of subarrays starting from index zero having particular value of sum . ; Sum of elements so far . ; Add current element to sum so far . ; If currsum is equal to desired sum , then a new subarray is found . So increase count of subarrays . ; currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount . ; Add currsum value to count of different values of sum . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubarraySum ( int arr [ ] , int n , int sum ) { unordered_map < int , int > prevSum ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == sum ) res ++ ; if ( prevSum . find ( currsum - sum ) != prevSum . end ( ) ) res += ( prevSum [ currsum - sum ] ) ; prevSum [ currsum ] ++ ; } return res ; } int main ( ) { int arr [ ] = { 10 , 2 , -2 , -20 , 10 } ; int sum = -10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSubarraySum ( arr , n , sum ) ; return 0 ; }
Count pairs whose products exist in array | C ++ program to count pairs whose product exist in array ; Returns count of pairs whose product exists in arr [ ] ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; break ; } } } } return result ; } int main ( ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Count pairs whose products exist in array | A hashing based C ++ program to count pairs whose product exists in arr [ ] ; Returns count of pairs whose product exists in arr [ ] ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int result = 0 ; set < int > Hash ; for ( int i = 0 ; i < n ; i ++ ) Hash . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . find ( product ) != Hash . end ( ) ) result ++ ; } } return result ; } int main ( ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }
Master Theorem For Subtract and Conquer Recurrences |  ; Driver Code
#include <stdio.h> NEW_LINE int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } int main ( ) { int n = 9 ; printf ( " % d " , fib ( n ) ) ; getchar ( ) ; return 0 ; }
Tail Recursion |  ; A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned factTR ( unsigned int n , unsigned int a ) { if ( n == 1 ) return a ; return factTR ( n - 1 , n * a ) ; } unsigned int fact ( unsigned int n ) { return factTR ( n , 1 ) ; } int main ( ) { cout << fact ( 5 ) ; return 0 ; }
Given two unsorted arrays , find all pairs whose sum is x | C ++ program to find all pairs in both arrays whose sum is equal to given value x ; Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int arr1 [ ] , int arr2 [ ] , int n , int m , int x ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( arr1 [ i ] + arr2 [ j ] == x ) cout << arr1 [ i ] << " ▁ " << arr2 [ j ] << endl ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 , 7 , 5 , 4 } ; int arr2 [ ] = { 0 , 7 , 4 , 3 , 2 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( int ) ; int m = sizeof ( arr2 ) / sizeof ( int ) ; int x = 8 ; findPairs ( arr1 , arr2 , n , m , x ) ; return 0 ; }
Smallest possible integer K such that ceil of each Array element when divided by K is at most M | C ++ program for the above approach ; Function to check if the sum of ceil values of the arr [ ] for the K value is at most M or not ; Stores the sum of ceil values ; Update the sum ; Return true if sum is less than or equal to M , false otherwise ; Function to find the smallest possible integer K such that ceil ( arr [ 0 ] / K ) + ceil ( arr [ 1 ] / K ) + ... . + ceil ( arr [ N - 1 ] / K ) is less than or equal to M ; Stores the low value ; Stores the high value ; Stores the middle value ; Update the mid value ; Check if the mid value is K ; Update the low value ; Update the high value ; Check if low is K or high is K and return it ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isvalid ( int arr [ ] , int K , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( int ) ceil ( arr [ i ] * 1.0 / K ) ; } return sum <= M ; } int smallestValueForK ( int arr [ ] , int N , int M ) { int low = 1 ; int high = * max_element ( arr , arr + N ) ; int mid ; while ( high - low > 1 ) { mid = ( high + low ) / 2 ; if ( ! isvalid ( arr , mid , N , M ) ) low = mid + 1 ; else high = mid ; } return isvalid ( arr , low , N , M ) ? low : high ; } int main ( ) { int arr [ ] = { 4 , 3 , 2 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 5 ; cout << smallestValueForK ( arr , N , M ) ; return 0 ; }
Given two unsorted arrays , find all pairs whose sum is x | C ++ program to find all pair in both arrays whose sum is equal to given value x ; Function to find all pairs in both arrays whose sum is equal to given value x ; Insert all elements of first array in a hash ; Subtract sum from second array elements one by one and check it 's present in array first  or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int arr1 [ ] , int arr2 [ ] , int n , int m , int x ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( arr1 [ i ] ) ; for ( int j = 0 ; j < m ; j ++ ) if ( s . find ( x - arr2 [ j ] ) != s . end ( ) ) cout << x - arr2 [ j ] << " ▁ " << arr2 [ j ] << endl ; } int main ( ) { int arr1 [ ] = { 1 , 0 , -4 , 7 , 6 , 4 } ; int arr2 [ ] = { 0 , 2 , 4 , -3 , 2 , 1 } ; int x = 8 ; int n = sizeof ( arr1 ) / sizeof ( int ) ; int m = sizeof ( arr2 ) / sizeof ( int ) ; findPairs ( arr1 , arr2 , n , m , x ) ; return 0 ; }
Altitude of largest Triangle that can be inscribed in a Rectangle | C ++ program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Stores the maximum altitude value ; Return res ; Driver Code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestAltitude ( int L , int B ) { if ( L > B ) swap ( L , B ) ; int res = min ( B / 2 , L ) ; return res ; } int main ( ) { int L = 3 ; int B = 4 ; cout << largestAltitude ( L , B ) ; return 0 ; }
Check if a given Binary Tree is height balanced like a Red | Program to check if a given Binary Tree is balanced like a Red - Black Tree ; utility that allocates a new Node with the given key ; Returns returns tree if the Binary tree is balanced like a Red - Black tree . This function also sets value in maxh and minh ( passed byreference ) . maxh and minh are set as maximum and minimum heights of root . ; Base case ; To store max and min heights of left subtree ; To store max and min heights of right subtree ; Check if left subtree is balanced , also set lmxh and lmnh ; Check if right subtree is balanced , also set rmxh and rmnh ; Set the max and min heights of this node for the parent call ; See if this node is balanced ; A wrapper over isBalancedUtil ( ) ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * node = new Node ; node -> key = key ; node -> left = node -> right = NULL ; return ( node ) ; } bool isBalancedUtil ( Node * root , int & maxh , int & minh ) { if ( root == NULL ) { maxh = minh = 0 ; return true ; } int lmxh , lmnh ; int rmxh , rmnh ; if ( isBalancedUtil ( root -> left , lmxh , lmnh ) == false ) return false ; if ( isBalancedUtil ( root -> right , rmxh , rmnh ) == false ) return false ; maxh = max ( lmxh , rmxh ) + 1 ; minh = min ( lmnh , rmnh ) + 1 ; if ( maxh <= 2 * minh ) return true ; return false ; } bool isBalanced ( Node * root ) { int maxh , minh ; return isBalancedUtil ( root , maxh , minh ) ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 5 ) ; root -> right = newNode ( 100 ) ; root -> right -> left = newNode ( 50 ) ; root -> right -> right = newNode ( 150 ) ; root -> right -> left -> left = newNode ( 40 ) ; isBalanced ( root ) ? cout << " Balanced " : cout << " Not ▁ Balanced " ; return 0 ; }
Maximum Fixed Point ( Value equal to index ) in a given Array | C ++ implementation of the above approach ; Function to find the maximum index i such that arr [ i ] is equal to i ; Traversing the array from backwards ; If arr [ i ] is equal to i ; If there is no such index ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLargestIndex ( int arr [ ] , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == i ) { cout << i << endl ; return ; } } cout << -1 << endl ; } int main ( ) { int arr [ ] = { -10 , -5 , 0 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findLargestIndex ( arr , n ) ; return 0 ; }
Cumulative frequency of count of each element in an unsorted array | C ++ program to print the cumulative frequency according to the order given ; Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative frequency  has been printed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countFreq ( int a [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] ) { cout << a [ i ] << " - > " << cumul << endl ; } hm [ a [ i ] ] = 0 ; } } int main ( ) { int a [ ] = { 1 , 3 , 2 , 4 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; countFreq ( a , n ) ; return 0 ; }
Find pairs in array whose sums already exist in array | A simple C ++ program to find pair whose sum already exists in array ; Function to find pair whose sum exists in arr [ ] ; Driven code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int arr [ ] , int n ) { bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { cout << arr [ i ] << " ▁ " << arr [ j ] << endl ; found = true ; } } } } if ( found == false ) cout << " Not ▁ exist " << endl ; } int main ( ) { int arr [ ] = { 10 , 4 , 8 , 13 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n ) ; return 0 ; }
Maximum number of mangoes that can be bought | C ++ program for the above approach ; Function to check if mid number of mangoes can be bought ; Store the coins ; If watermelons needed are greater than given watermelons ; Store remaining watermelons if vl watermelons are used to buy mangoes ; Store the value of coins if these watermelon get sold ; Increment coins by ex ; Number of mangoes that can be buyed if only x coins needed for one mango ; If the condition is satisfied , return true ; Otherwise return false ; Function to find the maximum number of mangoes that can be bought by selling watermelons ; Initialize the boundary values ; Store the required result ; Binary Search ; Store the mid value ; Check if it is possible to buy mid number of mangoes ; Otherwise , update r to mid - 1 ; Return the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int n , int m , int x , int y , int vl ) { int temp = m ; if ( vl > n ) return false ; int ex = n - vl ; ex *= y ; temp += ex ; int cr = temp / x ; if ( cr >= vl ) return true ; return false ; } int maximizeMangoes ( int n , int m , int x , int y ) { int l = 0 , r = n ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( check ( n , m , x , y , mid ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } return ans ; } int main ( ) { int W = 4 , C = 8 , x = 4 , y = 4 ; cout << maximizeMangoes ( W , C , x , y ) ; return 0 ; }
Kth Smallest Element in a sorted array formed by reversing subarrays from a random index | C ++ program for the above approach ; Function to find the Kth element in a sorted and rotated array at random point ; Set the boundaries for binary search ; Apply binary search to find R ; Initialize the middle element ; Check in the right side of mid ; Else check in the left side ; Random point either l or h ; Return the kth smallest element ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findkthElement ( vector < int > arr , int n , int K ) { int l = 0 ; int h = n - 1 , r ; while ( l + 1 < h ) { int mid = ( l + h ) / 2 ; if ( arr [ l ] >= arr [ mid ] ) l = mid ; else h = mid ; } if ( arr [ l ] < arr [ h ] ) r = l ; else r = h ; if ( K <= r + 1 ) return arr [ r + 1 - K ] ; else return arr [ n - ( K - ( r + 1 ) ) ] ; } int main ( ) { vector < int > arr = { 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 } ; int n = arr . size ( ) ; int K = 3 ; cout << findkthElement ( arr , n , K ) ; }
Find all pairs ( a , b ) in an array such that a % b = k | C ++ implementation to find such pairs ; Function to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool printPairs ( int arr [ ] , int n , int k ) { bool isPairFound = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j && arr [ i ] % arr [ j ] == k ) { cout << " ( " << arr [ i ] << " , ▁ " << arr [ j ] << " ) " << " ▁ " ; isPairFound = true ; } } } return isPairFound ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; if ( printPairs ( arr , n , k ) == false ) cout << " No ▁ such ▁ pair ▁ exists " ; return 0 ; }
Find all pairs ( a , b ) in an array such that a % b = k | C ++ program to find all pairs such that a % b = k . ; Utiltity function to find the divisors of n and store in vector v [ ] ; Vector is used to store the divisors ; If n is a square number , push only one occurrence ; Function to find pairs such that ( a % b = k ) ; Store all the elements in the map to use map as hash for finding elements in O ( 1 ) time . ; Print all the pairs with ( a , b ) as ( k , numbers greater than k ) as k % ( num ( > k ) ) = k i . e . 2 % 4 = 2 ; Now check for the current element as ' a ' how many b exists such that a % b = k ; find all the divisors of ( arr [ i ] - k ) ; Check for each divisor i . e . arr [ i ] % b = k or not , if yes then print that pair . ; Clear vector ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findDivisors ( int n ) { vector < int > v ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) v . push_back ( i ) ; else { v . push_back ( i ) ; v . push_back ( n / i ) ; } } } return v ; } bool printPairs ( int arr [ ] , int n , int k ) { unordered_map < int , bool > occ ; for ( int i = 0 ; i < n ; i ++ ) occ [ arr [ i ] ] = true ; bool isPairFound = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( occ [ k ] && k < arr [ i ] ) { cout << " ( " << k << " , ▁ " << arr [ i ] << " ) ▁ " ; isPairFound = true ; } if ( arr [ i ] >= k ) { vector < int > v = findDivisors ( arr [ i ] - k ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( arr [ i ] % v [ j ] == k && arr [ i ] != v [ j ] && occ [ v [ j ] ] ) { cout << " ( " << arr [ i ] << " , ▁ " << v [ j ] << " ) ▁ " ; isPairFound = true ; } } v . clear ( ) ; } } return isPairFound ; } int main ( ) { int arr [ ] = { 3 , 1 , 2 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; if ( printPairs ( arr , n , k ) == false ) cout << " No ▁ such ▁ pair ▁ exists " ; return 0 ; }
Sum of nodes in a Binary Search Tree with values from a given range | C ++ program for the above approach ; Class for node of the Tree ; Function to create a new BST node ; Return the newly created node ; Stores the sum of all nodes lying in the range [ L , R ] ; Function to perform level order traversal on the Tree and calculate the required sum ; Base Case ; Stores the nodes while performing level order traversal ; Push the root node into the queue ; Iterate until queue is empty ; Stores the front node of the queue ; If the value of the node lies in the given range ; Add it to sum ; If the left child is not NULL and exceeds low ; Insert into queue ; If the right child is not NULL and exceeds low ; Insert into queue ; Return the resultant sum ; Function to insert a new node into the Binary Search Tree ; Base Case ; If the data is less than the value of the current node ; Recur for left subtree ; Otherwise ; Recur for the right subtree ; Return the node ; Driver Code ; Let us create following BST 10 / \ 5 15 / \ \ 3 7 18
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int val ; Node * left , * right ; } ; Node * newNode ( int item ) { Node * temp = new Node ( ) ; temp -> val = item ; temp -> left = temp -> right = NULL ; return temp ; } int sum = 0 ; int rangeSumBST ( Node * root , int low , int high ) { if ( root == NULL ) return 0 ; queue < Node * > q ; q . push ( root ) ; while ( q . empty ( ) == false ) { Node * curr = q . front ( ) ; q . pop ( ) ; if ( curr -> val >= low && curr -> val <= high ) { sum += curr -> val ; } if ( curr -> left != NULL && curr -> val > low ) q . push ( curr -> left ) ; if ( curr -> right != NULL && curr -> val < high ) q . push ( curr -> right ) ; } return sum ; } Node * insert ( Node * node , int data ) { if ( node == NULL ) return newNode ( data ) ; if ( data <= node -> val ) node -> left = insert ( node -> left , data ) ; else node -> right = insert ( node -> right , data ) ; return node ; } int main ( ) { Node * root = NULL ; root = insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 3 ) ; insert ( root , 7 ) ; insert ( root , 18 ) ; int L = 7 , R = 15 ; cout << rangeSumBST ( root , L , R ) ; return 0 ; }
Convert an array to reduced form | Set 1 ( Simple and Hashing ) | C ++ program to convert an array in reduced form ; Create a temp array and copy contents of arr [ ] to temp ; Sort temp array ; Create a hash table . Refer tinyurl . com / zp5wgef http : ; One by one insert elements of sorted temp [ ] and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver program to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convert ( int arr [ ] , int n ) { int temp [ n ] ; memcpy ( temp , arr , n * sizeof ( int ) ) ; sort ( temp , temp + n ) ; unordered_map < int , int > umap ; int val = 0 ; for ( int i = 0 ; i < n ; i ++ ) umap [ temp [ i ] ] = val ++ ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = umap [ arr [ i ] ] ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Given ▁ Array ▁ is ▁ STRNEWLINE " ; printArr ( arr , n ) ; convert ( arr , n ) ; cout << " Converted Array is " ; printArr ( arr , n ) ; return 0 ; }
Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix | C ++ program for the above approach ; Function to count number of substrings that starts with string S1 and ends with string S2 ; Stores the length of each string ; Stores the count of prefixes as S1 and suffixes as S2 ; Traverse string S ; Find the prefix at index i ; Find the suffix at index i ; If the prefix is S1 ; If the suffix is S2 ; Return the count of substrings ; Function to check if the number of substrings starts with S1 and ends with S2 and vice - versa is same or not ; Count the number of substrings ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string S , string S1 , string S2 ) { int N = S . length ( ) ; int N1 = S1 . length ( ) ; int N2 = S2 . length ( ) ; int count = 0 , totalcount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { string prefix = S . substr ( i , N1 ) ; string suffix = S . substr ( i , N2 ) ; if ( S1 == prefix ) count ++ ; if ( S2 == suffix ) totalcount += count ; } return totalcount ; } void checkSubstrings ( string S , string S1 , string S2 ) { int x = countSubstrings ( S , S1 , S2 ) ; int y = countSubstrings ( S , S2 , S1 ) ; if ( x == y ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " opencloseopencloseopen " ; string S1 = " open " ; string S2 = " close " ; checkSubstrings ( S , S1 , S2 ) ; return 0 ; }