codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subarray with k distinct numbers | C ++ program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code for above function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { unordered_set < int > s ; int j ; for ( j = i ; j < n ; j ++ ) { s . insert ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) cout << " Invalid ▁ k " ; else cout << l << " ▁ " << r ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; minRange ( arr , n , k ) ; return 0 ; }
Smallest subarray with k distinct numbers | C ++ program to find minimum range that contains exactly k distinct numbers . ; prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; increment right side . ; if number of distinct elements less than k . ; if distinct elements are equal to k and length is less than previous length . ; if number of distinct elements less than k , then break . ; if distinct elements equals to k then try to increment left side . ; increment left side . ; it is same as explained in above loop . ; Driver code for above function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; int j = -1 ; map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( hm . size ( ) < k ) hm [ arr [ j ] ] ++ ; if ( hm . size ( ) == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size ( ) < k ) break ; while ( hm . size ( ) == k ) { if ( hm [ arr [ i ] ] == 1 ) hm . erase ( arr [ i ] ) ; else hm [ arr [ i ] ] -- ; i ++ ; if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm [ arr [ i ] ] == 1 ) hm . erase ( arr [ i ] ) ; else hm [ arr [ i ] ] -- ; } if ( l == 0 && r == n ) cout << " Invalid ▁ k " << endl ; else cout << l << " ▁ " << r << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; minRange ( arr , n , k ) ; return 0 ; }
Sum of f ( a [ i ] , a [ j ] ) over all pairs in an array of n integers | CPP program to calculate the sum of f ( a [ i ] , aj ] ) ; Function to calculate the sum ; map to keep a count of occurrences ; Traverse in the list from start to end number of times a [ i ] can be in a pair and to get the difference we subtract pre_sum . ; if the ( a [ i ] - 1 ) is present then subtract that value as f ( a [ i ] , a [ i ] - 1 ) = 0 ; if the ( a [ i ] + 1 ) is present then add that value as f ( a [ i ] , a [ i ] - 1 ) = 0 here we add as a [ i ] - ( a [ i ] - 1 ) < 0 which would have been added as negative sum , so we add to remove this pair from the sum value ; keeping a counter for every element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int a [ ] , int n ) { unordered_map < int , int > cnt ; int ans = 0 , pre_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt [ a [ i ] - 1 ] ) ans -= cnt [ a [ i ] - 1 ] ; if ( cnt [ a [ i ] + 1 ] ) ans += cnt [ a [ i ] + 1 ] ; cnt [ a [ i ] ] ++ ; } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << sum ( a , n ) ; return 0 ; }
Minimum distance between the maximum and minimum element of a given Array | C ++ Program to implement the above approach ; Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDistance ( int a [ ] , int n ) { int maximum = -1 , minimum = INT_MAX ; int min_index = -1 , max_index = -1 ; int min_dist = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maximum ) maximum = a [ i ] ; if ( a [ i ] < minimum ) minimum = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == minimum ) min_index = i ; if ( a [ i ] == maximum ) max_index = i ; if ( min_index != -1 && max_index != -1 ) min_dist = min ( min_dist , abs ( min_index - max_index ) ) ; } return min_dist ; } int main ( ) { int a [ ] = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 } ; int n = sizeof a / sizeof a [ 0 ] ; cout << minDistance ( a , n ) ; }
Kth diagonal from the top left of a given matrix | C ++ implementation of the above approach ; Function returns required diagonal ; Initialize values to print upper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonal ; Print its contents ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printDiagonal ( int K , int N , vector < vector < int > > & M ) { int startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { cout << M [ startrow ] [ startcol ] << " ▁ " ; } } int main ( ) { int N = 3 , K = 4 ; vector < vector < int > > M = { { 4 , 7 , 8 } , { 9 , 2 , 3 } , { 0 , 4 , 1 } } ; printDiagonal ( K , N , M ) ; return 0 ; }
Longest substring that starts with X and ends with Y | C ++ program for the above approach ; Function returns length of longest substring starting with X and ending with Y ; Length of string ; Find the length of the string starting with X from the beginning ; Find the length of the string ending with Y from the end ; Longest substring ; Print the length ; Driver Code ; Given string str ; Starting and Ending characters ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubstring ( string str , char X , char Y ) { int N = str . length ( ) ; int start = 0 ; int end = N - 1 ; int xPos = 0 ; int yPos = 0 ; while ( true ) { if ( str [ start ] == X ) { xPos = start ; break ; } start ++ ; } while ( true ) { if ( str [ end ] == Y ) { yPos = end ; break ; } end -- ; } int length = ( yPos - xPos ) + 1 ; cout << length ; } int main ( ) { string str = " HASFJGHOGAKZZFEGA " ; char X = ' A ' , Y = ' Z ' ; longestSubstring ( str , X , Y ) ; return 0 ; }
Find parent of given node in a Binary Tree with given postorder traversal | C ++ implementation to find the parent of the given node K in a binary tree whose post - order traversal is N natural numbers ; Function to find the parent of the given node ; Condition to check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Finding the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node / 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findParent ( int height , int node ) { int start = 1 ; int end = pow ( 2 , height ) - 1 ; if ( end == node ) return -1 ; while ( node >= 1 ) { end = end - 1 ; int mid = start + ( end - start ) / 2 ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } } int main ( ) { int height = 4 ; int node = 6 ; int k = findParent ( height , node ) ; cout << k ; return 0 ; }
Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | C ++ implementation to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table ' um ' ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubarrWithEqualZeroAndOne ( int arr [ ] , int n ) { unordered_map < int , int > um ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? -1 : arr [ i ] ; um [ curr_sum ] ++ ; } int count = 0 ; for ( auto itr = um . begin ( ) ; itr != um . end ( ) ; itr ++ ) { if ( itr -> second > 1 ) count += ( ( itr -> second * ( itr -> second - 1 ) ) / 2 ) ; } if ( um . find ( 0 ) != um . end ( ) ) count += um [ 0 ] ; return count ; } int main ( ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Count ▁ = ▁ " << countSubarrWithEqualZeroAndOne ( arr , n ) ; return 0 ; }
Longest subarray having count of 1 s one more than count of 0 s | C ++ implementation to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider '0' as ' - 1' ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lenOfLongSubarr ( int arr [ ] , int n ) { unordered_map < int , int > um ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? -1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - 1 ) != um . end ( ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ = ▁ " << lenOfLongSubarr ( arr , n ) ; return 0 ; }
Maximum sum subarray having sum less than or equal to given sum using Set | C ++ program to find maximum sum subarray less than K ; Function to maximum required sum < K ; Hash to lookup for value ( cum_sum - K ) ; getting cummulative sum from [ 0 to i ] ; lookup for upperbound of ( cSum - K ) in hash ; check if upper_bound of ( cSum - K ) exists then update max sum ; insert cummulative value in hash ; return maximum sum lesser than K ; Driver code ; initialise the array ; initialise the value of K ; size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubarraySum ( int arr [ ] , int N , int K ) { set < int > cum_set ; cum_set . insert ( 0 ) ; int max_sum = INT_MIN , cSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; set < int > :: iterator sit = cum_set . lower_bound ( cSum - K ) ; if ( sit != cum_set . end ( ) ) max_sum = max ( max_sum , cSum - * sit ) ; cum_set . insert ( cSum ) ; } return max_sum ; } int main ( ) { int arr [ ] = { 5 , -2 , 6 , 3 , -5 } ; int K = 15 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSubarraySum ( arr , N , K ) ; return 0 ; }
Print all triplets in sorted array that form AP | C ++ program to print all triplets in given array that form Arithmetic Progression C ++ program to print all triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAllAPTriplets ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; if ( s . find ( arr [ i ] - diff ) != s . end ( ) ) cout << arr [ i ] - diff << " ▁ " << arr [ i ] << " ▁ " << arr [ j ] << endl ; } s . insert ( arr [ i ] ) ; } } int main ( ) { int arr [ ] = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printAllAPTriplets ( arr , n ) ; return 0 ; }
Print all triplets in sorted array that form AP | C ++ program to print all triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAllAPTriplets ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { for ( int j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { cout << arr [ j ] << " ▁ " << arr [ i ] << " ▁ " << arr [ k ] << endl ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } int main ( ) { int arr [ ] = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printAllAPTriplets ( arr , n ) ; return 0 ; }
Check if there is a root to leaf path with given sequence | C ++ program to see if there is a root to leaf path with given sequence . ; A binary tree node has data , pointer to left child and a pointer to right child ; utility that allocates a new node with the given data and NULL left and right pointers . ; Util function ; If root is NULL or reached end of the array ; If current node is leaf ; If current node is equal to arr [ index ] this means that till this level path has been matched and remaining path can be either in left subtree or right subtree . ; Function to check given sequence of root to leaf path exist in tree or not . index represents current element in sequence of rooth to leaf path ; Driver function to run the case ; arr [ ] -- > sequence of root to leaf path
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newnode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } bool existPathUtil ( struct Node * root , int arr [ ] , int n , int index ) { if ( root == NULL or index == n ) return false ; if ( root -> left == NULL && root -> right == NULL ) { if ( ( root -> data == arr [ index ] ) && ( index == n - 1 ) ) return true ; return false ; } return ( ( index < n ) && ( root -> data == arr [ index ] ) && ( existPathUtil ( root -> left , arr , n , index + 1 ) || existPathUtil ( root -> right , arr , n , index + 1 ) ) ) ; } bool existPath ( struct Node * root , int arr [ ] , int n , int index ) { if ( ! root ) return ( n == 0 ) ; return existPathUtil ( root , arr , n , 0 ) ; } int main ( ) { int arr [ ] = { 5 , 8 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; struct Node * root = newnode ( 5 ) ; root -> left = newnode ( 3 ) ; root -> right = newnode ( 8 ) ; root -> left -> left = newnode ( 2 ) ; root -> left -> right = newnode ( 4 ) ; root -> left -> left -> left = newnode ( 1 ) ; root -> right -> left = newnode ( 6 ) ; root -> right -> left -> right = newnode ( 7 ) ; existPath ( root , arr , n , 0 ) ? cout << " Path ▁ Exists " : cout << " Path ▁ does ▁ not ▁ Exist " ; return 0 ; }
All unique triplets that sum up to a given value | C ++ program to find all unique triplets without using any extra space . ; Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTriplets ( int a [ ] , int n , int sum ) { int i ; sort ( a , a + n ) ; bool flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { int start = i + 1 ; int end = n - 1 ; int target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { cout << " [ " << a [ i ] << " , " << a [ start ] << " , " << a [ end ] << " ] ▁ " ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { cout << " No ▁ Such ▁ Triplets ▁ Exist " << " STRNEWLINE " ; } } int main ( ) { int a [ ] = { 12 , 3 , 6 , 1 , 6 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int sum = 24 ; findTriplets ( a , n , sum ) ; return 0 ; }
Find the position of the given row in a 2 | C ++ implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int m = 6 , n = 4 ; int linearCheck ( int ar [ ] [ n ] , int arr [ ] ) { for ( int i = 0 ; i < m ; i ++ ) { bool matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return -1 ; } int main ( ) { int mat [ m ] [ n ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ n ] = { 10 , 9 , 22 , 23 } ; cout << linearCheck ( mat , row ) ; return 0 ; }
Count number of triplets with product equal to given number | C ++ program to count triplets with given product m ; Function to count such triplets ; Consider all triplets and count if their product is equal to m ; Drivers code
#include <iostream> NEW_LINE using namespace std ; int countTriplets ( int arr [ ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) count ++ ; return count ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 2 , 3 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 24 ; cout << countTriplets ( arr , n , m ) ; return 0 ; }
Count number of triplets with product equal to given number | C ++ program to count triplets with given product m ; Function to count such triplets ; Store all the elements in a set ; Consider all pairs and check for a third number so their product is equal to m ; Check if current pair divides m or not If yes , then search for ( m / arr [ i ] * arr [ j ] ) ; Check if the third number is present in the map and it is not equal to any other two elements and also check if this triplet is not counted already using their indexes ; Return number of triplets ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int arr [ ] , int n , int m ) { unordered_map < int , int > occ ; for ( int i = 0 ; i < n ; i ++ ) occ [ arr [ i ] ] = i ; int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ i ] * arr [ j ] <= m ) && ( arr [ i ] * arr [ j ] != 0 ) && ( m % ( arr [ i ] * arr [ j ] ) == 0 ) ) { int check = m / ( arr [ i ] * arr [ j ] ) ; auto it = occ . find ( check ) ; if ( check != arr [ i ] && check != arr [ j ] && it != occ . end ( ) && it -> second > i && it -> second > j ) count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 2 , 3 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 24 ; cout << countTriplets ( arr , n , m ) ; return 0 ; }
Count of index pairs with equal elements in an array | C ++ program to count of pairs with equal elements in an array . ; Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) << endl ; return 0 ; }
Queries to answer the number of ones and zero to the left of given index | C ++ implementation of the approach ; Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void preCalculate ( int binary [ ] , int n , pair < int , int > left [ ] ) { int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] . first = count1 ; left [ i ] . second = count0 ; if ( binary [ i ] ) count1 ++ ; else count0 ++ ; } } int main ( ) { int binary [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = sizeof ( binary ) / sizeof ( binary [ 0 ] ) ; pair < int , int > left [ n ] ; preCalculate ( binary , n , left ) ; int queries [ ] = { 0 , 1 , 2 , 4 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << left [ queries [ i ] ] . first << " ▁ ones ▁ " << left [ queries [ i ] ] . second << " ▁ zeros STRNEWLINE " ; return 0 ; }
Count of index pairs with equal elements in an array | C ++ program to count of index pairs with equal elements in an array . ; Return the number of pairs with equal values . ; Finding frequency of each number . ; Calculating pairs of each value . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; int ans = 0 ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; it ++ ) { int count = it -> second ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) << endl ; return 0 ; }