codeparrot/xlcost-text-to-code huggingface dataset:
Find the winner of the match | Multiple Queries | C ++ program to find winner of the match ; Function to add edge between two nodes ; Function returns topological order of given graph ; Indeg vector will store indegrees of all vertices ; Answer vector will have our final topological order ; Visited will be true if that vertex has been visited ; q will store the vertices that have indegree equal to zero ; Iterate till queue is not empty ; Push the front of queue to answer ; For all neighbours of u , decrement their indegree value ; If indegree of any vertex becomes zero and it is not marked then push it to queue ; Mark this vertex as visited ; Return the resultant topological order ; Function to return the winner between u and v ; Player who comes first wins ; Driver code ; Total number of players ; Build the graph add ( adj , x , y ) means x wins over y ; Resultant topological order in topotable ; Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void add ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; } vector < int > topo ( vector < int > adj [ ] , int n ) { vector < int > indeg ( n , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( auto x : adj [ i ] ) indeg [ x ] ++ ; } vector < int > answer ; vector < bool > visited ( n , false ) ; queue < int > q ; for ( int i = 0 ; i < n ; i ++ ) { if ( indeg [ i ] == 0 ) { q . push ( i ) ; visited [ i ] = true ; } } while ( ! q . empty ( ) ) { int u = q . front ( ) ; answer . push_back ( u ) ; q . pop ( ) ; for ( auto x : adj [ u ] ) { indeg [ x ] -- ; if ( indeg [ x ] == 0 && ! visited [ x ] ) { q . push ( x ) ; visited [ x ] = true ; } } } return answer ; } int who_wins ( vector < int > topotable , int u , int v ) { for ( auto x : topotable ) { if ( x == u ) return u ; if ( x == v ) return v ; } } int main ( ) { vector < int > adj [ 10 ] ; int n = 7 ; add ( adj , 0 , 1 ) ; add ( adj , 0 , 2 ) ; add ( adj , 0 , 3 ) ; add ( adj , 1 , 5 ) ; add ( adj , 2 , 5 ) ; add ( adj , 3 , 4 ) ; add ( adj , 4 , 5 ) ; add ( adj , 6 , 0 ) ; vector < int > topotable = topo ( adj , n ) ; cout << who_wins ( topotable , 3 , 5 ) << endl ; cout << who_wins ( topotable , 1 , 2 ) << endl ; return 0 ; }
Square root of a number without using sqrt ( ) function | C ++ implementation of the approach ; Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } void findSqrt ( double n ) { double i = 1 ; bool found = false ; while ( ! found ) { if ( i * i == n ) { cout << fixed << setprecision ( 0 ) << i ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; cout << fixed << setprecision ( 5 ) << res ; found = true ; } i ++ ; } } int main ( ) { double n = 3 ; findSqrt ( n ) ; return 0 ; }
Pair with largest sum which is less than K in the array | CPP program for the above approach ; Function to find max sum less than k ; Sort the array ; While l is less than r ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( vector < int > arr , int k ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int n = arr . size ( ) , l = 0 , r = n - 1 , ans = 0 ; while ( l < r ) { if ( arr [ l ] + arr [ r ] < k ) { ans = max ( ans , arr [ l ] + arr [ r ] ) ; l ++ ; } else { r -- ; } } return ans ; } int main ( ) { vector < int > A = { 20 , 10 , 30 , 100 , 110 } ; int k = 85 ; cout << maxSum ( A , k ) << endl ; }
Length of longest substring having all characters as K | C ++ program for the above approach ; Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int length_substring ( string S , char K ) { int curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { if ( S [ i ] == K ) { curr_cnt += 1 ; } else { prev_cnt = max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } int main ( ) { string S = " abcd1111aabc " ; char K = '1' ; cout << length_substring ( S , K ) ; return 0 ; }
Find a partition point in array to maximize its xor sum | CPP program to find partition point in array to maximize xor sum ; Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Xor_Sum ( int arr [ ] , int n ) { int sum = 0 , index , left_xor = 0 , right_xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << Xor_Sum ( arr , n ) ; return 0 ; }
Find a partition point in array to maximize its xor sum | CPP program to find partition point in array to maximize xor sum ; Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ComputePrefixXor ( int arr [ ] , int PrefixXor [ ] , int n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } int Xor_Sum ( int arr [ ] , int n ) { int PrefixXor [ n ] ; ComputePrefixXor ( arr , PrefixXor , n ) ; int sum = 0 , index ; for ( int i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << Xor_Sum ( arr , n ) ; return 0 ; }
Strings from an array which are not prefix of any other string | C ++ implementation of the approach ; Trie node ; isEndOfWord is true if the node represents end of a word ; Returns new trie node ( initialized to NULLs ) ; Function to insert a string into trie ; While inerting a word make each isEndOfWord as false ; Check if this word is prefix of some already inserted word If it is then don 't insert this word ; If present word is not prefix of any other word then insert it ; Function to display words in Trie ; If node is leaf node , it indicates end of string , so a null character is added and string is displayed ; If NON NULL child is found add parent key to str and call the display function recursively for child node ; Driver code ; Construct trie
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int ALPHABET_SIZE = 26 ; struct TrieNode { struct TrieNode * children [ ALPHABET_SIZE ] ; bool isEndOfWord ; } ; struct TrieNode * getNode ( void ) { struct TrieNode * pNode = new TrieNode ; pNode -> isEndOfWord = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode -> children [ i ] = NULL ; return pNode ; } void insert ( struct TrieNode * root , string key ) { struct TrieNode * pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key [ i ] - ' a ' ; if ( ! pCrawl -> children [ index ] ) pCrawl -> children [ index ] = getNode ( ) ; pCrawl -> isEndOfWord = false ; pCrawl = pCrawl -> children [ index ] ; } int i ; for ( i = 0 ; i < 26 ; i ++ ) { if ( pCrawl -> children [ i ] ) { break ; } } if ( i == 26 ) { pCrawl -> isEndOfWord = true ; } } void display ( struct TrieNode * root , char str [ ] , int level ) { if ( root -> isEndOfWord ) { str [ level ] = ' \0' ; cout << str << endl ; } int i ; for ( i = 0 ; i < ALPHABET_SIZE ; i ++ ) { if ( root -> children [ i ] ) { str [ level ] = i + ' a ' ; display ( root -> children [ i ] , str , level + 1 ) ; } } } int main ( ) { string keys [ ] = { " apple " , " app " , " there " , " the " , " like " } ; int n = sizeof ( keys ) / sizeof ( string ) ; struct TrieNode * root = getNode ( ) ; for ( int i = 0 ; i < n ; i ++ ) insert ( root , keys [ i ] ) ; char str [ 100 ] ; display ( root , str , 0 ) ; return 0 ; }
Find the color of given node in an infinite binary tree | CPP program to find color of the node ; Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findColor ( map < int , int > mapWithColor , int query ) { int maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . find ( query ) != mapWithColor . end ( ) ) { maximum = max ( maximum , mapWithColor [ query ] ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } map < int , int > buildMapWithColor ( int arr [ ] , int n ) { map < int , int > mapWithColor ; for ( int i = 0 ; i < n ; i ++ ) { mapWithColor [ arr [ i ] ] = i + 1 ; } return mapWithColor ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 7 ; map < int , int > mapWithColor = buildMapWithColor ( arr , n ) ; cout << findColor ( mapWithColor , k ) ; return 0 ; }
Count of strings in the first array which are smaller than every string in the second array | C ++ implementation of the approach ; Function to count the number of smaller strings in A [ ] for every string in B [ ] ; Count the frequency of all characters ; Iterate for all possible strings in A [ ] ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every string in B [ ] ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A [ ] which has the frequency of the smaller character less than the frequency of the smaller character of the string in B [ ] ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE vector < int > findCount ( string a [ ] , string b [ ] , int n , int m ) { int freq [ MAX ] = { 0 } ; vector < int > smallestFreq ; for ( int i = 0 ; i < n ; i ++ ) { string s = a [ i ] ; memset ( freq , 0 , sizeof freq ) ; for ( int j = 0 ; j < s . size ( ) ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] ) { smallestFreq . push_back ( freq [ j ] ) ; break ; } } } sort ( smallestFreq . begin ( ) , smallestFreq . end ( ) ) ; vector < int > ans ; for ( int i = 0 ; i < m ; i ++ ) { string s = b [ i ] ; memset ( freq , 0 , sizeof freq ) ; for ( int j = 0 ; j < s . size ( ) ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } int frequency = 0 ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] ) { frequency = freq [ j ] ; break ; } } int ind = lower_bound ( smallestFreq . begin ( ) , smallestFreq . end ( ) , frequency ) - smallestFreq . begin ( ) ; ans . push_back ( ind ) ; } return ans ; } void printAnswer ( string a [ ] , string b [ ] , int n , int m ) { vector < int > ans = findCount ( a , b , n , m ) ; for ( auto it : ans ) { cout << it << " ▁ " ; } } int main ( ) { string A [ ] = { " aaa " , " aa " , " bdc " } ; string B [ ] = { " cccch " , " cccd " } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int m = sizeof ( B ) / sizeof ( B [ 0 ] ) ; printAnswer ( A , B , n , m ) ; return 0 ; }
Find Leftmost and Rightmost node of BST from its given preorder traversal | C ++ program to find leftmost and rightmost node from given preorder sequence ; Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LeftRightNode ( int preorder [ ] , int n ) { int min = INT_MAX , max = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } cout << " Leftmost ▁ node ▁ is ▁ " << min << " STRNEWLINE " ; cout << " Rightmost ▁ node ▁ is ▁ " << max ; } int main ( ) { int preorder [ ] = { 3 , 2 , 1 , 5 , 4 } ; int n = 5 ; LeftRightNode ( preorder , n ) ; return 0 ; }
Find the position of the given row in a 2 | C ++ implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int m = 6 , n = 4 ; int compareRow ( int a1 [ ] , int a2 [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return -1 ; } return 0 ; } int binaryCheck ( int ar [ ] [ n ] , int arr [ ] ) { int l = 0 , r = m - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int temp = compareRow ( ar [ mid ] , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 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 << binaryCheck ( mat , row ) ; return 0 ; }
Number of subarrays having absolute sum greater than K | Set | C ++ implementation of the above approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value
#include <bits/stdc++.h> NEW_LINE #define maxLen  30 NEW_LINE using namespace std ; int findCnt ( int arr [ ] , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k or arr [ i ] < -1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < -1 * k ) ans ++ ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - ( upper_bound ( arr , arr + n , arr [ i ] + k ) - arr ) ; return ans ; } int main ( ) { int arr [ ] = { -1 , 4 , -5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 0 ; cout << findCnt ( arr , n , k ) ; }
Number of intersections between two ranges | C ++ implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindIntersection ( pair < int , int > type1 [ ] , int n , pair < int , int > type2 [ ] , int m ) { int ans = n * m ; vector < int > start , end ; for ( int i = 0 ; i < n ; i ++ ) { start . push_back ( type1 [ i ] . first ) ; end . push_back ( type1 [ i ] . second ) ; } sort ( start . begin ( ) , start . end ( ) ) ; sort ( end . begin ( ) , end . end ( ) ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 [ i ] . first ; int R = type2 [ i ] . second ; ans -= ( start . end ( ) - upper_bound ( start . begin ( ) , start . end ( ) , R ) ) ; ans -= ( upper_bound ( end . begin ( ) , end . end ( ) , L - 1 ) - end . begin ( ) ) ; } return ans ; } int main ( ) { pair < int , int > type1 [ ] = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } , { 6 , 7 } } ; pair < int , int > type2 [ ] = { { 1 , 5 } , { 2 , 3 } , { 4 , 7 } , { 5 , 7 } } ; int n = sizeof ( type1 ) / ( sizeof ( type1 [ 0 ] ) ) ; int m = sizeof ( type2 ) / sizeof ( type2 [ 0 ] ) ; cout << FindIntersection ( type1 , n , type2 , m ) ; return 0 ; }
Find the Nth term divisible by a or b or c | C ++ implementation of the approach ; Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } int divTermCount ( int a , int b , int c , int num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / lcm ( a , b ) ) - ( num / lcm ( b , c ) ) - ( num / lcm ( a , c ) ) + ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } int findNthTerm ( int a , int b , int c , int n ) { int low = 1 , high = INT_MAX , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } int main ( ) { int a = 2 , b = 3 , c = 5 , n = 10 ; cout << findNthTerm ( a , b , c , n ) ; return 0 ; }
Split the given array into K sub | C ++ implementation of the above approach ; Function to check if mid can be maximum sub - arrays sum ; If individual element is greater maximum possible sum ; Increase sum of current sub - array ; If the sum is greater than mid increase count ; Check condition ; Function to find maximum subarray sum which is minimum ; int start = * max ; Max subarray sum , considering subarray of length 1 ; end += array [ i ] ; Max subarray sum , considering subarray of length n ; Answer stores possible maximum sub array sum ; If mid is possible solution Put answer = mid ; ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int mid , int array [ ] , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } int solve ( int array [ ] , int n , int K ) { int * max = max_element ( array , array + n ) ; int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } int main ( ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( array ) / sizeof ( array [ 0 ] ) ; int K = 3 ; cout << solve ( array , n , K ) ; }
Count number of common elements between two arrays by using Bitset and Bitwise operation | C ++ implementation of the approach ; Function to return the count of common elements ; Traverse the first array ; Set 1 at position a [ i ] ; Traverse the second array ; Set 1 at position b [ i ] ; Bitwise AND of both the bitsets ; Find the count of 1 's ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100000 NEW_LINE bitset < MAX > bit1 , bit2 , bit3 ; int count_common ( int a [ ] , int n , int b [ ] , int m ) { for ( int i = 0 ; i < n ; i ++ ) { bit1 . set ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { bit2 . set ( b [ i ] ) ; } bit3 = bit1 & bit2 ; int count = bit3 . count ( ) ; return count ; } int main ( ) { int a [ ] = { 1 , 4 , 7 , 2 , 3 } ; int b [ ] = { 2 , 11 , 7 , 4 , 15 , 20 , 24 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << count_common ( a , n , b , m ) ; return 0 ; }
Create a Sorted Array Using Binary Search | C ++ program to create a sorted array using Binary Search ; Function to create a new sorted array using Binary Search ; Auxiliary Array ; if b is empty any element can be at first place ; Perform Binary Search to find the correct position of current element in the new array ; let the element should be at first index ; if a [ j ] is already present in the new array ; add a [ j ] at mid + 1. you can add it at mid ; if a [ j ] is lesser than b [ mid ] go right side ; means pos should be between start and mid - 1 ; else pos should be between mid + 1 and end ; if a [ j ] is the largest push it at last ; here max ( 0 , pos ) is used because sometimes pos can be negative as smallest duplicates can be present in the array ; Print the new generated sorted array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createSorted ( int a [ ] , int n ) { vector < int > b ; for ( int j = 0 ; j < n ; j ++ ) { if ( b . empty ( ) ) b . push_back ( a [ j ] ) ; else { int start = 0 , end = b . size ( ) - 1 ; int pos = 0 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( b [ mid ] == a [ j ] ) { b . emplace ( b . begin ( ) + max ( 0 , mid + 1 ) , a [ j ] ) ; break ; } else if ( b [ mid ] > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . emplace ( b . begin ( ) + max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( int i = 0 ; i < n ; i ++ ) cout << b [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { 2 , 5 , 4 , 9 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; createSorted ( a , n ) ; return 0 ; }