codeparrot/xlcost-text-to-code huggingface dataset:
Search in a trie Recursively | CPP program to search in a trie ; Trie node ; endOfWord is true if the node represents end of a word ; Function will return the new node ( initialized to NULLs ) ; initially assign null to the all child ; function will insert the string in a trie recursively ; Insert a new node ; Recursive call for insertion of a string ; Make the endOfWord true which represents the end of string ; Function call to insert a string ; Function call with necessary arguments ; Function to search the string in a trie recursively ; When a string or any character of a string is not found ; Condition of finding string successfully ; Return true when endOfWord of last node containes true ; Recursive call and return value of function call stack ; Function call to search the string ; If string found ; Driver code ; Function call to insert the string ; Function call to search the string
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define CHILDREN  26 NEW_LINE #define MAX  100 NEW_LINE struct trie { trie * child [ CHILDREN ] ; bool endOfWord ; } ; trie * createNode ( ) { trie * temp = new trie ( ) ; temp -> endOfWord = false ; for ( int i = 0 ; i < CHILDREN ; i ++ ) { temp -> child [ i ] = NULL ; } return temp ; } void insertRecursively ( trie * itr , string str , int i ) { if ( i < str . length ( ) ) { int index = str [ i ] - ' a ' ; if ( itr -> child [ index ] == NULL ) { itr -> child [ index ] = createNode ( ) ; } insertRecursively ( itr -> child [ index ] , str , i + 1 ) ; } else { itr -> endOfWord = true ; } } void insert ( trie * itr , string str ) { insertRecursively ( itr , str , 0 ) ; } bool searchRecursively ( trie * itr , char str [ ] , int i , int len ) { if ( itr == NULL ) return false ; if ( itr -> endOfWord == true && i == len - 1 ) { return true ; } int index = str [ i ] - ' a ' ; return searchRecursively ( itr -> child [ index ] , str , i + 1 , len ) ; } void search ( trie * root , string str ) { char arr [ str . length ( ) + 1 ] ; strcpy ( arr , str . c_str ( ) ) ; if ( searchRecursively ( root , arr , 0 , str . length ( ) + 1 ) ) cout << " found " << endl ; else { cout << " not ▁ found " << endl ; } } int main ( ) { trie * root = createNode ( ) ; insert ( root , " thier " ) ; insert ( root , " there " ) ; insert ( root , " answer " ) ; insert ( root , " any " ) ; search ( root , " anywhere " ) ; search ( root , " answer " ) ; return 0 ; }
Count duplicates in a given linked list | C ++ implementation of the approach ; Representation of node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Starting from the next node ; If some duplicate node is found ; Return the count of duplicate nodes ; Driver code
#include <iostream> NEW_LINE #include <unordered_set> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void insert ( Node * * head , int item ) { Node * temp = new Node ( ) ; temp -> data = item ; temp -> next = * head ; * head = temp ; } int countNode ( Node * head ) { int count = 0 ; while ( head -> next != NULL ) { Node * ptr = head -> next ; while ( ptr != NULL ) { if ( head -> data == ptr -> data ) { count ++ ; break ; } ptr = ptr -> next ; } head = head -> next ; } return count ; } int main ( ) { Node * head = NULL ; insert ( & head , 5 ) ; insert ( & head , 7 ) ; insert ( & head , 5 ) ; insert ( & head , 1 ) ; insert ( & head , 7 ) ; cout << countNode ( head ) ; return 0 ; }
Check if it is possible to form string B from A under the given constraints | C ++ implementation of the approach ; Function that returns true if it is possible to form B from A satisfying the given conditions ; Vector to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( string A , string B , int b , int m ) { vector < int > S [ 26 ] ; vector < int > box ( A . length ( ) , 0 ) ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { S [ A [ i ] - ' a ' ] . push_back ( i ) ; } int low = 0 ; for ( int i = 0 ; i < B . length ( ) ; i ++ ) { auto it = lower_bound ( S [ B [ i ] - ' a ' ] . begin ( ) , S [ B [ i ] - ' a ' ] . end ( ) , low ) ; if ( it == S [ B [ i ] - ' a ' ] . end ( ) ) return false ; int count = ( * it ) / b ; box [ count ] = box [ count ] + 1 ; if ( box [ count ] >= m ) { count ++ ; low = ( count ) * b ; } else low = ( * it ) + 1 ; } return true ; } int main ( ) { string A = " abcbbcdefxyz " ; string B = " acdxyz " ; int b = 5 ; int m = 2 ; if ( isPossible ( A , B , b , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Remove all occurrences of any element for maximum array sum | Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumArray ( int arr [ ] , int n ) { int sum = 0 ; unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; mp [ arr [ i ] ] ++ ; } int minimum = INT_MAX ; for ( auto x : mp ) minimum = min ( minimum , x . second * x . first ) ; return ( sum - minimum ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxSumArray ( arr , n ) ; return 0 ; }
Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | C ++ implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1001 NEW_LINE static int prefix [ MAX ] [ MAX ] ; int ar [ MAX ] ; void cal_prefix ( int n , int arr [ ] ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) ar [ i + 1 ] = arr [ i ] ; for ( i = 1 ; i <= 1000 ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) prefix [ i ] [ j ] = 0 ; for ( j = 1 ; j <= n ; j ++ ) { prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + ( int ) ( ar [ j ] <= i ? 1 : 0 ) ; } } } int ksub ( int l , int r , int n , int k ) { int lo , hi , mid ; lo = 1 ; hi = 1000 ; while ( lo + 1 < hi ) { mid = ( lo + hi ) / 2 ; if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) hi = mid ; else lo = mid + 1 ; } if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) hi = lo ; return hi ; } int main ( ) { int arr [ ] = { 1 , 4 , 2 , 3 , 5 , 7 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; cal_prefix ( n , arr ) ; int queries [ ] [ 3 ] = { { 1 , n , 1 } , { 2 , n - 2 , 2 } , { 3 , n - 1 , 3 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) << endl ; return 0 ; }
Find maximum N such that the sum of square of first N natural numbers is not more than X | C ++ implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate till maxvalue of N ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int squareSum ( int N ) { int sum = ( int ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } int findMaxN ( int X ) { int N = sqrt ( X ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( squareSum ( i ) > X ) return i - 1 ; } return -1L ; } int main ( ) { int X = 25 ; cout << findMaxN ( X ) ; return 0 ; }
Remove exactly one element from the array such that max | C ++ implementation of the above approach ; function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_min ( int a [ ] , int n ) { if ( n <= 1 ) return INT_MAX ; int f_min = a [ 0 ] , s_min = INT_MAX ; int f_max = a [ 0 ] , s_max = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= f_min ) { s_min = f_min ; f_min = a [ i ] ; } else if ( a [ i ] < s_min ) { s_min = a [ i ] ; } if ( a [ i ] >= f_max ) { s_max = f_max ; f_max = a [ i ] ; } else if ( a [ i ] > s_max ) { s_max = a [ i ] ; } } return min ( ( f_max - s_min ) , ( s_max - f_min ) ) ; } int main ( ) { int a [ ] = { 1 , 3 , 3 , 7 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << max_min ( a , n ) ; return 0 ; }
Minimum in an array which is first decreasing then increasing | C ++ program to find the smallest number in an array of decrease and increasing numbers ; Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimal ( int a [ ] , int n ) { int lo = 0 , hi = n - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } int main ( ) { int a [ ] = { 8 , 5 , 4 , 3 , 4 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int ind = minimal ( a , n ) ; cout << a [ ind ] ; }
Leftmost and rightmost indices of the maximum and the minimum element of an array | C ++ implementation of the approach ; If found new minimum ; If arr [ i ] = min then rightmost index for min will change ; If found new maximum ; If arr [ i ] = max then rightmost index for max will change ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findIndices ( int arr [ ] , int n ) { int leftMin = 0 , rightMin = 0 ; int leftMax = 0 , rightMax = 0 ; int min = arr [ 0 ] , max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) { leftMin = rightMin = i ; min = arr [ i ] ; } else if ( arr [ i ] == min ) rightMin = i ; if ( arr [ i ] > max ) { leftMax = rightMax = i ; max = arr [ i ] ; } else if ( arr [ i ] == max ) rightMax = i ; } cout << " Minimum ▁ left ▁ : ▁ " << leftMin << " STRNEWLINE " ; cout << " Minimum ▁ right ▁ : ▁ " << rightMin << " STRNEWLINE " ; cout << " Maximum ▁ left ▁ : ▁ " << leftMax << " STRNEWLINE " ; cout << " Maximum ▁ right ▁ : ▁ " << rightMax << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 2 , 1 , 1 , 2 , 1 , 5 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findIndices ( arr , n ) ; }
Find smallest and largest element from square matrix diagonals | CPP program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; void diagonalsMinMax ( int mat [ 5 ] [ 5 ] ) { int n = sizeof ( * mat ) / 4 ; if ( n == 0 ) return ; int principalMin = mat [ 0 ] [ 0 ] , principalMax = mat [ 0 ] [ 0 ] ; int secondaryMin = mat [ n - 1 ] [ 0 ] , secondaryMax = mat [ n - 1 ] [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { if ( i == j ) { if ( mat [ i ] [ j ] < principalMin ) { principalMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > principalMax ) { principalMax = mat [ i ] [ j ] ; } } if ( ( i + j ) == ( n - 1 ) ) { if ( mat [ i ] [ j ] < secondaryMin ) { secondaryMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > secondaryMax ) { secondaryMax = mat [ i ] [ j ] ; } } } } cout << ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " ) << principalMin << endl ; cout << ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " ) << principalMax << endl ; cout << ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " ) << secondaryMin << endl ; cout << ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " ) << secondaryMax << endl ; } int main ( ) { int matrix [ 5 ] [ 5 ] = { { 1 , 2 , 3 , 4 , -10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , -5 } } ; diagonalsMinMax ( matrix ) ; }
Count elements such that there are exactly X elements with values greater than or equal to X | C ++ implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll int getCount ( vector < ll int > v , int n ) { sort ( ( v ) . begin ( ) , ( v ) . end ( ) ) ; ll int cnt = 0 ; for ( ll int i = 0 ; i < n ; i ++ ) { ll int tmp = v . end ( ) - 1 - upper_bound ( ( v ) . begin ( ) , ( v ) . end ( ) , v [ i ] - 1 ) ; if ( tmp == v [ i ] ) cnt ++ ; } return cnt ; } int main ( ) { ll int n ; n = 4 ; vector < ll int > v ; v . push_back ( 1 ) ; v . push_back ( 2 ) ; v . push_back ( 3 ) ; v . push_back ( 4 ) ; cout << getCount ( v , n ) ; return 0 ; }
Number of segments where all elements are greater than X | C ++ program to count the number of segments in which all elements are greater than X ; Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSegments ( int a [ ] , int n , int x ) { bool flag = false ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } int main ( ) { int a [ ] = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int x = 13 ; cout << countSegments ( a , n , x ) ; return 0 ; }
Find array elements with frequencies in range [ l , r ] | C ++ program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver code
#include " iostream " NEW_LINE #include " unordered _ map " NEW_LINE using namespace std ; void findElements ( int arr [ ] , int n , int l , int r ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; ++ i ) { mp [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n ; ++ i ) { if ( l <= mp [ arr [ i ] ] && mp [ arr [ i ] <= r ] ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int l = 2 , r = 3 ; findElements ( arr , n , l , r ) ; return 0 ; }
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | C ++ implementation of the approach ; Function returns the count of the triplets ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver code
#include <iostream> NEW_LINE using namespace std ; class gfg { public : long count_triples ( int n , int k ) ; } ; long gfg :: count_triples ( int n , int k ) { int i = 0 , j = 0 , l = 0 ; int count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } int main ( ) { gfg g ; int n = 3 ; int k = 2 ; long ans = g . count_triples ( n , k ) ; cout << ans ; }
kth smallest / largest in a small range unsorted array | C ++ program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxs  1000001 NEW_LINE int kthSmallestLargest ( int * arr , int n , int k ) { int max_val = * max_element ( arr , arr + n ) ; int hash [ max_val + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int count = 0 ; for ( int i = 0 ; i <= max_val ; i ++ ) { while ( hash [ i ] > 0 ) { count ++ ; if ( count == k ) return i ; hash [ i ] -- ; } } return -1 ; } int main ( ) { int arr [ ] = { 11 , 6 , 2 , 9 , 4 , 3 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) , k = 3 ; cout << " kth ▁ smallest ▁ number ▁ is : ▁ " << kthSmallestLargest ( arr , n , k ) << endl ; return 0 ; }
Sum and Product of minimum and maximum element of an Array | C ++ program for the above approach ; Function to get sum ; Function to get product ; Driver Code ; sorting the array ; min element would be the element at 0 th index of array after sorting ; max element would be the element at ( n - 1 ) th index of array after sorting
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int minEle , int maxEle ) { return minEle + maxEle ; } int findProduct ( int minEle , int maxEle ) { return minEle * maxEle ; } int main ( ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , arr + n ) ; int minEle = arr [ 0 ] ; int maxEle = arr [ n - 1 ] ; cout << " Sum ▁ = ▁ " << findSum ( minEle , maxEle ) << endl ; cout << " Product ▁ = ▁ " << findProduct ( minEle , maxEle ) ; return 0 ; }
Meta Binary Search | One | C ++ implementation of above approach ; Function to show the working of Meta binary search ; Set number of bits to represent largest array index ; while ( ( 1 << lg ) < n - 1 ) lg += 1 ; ; Incrementally construct the index of the target value ; find the element in one direction and update position ; if element found return pos otherwise - 1 ; Driver code
#include <iostream> NEW_LINE #include <cmath> NEW_LINE #include <vector> NEW_LINE using namespace std ; int bsearch ( vector < int > A , int key_to_search ) { int n = ( int ) A . size ( ) ; int lg = log2 ( n - 1 ) + 1 ; int pos = 0 ; for ( int i = lg ; i >= 0 ; i -- ) { if ( A [ pos ] == key_to_search ) return pos ; int new_pos = pos | ( 1 << i ) ; if ( ( new_pos < n ) && ( A [ new_pos ] <= key_to_search ) ) pos = new_pos ; } return ( ( A [ pos ] == key_to_search ) ? pos : -1 ) ; } int main ( void ) { vector < int > A = { -2 , 10 , 100 , 250 , 32315 } ; cout << bsearch ( A , 10 ) << endl ; return 0 ; }
Queries to check if a number lies in N ranges of L | C ++ program to check if the number lies in given range ; Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; else check if greater is hashed as 1 ; Driver code ; function call to answer queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void answerQueries ( int a [ ] [ 2 ] , int n , int queries [ ] , int q ) { vector < int > v ; unordered_map < int , int > mpp ; for ( int i = 0 ; i < n ; i ++ ) { v . push_back ( a [ i ] [ 0 ] ) ; mpp [ a [ i ] [ 0 ] ] = 1 ; v . push_back ( a [ i ] [ 1 ] ) ; mpp [ a [ i ] [ 1 ] ] = 2 ; } sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < q ; i ++ ) { int num = queries [ i ] ; int ind = lower_bound ( v . begin ( ) , v . end ( ) , num ) - v . begin ( ) ; if ( v [ ind ] == num ) { cout << " Yes STRNEWLINE " ; } else { if ( mpp [ v [ ind ] ] == 2 ) cout << " Yes STRNEWLINE " ; cout << " No STRNEWLINE " ; } } } int main ( ) { int a [ ] [ 2 ] = { { 5 , 6 } , { 1 , 3 } , { 8 , 10 } } ; int n = 3 ; int queries [ ] = { 2 , 3 , 4 , 7 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; answerQueries ( a , n , queries , q ) ; return 0 ; }
Median of two sorted arrays of different sizes | Set 1 ( Linear ) | A C ++ program to find median of two sorted arrays of unequal sizes ; This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; float findmedian ( int a [ ] , int n1 , int b [ ] , int n2 ) { int i = 0 ; int j = 0 ; int k ; int m1 = -1 , m2 = -1 ; for ( k = 0 ; k <= ( n1 + n2 ) / 2 ; k ++ ) { if ( i < n1 && j < n2 ) { if ( a [ i ] < b [ j ] ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } else { m2 = m1 ; m1 = b [ j ] ; j ++ ; } } else if ( i == n1 ) { m2 = m1 ; m1 = b [ j ] ; j ++ ; } else if ( j == n2 ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } } if ( ( n1 + n2 ) % 2 == 0 ) return ( m1 + m2 ) * 1.0 / 2 ; return m1 ; } int main ( ) { int a [ ] = { 1 , 12 , 15 , 26 , 38 } ; int b [ ] = { 2 , 13 , 24 } ; int n1 = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int n2 = sizeof ( b ) / sizeof ( b [ 0 ] ) ; printf ( " % f " , findmedian ( a , n1 , b , n2 ) ) ; return 0 ; }
Next Smaller Element | Simple C ++ program to print next smaller elements in a given array ; prints element and NSE pair for all elements of arr [ ] of size n ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void printNSE ( int arr [ ] , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = -1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { next = arr [ j ] ; break ; } } cout << arr [ i ] << " ▁ - - ▁ " << next << endl ; } } int main ( ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printNSE ( arr , n ) ; return 0 ; }
Longest subarray such that the difference of max and min is at | C ++ code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( int input [ ] , int length ) { int prev = -1 ; int current , next ; int prevCount = 0 , currentCount = 1 ; int longest = 1 ; current = input [ 0 ] ; for ( int i = 1 ; i < length ; i ++ ) { next = input [ i ] ; if ( next == current ) { currentCount ++ ; } else if ( next == prev ) { prevCount += currentCount ; prev = current ; current = next ; currentCount = 1 ; } else { longest = max ( longest , currentCount + prevCount ) ; prev = current ; prevCount = currentCount ; current = next ; currentCount = 1 ; } } return max ( longest , currentCount + prevCount ) ; } int main ( ) { int input [ ] = { 5 , 5 , 6 , 7 , 6 } ; int n = sizeof ( input ) / sizeof ( int ) ; cout << longestSubarray ( input , n ) ; return 0 ; }