codeparrot/xlcost-text-to-code huggingface dataset:
Find the repeating and the missing | Added 3 new methods | C ++ program to Find the repeating and missing elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwoElements ( int arr [ ] , int size ) { int i ; cout << " ▁ The ▁ repeating ▁ element ▁ is ▁ " ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ abs ( arr [ i ] ) - 1 ] > 0 ) arr [ abs ( arr [ i ] ) - 1 ] = - arr [ abs ( arr [ i ] ) - 1 ] ; else cout << abs ( arr [ i ] ) << " STRNEWLINE " ; } cout << " and ▁ the ▁ missing ▁ element ▁ is ▁ " ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > 0 ) cout << ( i + 1 ) ; } } int main ( ) { int arr [ ] = { 7 , 3 , 4 , 5 , 5 , 6 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printTwoElements ( arr , n ) ; }
Find the repeating and the missing | Added 3 new methods | C ++ program to Find the repeating and missing elements ; The output of this function is stored at * x and * y ; Will hold xor of all elements and numbers from 1 to n ; Will have only single set bit of xor1 ; Get the xor of all array elements ; XOR the previous result with numbers from 1 to n ; Get the rightmost set bit in set_bit_no ; Now divide elements into two sets by comparing a rightmost set bit of xor1 with the bit at the same position in each element . Also , get XORs of two sets . The two XORs are the output elements . The following two for loops serve the purpose ; arr [ i ] belongs to first set ; arr [ i ] belongs to second set ; i belongs to first set ; i belongs to second set ; * x and * y hold the desired output elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getTwoElements ( int arr [ ] , int n , int * x , int * y ) { int xor1 ; int set_bit_no ; int i ; * x = 0 ; * y = 0 ; xor1 = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) xor1 = xor1 ^ arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor1 = xor1 ^ i ; set_bit_no = xor1 & ~ ( xor1 - 1 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & set_bit_no ) * x = * x ^ arr [ i ] ; else * y = * y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { if ( i & set_bit_no ) * x = * x ^ i ; else * y = * y ^ i ; } } int main ( ) { int arr [ ] = { 1 , 3 , 4 , 5 , 5 , 6 , 2 } ; int * x = ( int * ) malloc ( sizeof ( int ) ) ; int * y = ( int * ) malloc ( sizeof ( int ) ) ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getTwoElements ( arr , n , x , y ) ; cout << " ▁ The ▁ missing ▁ element ▁ is ▁ " << * x << " ▁ and ▁ the ▁ repeating " << " ▁ number ▁ is ▁ " << * y ; getchar ( ) ; }
Find the repeating and the missing | Added 3 new methods | C ++ program to find the repeating and missing elements using Maps
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 4 , 3 , 6 , 2 , 1 , 1 } ; int n = 6 ; unordered_map < int , bool > numberMap ; for ( int i : arr ) { if ( numberMap . find ( i ) == numberMap . end ( ) ) { numberMap [ i ] = true ; } else { cout << " Repeating ▁ = ▁ " << i ; } } cout << endl ; for ( int i = 1 ; i <= n ; i ++ ) { if ( numberMap . find ( i ) == numberMap . end ( ) ) { cout << " Missing ▁ = ▁ " << i ; } } return 0 ; }
Find four elements that sum to a given value | Set 1 ( n ^ 3 solution ) | C ++ program for naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; A naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; Fix the first element and find other three ; Fix the second element and find other two ; Fix the third element and find the fourth ; find the fourth ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFourElements ( int A [ ] , int n , int X ) { for ( int i = 0 ; i < n - 3 ; i ++ ) { for ( int j = i + 1 ; j < n - 2 ; j ++ ) { for ( int k = j + 1 ; k < n - 1 ; k ++ ) { for ( int l = k + 1 ; l < n ; l ++ ) if ( A [ i ] + A [ j ] + A [ k ] + A [ l ] == X ) cout << A [ i ] << " , ▁ " << A [ j ] << " , ▁ " << A [ k ] << " , ▁ " << A [ l ] ; } } } } int main ( ) { int A [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int X = 91 ; findFourElements ( A , n , X ) ; return 0 ; }
Find four elements that sum to a given value | Set 2 | C ++ program to find 4 elements with given sum ; The following structure is needed to store pair sums in aux [ ] ; index ( int A [ ] ) of first element in pair ; index of second element in pair ; sum of the pair ; Following function is needed for library function qsort ( ) ; Function to check if two given pairs have any common element or not ; The function finds four elements with given sum X ; Create an auxiliary array to store all pair sums ; Generate all possible pairs from A [ ] and store sums of all possible pairs in aux [ ] ; Sort the aux [ ] array using library function for sorting ; Now start two index variables from two corners of array and move them toward each other . ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class pairSum { public : int first ; int sec ; int sum ; } ; int compare ( const void * a , const void * b ) { return ( ( * ( pairSum * ) a ) . sum - ( * ( pairSum * ) b ) . sum ) ; } bool noCommon ( pairSum a , pairSum b ) { if ( a . first == b . first a . first == b . sec a . sec == b . first a . sec == b . sec ) return false ; return true ; } void findFourElements ( int arr [ ] , int n , int X ) { int i , j ; int size = ( n * ( n - 1 ) ) / 2 ; pairSum aux [ size ] ; int k = 0 ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { aux [ k ] . sum = arr [ i ] + arr [ j ] ; aux [ k ] . first = i ; aux [ k ] . sec = j ; k ++ ; } } qsort ( aux , size , sizeof ( aux [ 0 ] ) , compare ) ; i = 0 ; j = size - 1 ; while ( i < size && j >= 0 ) { if ( ( aux [ i ] . sum + aux [ j ] . sum == X ) && noCommon ( aux [ i ] , aux [ j ] ) ) { cout << arr [ aux [ i ] . first ] << " , ▁ " << arr [ aux [ i ] . sec ] << " , ▁ " << arr [ aux [ j ] . first ] << " , ▁ " << arr [ aux [ j ] . sec ] << endl ; return ; } else if ( aux [ i ] . sum + aux [ j ] . sum < X ) i ++ ; else j -- ; } } int main ( ) { int arr [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 91 ; findFourElements ( arr , n , X ) ; return 0 ; }
Find four elements that sum to a given value | Set 2 | A hashing based CPP program to find if there are four elements with given sum . ; The function finds four elements with given sum X ; Store sums of all pairs in a hash table ; Traverse through all pairs and search for X - ( current pair sum ) . ; If X - sum is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFourElements ( int arr [ ] , int n , int X ) { unordered_map < int , pair < int , int > > mp ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) mp [ arr [ i ] + arr [ j ] ] = { i , j } ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( mp . find ( X - sum ) != mp . end ( ) ) { pair < int , int > p = mp [ X - sum ] ; if ( p . first != i && p . first != j && p . second != i && p . second != j ) { cout << arr [ i ] << " , ▁ " << arr [ j ] << " , ▁ " << arr [ p . first ] << " , ▁ " << arr [ p . second ] ; return ; } } } } } int main ( ) { int arr [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 91 ; findFourElements ( arr , n , X ) ; return 0 ; }
Find four elements that sum to a given value | Set 2 | C ++ program to find four elements with the given sum ; Function to find 4 elements that add up to given sum ; Iterate from 0 to temp . length ; Iterate from 0 to arr . length ; Iterate from i + 1 to arr . length ; Store curr_sum = arr [ i ] + arr [ j ] ; Check if X - curr_sum if present in map ; Store pair having map value X - curr_sum ; Print the output ; Program for two Sum ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fourSum ( int X , int arr [ ] , map < int , pair < int , int > > Map , int N ) { int temp [ N ] ; for ( int i = 0 ; i < N ; i ++ ) temp [ i ] = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int curr_sum = arr [ i ] + arr [ j ] ; if ( Map . find ( X - curr_sum ) != Map . end ( ) ) { pair < int , int > p = Map [ X - curr_sum ] ; if ( p . first != i && p . second != i && p . first != j && p . second != j && temp [ p . first ] == 0 && temp [ p . second ] == 0 && temp [ i ] == 0 && temp [ j ] == 0 ) { cout << arr [ i ] << " , " << arr [ j ] << " , " << arr [ p . first ] << " , " << arr [ p . second ] ; temp [ p . second ] = 1 ; temp [ i ] = 1 ; temp [ j ] = 1 ; break ; } } } } } map < int , pair < int , int > > twoSum ( int nums [ ] , int N ) { map < int , pair < int , int > > Map ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { Map [ nums [ i ] + nums [ j ] ] . first = i ; Map [ nums [ i ] + nums [ j ] ] . second = j ; } } return Map ; } int main ( ) { int arr [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 91 ; map < int , pair < int , int > > Map = twoSum ( arr , n ) ; fourSum ( X , arr , Map , n ) ; return 0 ; }
Search an element in an array where difference between adjacent elements is 1 | C ++ program to search an element in an array where difference between all elements is 1 ; x is the element to be searched in arr [ 0. . n - 1 ] ; Traverse the given array starting from leftmost element ; If x is found at index i ; Jump the difference between current array element and x ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int search ( int arr [ ] , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + abs ( arr [ i ] - x ) ; } cout << " number ▁ is ▁ not ▁ present ! " ; return -1 ; } int main ( ) { int arr [ ] = { 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 3 ; cout << " Element ▁ " << x << " ▁ is ▁ present ▁ at ▁ index ▁ " << search ( arr , n , 3 ) ; return 0 ; }
Third largest element in an array of distinct elements | C ++ program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = INT_MIN ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; int third = INT_MIN ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; printf ( " The ▁ third ▁ Largest ▁ element ▁ is ▁ % d STRNEWLINE " , third ) ; } int main ( ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; thirdLargest ( arr , n ) ; return 0 ; }
Third largest element in an array of distinct elements | C ++ program to find third Largest element in an array ; There should be atleast three elements ; Initialize first , second and third Largest element ; Traverse array elements to find the third Largest ; If current element is greater than first , then update first , second and third ; If arr [ i ] is in between first and second ; If arr [ i ] is in between second and third ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] , second = INT_MIN , third = INT_MIN ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { third = second ; second = arr [ i ] ; } else if ( arr [ i ] > third ) third = arr [ i ] ; } printf ( " The ▁ third ▁ Largest ▁ element ▁ is ▁ % d STRNEWLINE " , third ) ; } int main ( ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; thirdLargest ( arr , n ) ; return 0 ; }
Check if there exist two elements in an array whose sum is equal to the sum of rest of the array | C ++ program to find whether two elements exist whose sum is equal to sum of rest of the elements . ; Function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; Find sum of whole array ; If sum of array is not even than we can not divide it into two part ; For each element arr [ i ] , see if there is another element with vaalue sum - arr [ i ] ; If element exist than return the pair ; Driver program .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPair ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; sum = sum / 2 ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . find ( val ) != s . end ( ) ) { printf ( " Pair ▁ elements ▁ are ▁ % d ▁ and ▁ % d STRNEWLINE " , arr [ i ] , val ) ; return true ; } s . insert ( arr [ i ] ) ; } return false ; } int main ( ) { int arr [ ] = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkPair ( arr , n ) == false ) printf ( " No ▁ pair ▁ found " ) ; return 0 ; }
Search an element in an unsorted array using minimum number of comparisons | C ++ implementation to search an element in the unsorted array using minimum number of comparisons ; function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original ' arr [ ] ' ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string search ( int arr [ ] , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } int main ( ) { int arr [ ] = { 4 , 6 , 1 , 5 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 1 ; cout << search ( arr , n , x ) ; return 0 ; }
Count of only repeated element in a sorted array of consecutive elements | C ++ program to find the only repeated element and number of times it appears ; Assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > sequence ( const vector < int > & a ) { if ( a . size ( ) == 0 ) return { 0 , 0 } ; int s = 0 ; int e = a . size ( ) - 1 ; while ( s < e ) { int m = ( s + e ) / 2 ; if ( a [ m ] >= m + a [ 0 ] ) s = m + 1 ; else e = m ; } return { a [ s ] , a . size ( ) - ( a [ a . size ( ) - 1 ] - a [ 0 ] ) } ; } int main ( ) { pair < int , int > p = sequence ( { 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 } ) ; cout << " Repeated ▁ element ▁ is ▁ " << p . first << " , ▁ it ▁ appears ▁ " << p . second << " ▁ times " ; return 0 ; }
Find element in a sorted array whose frequency is greater than or equal to n / 2. | C ++ code to find majority element in a sorted array ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findMajority ( int arr [ ] , int n ) { return arr [ n / 2 ] ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMajority ( arr , n ) ; return 0 ; }
Minimum absolute difference of adjacent elements in a circular array | C ++ program to find maximum difference between adjacent elements in a circular array . ; Checking normal adjacent elements ; Checking circular link ; driver program to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minAdjDifference ( int arr [ ] , int n ) { if ( n < 2 ) return ; int res = abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) res = min ( res , abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = min ( res , abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; cout << " Min ▁ Difference ▁ = ▁ " << res ; } int main ( ) { int a [ ] = { 10 , 12 , 13 , 15 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; minAdjDifference ( a , n ) ; return 0 ; }
Find the first , second and third minimum elements in an array | CPP program to find the first , second and third minimum element in an array ; Check if current element is less than firstmin , then update first , second and third ; Check if current element is less than secmin then update second and third ; Check if current element is less than then update third ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  100000 NEW_LINE using namespace std ; int Print3Smallest ( int array [ ] , int n ) { int firstmin = MAX , secmin = MAX , thirdmin = MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } cout << " First ▁ min ▁ = ▁ " << firstmin << " STRNEWLINE " ; cout << " Second ▁ min ▁ = ▁ " << secmin << " STRNEWLINE " ; cout << " Third ▁ min ▁ = ▁ " << thirdmin << " STRNEWLINE " ; } int main ( ) { int array [ ] = { 4 , 9 , 1 , 32 , 12 } ; int n = sizeof ( array ) / sizeof ( array [ 0 ] ) ; Print3Smallest ( array , n ) ; return 0 ; }
Program to find the minimum ( or maximum ) element of an array | CPP program to find minimum ( or maximum ) element in an array . ; If there is single element , return it . Else return minimum of first element and minimum of remaining array . ; If there is single element , return it . Else return maximum of first element and maximum of remaining array . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMin ( int arr [ ] , int n ) { return ( n == 1 ) ? arr [ 0 ] : min ( arr [ 0 ] , getMin ( arr + 1 , n - 1 ) ) ; } int getMax ( int arr [ ] , int n ) { return ( n == 1 ) ? arr [ 0 ] : max ( arr [ 0 ] , getMax ( arr + 1 , n - 1 ) ) ; } int main ( ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ element ▁ of ▁ array : ▁ " << getMin ( arr , n ) << " STRNEWLINE " ; cout << " Maximum ▁ element ▁ of ▁ array : ▁ " << getMax ( arr , n ) ; return 0 ; }
Program to find the minimum ( or maximum ) element of an array | CPP program to find minimum ( or maximum ) element in an array . ; C ++ program to find minimum ( or maximum ) element in an array . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMin ( int arr [ ] , int n ) { return * min_element ( arr , arr + n ) ; } int getMax ( int arr [ ] , int n ) { return * max_element ( arr , arr + n ) ; } int main ( ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ element ▁ of ▁ array : ▁ " << getMin ( arr , n ) << " STRNEWLINE " ; cout << " Maximum ▁ element ▁ of ▁ array : ▁ " << getMax ( arr , n ) ; return 0 ; }
Closest greater element for every array element from another array | CPP to find result from target array for closest element ; Function for printing resultant array ; change arr [ ] to vector ; sort vector for ease ; iterator for upper_bound ; vector for result ; calculate resultant array ; check upper bound element ; if no element found push - 1 ; Else push the element ; add to resultant ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void closestResult ( int a [ ] , int b [ ] , int n ) { vector < int > vect ( a , a + n ) ; sort ( vect . begin ( ) , vect . end ( ) ) ; vector < int > :: iterator up ; vector < int > c ; for ( int i = 0 ; i < n ; i ++ ) { up = upper_bound ( vect . begin ( ) , vect . end ( ) , b [ i ] ) ; if ( up == vect . end ( ) ) c . push_back ( -1 ) ; else c . push_back ( * up ) ; } cout << " Result ▁ = ▁ " ; for ( auto it = c . begin ( ) ; it != c . end ( ) ; it ++ ) cout << * it << " ▁ " ; } int main ( ) { int a [ ] = { 2 , 5 , 6 , 1 , 8 , 9 } ; int b [ ] = { 2 , 1 , 0 , 5 , 4 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; closestResult ( a , b , n ) ; return 0 ; }
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | C ++ program to print frequencies of all array elements in O ( n ) extra space and O ( n ) time ; Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Hashmap ; Traverse all array elements ; update the frequency of array [ i ] ; increase the index ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findCounts ( int * arr , int n ) { int hash [ n ] = { 0 } ; int i = 0 ; while ( i < n ) { hash [ arr [ i ] - 1 ] ++ ; i ++ ; } printf ( " Below are counts of all elements " for ( int i = 0 ; i < n ; i ++ ) printf ( " % d ▁ - > ▁ % d STRNEWLINE " , i + 1 , hash [ i ] ) ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 2 , 5 } ; findCounts ( arr , sizeof ( arr ) / sizeof ( arr [ 0 ] ) ) ; int arr1 [ ] = { 1 } ; findCounts ( arr1 , sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ) ; int arr3 [ ] = { 4 , 4 , 4 , 4 } ; findCounts ( arr3 , sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ) ; int arr2 [ ] = { 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 } ; findCounts ( arr2 , sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ) ; int arr4 [ ] = { 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 } ; findCounts ( arr4 , sizeof ( arr4 ) / sizeof ( arr4 [ 0 ] ) ) ; int arr5 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 } ; findCounts ( arr5 , sizeof ( arr5 ) / sizeof ( arr5 [ 0 ] ) ) ; int arr6 [ ] = { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ; findCounts ( arr6 , sizeof ( arr6 ) / sizeof ( arr6 [ 0 ] ) ) ; return 0 ; }