codeparrot/xlcost-text-to-code huggingface dataset:
Two equal sum segment range queries | C ++ implementation of the approach ; Function to find the required prefix sum ; Function to hash all the values of prefix sum array in an unordered map ; Function to check if a range can be divided into two equal parts ; To store the value of sum of entire range ; If value of sum is odd ; To store p_arr [ l - 1 ] ; If the value exists in the map ; Driver code ; prefix - sum array ; Map to store the values of prefix - sum ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void prefixSum ( int * p_arr , int * arr , int n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } void hashPrefixSum ( int * p_arr , int n , unordered_set < int > & q ) { for ( int i = 0 ; i < n ; i ++ ) q . insert ( p_arr [ i ] ) ; } void canDivide ( int * p_arr , int n , unordered_set < int > & q , int l , int r ) { int sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { cout << " No " << endl ; return ; } int beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . find ( beg + sum / 2 ) != q . end ( ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int p_arr [ n ] ; prefixSum ( p_arr , arr , n ) ; unordered_set < int > q ; hashPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ; return 0 ; }
Cost to make a string Panagram | Set 2 | C ++ implementation of the approach ; Function to return the cost to make str a Panagram ; Count the occurrences of each lowercase character ; To store the total gain ; If some character is missing , it has to be added at twice the cost ; If some character appears more than once , all of its occurrences except 1 can be traded for some gain ; If gain is more than the cost ; Return the total cost if gain < 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int costToPanagram ( string str , int cost [ ] ) { int i , n = str . length ( ) ; int occurrences [ 26 ] = { 0 } ; for ( i = 0 ; i < n ; i ++ ) occurrences [ str [ i ] - ' a ' ] ++ ; int gain = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( occurrences [ i ] == 0 ) gain -= ( 2 * cost [ i ] ) ; else if ( occurrences [ i ] > 1 ) gain += ( cost [ i ] * ( occurrences [ i ] - 1 ) ) ; } if ( gain >= 0 ) return 0 ; return ( gain * -1 ) ; } int main ( ) { int cost [ ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; string str = " geeksforgeeks " ; cout << costToPanagram ( str , cost ) ; }
Weighted sum of the characters of a string in an array | Set 2 | C ++ implementation of the approach ; Function to return the required string score ; create a hash map of strings in str ; Store every string in the map along with its position in the array ; If given string is not present in str [ ] ; Multiply sum of alphabets with position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int strScore ( string str [ ] , string s , int n ) { unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] = i + 1 ; if ( m . find ( s ) == m . end ( ) ) return 0 ; int score = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) score += s [ i ] - ' a ' + 1 ; score = score * m [ s ] ; return score ; } int main ( ) { string str [ ] = { " geeksforgeeks " , " algorithms " , " stack " } ; string s = " algorithms " ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; int score = strScore ( str , s , n ) ; cout << score ; return 0 ; }
Number of subarrays have bitwise OR >= K | C ++ implementation of the approach ; Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubArrays ( const int * arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = 0 ; for ( int k = i ; k <= j ; k ++ ) { bitwise_or = bitwise_or | arr [ k ] ; } if ( bitwise_or >= K ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 6 ; cout << countSubArrays ( arr , n , k ) ; return 0 ; }
Number of subarrays have bitwise OR >= K | C ++ implementation of the approach ; Function to build the segment tree ; Function to return the bitwise OR of segment [ L . . R ] ; Function to return the count of required sub - arrays ; Build segment tree ; Query segment tree for bitwise OR of sub - array [ i . . j ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100002 NEW_LINE int tree [ 4 * N ] ; void build ( int * arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } int countSubArrays ( int arr [ ] , int n , int K ) { build ( arr , 1 , 0 , n - 1 ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = query ( 1 , 0 , n - 1 , i , j ) ; if ( bitwise_or >= K ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 6 ; cout << countSubArrays ( arr , n , k ) ; return 0 ; }
Number of subarrays have bitwise OR >= K | C ++ program to implement the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree .
#include <bits/stdc++.h> NEW_LINE #define N  100002 NEW_LINE using namespace std ; int tree [ 4 * N ] ; void build ( int * arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } int countSubArrays ( const int * arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int low = i , high = n - 1 , index = INT_MAX ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != INT_MAX ) { count += n - index ; } } return count ; } int main ( ) { int arr [ ] = { 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; build ( arr , 1 , 0 , n - 1 ) ; int k = 6 ; cout << countSubArrays ( arr , n , k ) ; return 0 ; }
Occurrences of a pattern in binary representation of a number | C ++ program to find the number of times pattern p occurred in binary representation on n . ; Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of pattern_int and all_ones ; If the pattern occurs in the last digits of n ; Right shift n by 1 bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPattern ( int n , string pat ) { int pattern_int = 0 ; int power_two = 1 ; int all_ones = 0 ; for ( int i = pat . length ( ) - 1 ; i >= 0 ; i -- ) { int current_bit = pat [ i ] - '0' ; pattern_int += ( power_two * current_bit ) ; all_ones = all_ones + power_two ; power_two = power_two * 2 ; } int count = 0 ; while ( n && n >= pattern_int ) { if ( ( n & all_ones ) == pattern_int ) { count ++ ; } n = n >> 1 ; } return count ; } int main ( ) { int n = 500 ; string pat = "10" ; cout << countPattern ( n , pat ) ; }
Pairs with same Manhattan and Euclidean distance | C ++ implementation of the above approach ; Function to return the number of non coincident pairs of points with manhattan distance equal to euclidean distance ; To store frequency of all distinct Xi ; To store Frequency of all distinct Yi ; To store Frequency of all distinct points ( Xi , Yi ) ; ; Hash xi coordinate ; Hash yi coordinate ; Hash the point ( xi , yi ) ; find pairs with same Xi ; calculate ( ( xFrequency ) C2 ) ; find pairs with same Yi ; calculate ( ( yFrequency ) C2 ) ; find pairs with same ( Xi , Yi ) ; calculate ( ( xyFrequency ) C2 ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findManhattanEuclidPair ( pair < int , int > arr [ ] , int n ) { map < int , int > X ; map < int , int > Y ; map < pair < int , int > , int > XY ; for ( int i = 0 ; i < n ; i ++ ) { int xi = arr [ i ] . first ; int yi = arr [ i ] . second ; X [ xi ] ++ ; Y [ yi ] ++ ; XY [ arr [ i ] ] ++ ; } int xAns = 0 , yAns = 0 , xyAns = 0 ; for ( auto xCoordinatePair : X ) { int xFrequency = xCoordinatePair . second ; int sameXPairs = ( xFrequency * ( xFrequency - 1 ) ) / 2 ; xAns += sameXPairs ; } for ( auto yCoordinatePair : Y ) { int yFrequency = yCoordinatePair . second ; int sameYPairs = ( yFrequency * ( yFrequency - 1 ) ) / 2 ; yAns += sameYPairs ; } for ( auto XYPair : XY ) { int xyFrequency = XYPair . second ; int samePointPairs = ( xyFrequency * ( xyFrequency - 1 ) ) / 2 ; xyAns += samePointPairs ; } return ( xAns + yAns - xyAns ) ; } int main ( ) { pair < int , int > arr [ ] = { { 1 , 2 } , { 2 , 3 } , { 1 , 3 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findManhattanEuclidPair ( arr , n ) << endl ; return 0 ; }
Remove exactly one element from the array such that max | C ++ implementation of the above approach ; function to calculate max - min ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_min ( int a [ ] , int n ) { sort ( a , a + n ) ; return min ( a [ n - 2 ] - a [ 0 ] , a [ n - 1 ] - a [ 1 ] ) ; } int main ( ) { int a [ ] = { 1 , 3 , 3 , 7 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << max_min ( a , n ) ; return 0 ; }
Count numbers < = N whose difference with the count of primes upto them is > = K | C ++ implementation of the above approach ; primeUpto [ i ] denotes count of prime numbers upto i ; Function to compute all prime numbers and update primeUpto array ; 0 and 1 are not primes ; If i is prime ; Set all multiples of i as non - prime ; Compute primeUpto array ; Function to return the count of valid numbers ; Compute primeUpto array ; Check if the number is valid , try to reduce it ; ans is the minimum valid number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000001 ; int primeUpto [ MAX ] ; void SieveOfEratosthenes ( ) { bool isPrime [ MAX ] ; memset ( isPrime , 1 , sizeof ( isPrime ) ) ; isPrime [ 0 ] = isPrime [ 1 ] = 0 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * 2 ; j < MAX ; j += i ) isPrime [ j ] = 0 ; } } for ( int i = 1 ; i < MAX ; i ++ ) { primeUpto [ i ] = primeUpto [ i - 1 ] ; if ( isPrime [ i ] ) primeUpto [ i ] ++ ; } } int countOfNumbers ( int N , int K ) { SieveOfEratosthenes ( ) ; int low = 1 , high = N , ans = 0 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( mid - primeUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ( ans ? N - ans + 1 : 0 ) ; } int main ( ) { int N = 10 , K = 3 ; cout << countOfNumbers ( N , K ) ; }
Find the element whose multiplication with | C ++ program to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minIndex ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return -1 ; } int main ( ) { int arr [ ] = { 1 , 3 , -5 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minIndex ( arr , n ) << endl ; return 0 ; }
Ceiling of every element in same array | C ++ implementation of efficient algorithm to find floor of every element ; Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element . ; Find the first element that is greater than the given element ; Since arr [ i ] also exists in array , * ( it - 1 ) will be same as arr [ i ] . Let us check * ( it - 2 ) is also same as arr [ i ] . If true , then arr [ i ] exists twice in array , so ceiling is same same as arr [ i ] ; If next element is also same , then there are multiple occurrences , so print it ; Driver program to test insertion sort
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPrevGreater ( int arr [ ] , int n ) { if ( n == 1 ) { cout << " - 1" ; return ; } vector < int > v ( arr , arr + n ) ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { auto it = upper_bound ( v . begin ( ) , v . end ( ) , arr [ i ] ) ; if ( ( it - 1 ) != v . begin ( ) && * ( it - 2 ) == arr [ i ] ) { cout << arr [ i ] << " ▁ " ; } else if ( it != v . end ( ) ) cout << * it << " ▁ " ; else cout << -1 << " ▁ " ; } } int main ( ) { int arr [ ] = { 10 , 5 , 11 , 10 , 20 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPrevGreater ( arr , n ) ; return 0 ; }
Floor of every element in same array | C ++ implementation of efficient algorithm to find floor of every element ; Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element . ; Floor of first element is - 1 if there is only one occurrence of it . ; Find the first element that is greater than or or equal to given element ; If next element is also same , then there are multiple occurrences , so print it ; Otherwise print previous element ; Driver program to test insertion sort
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPrevGreater ( int arr [ ] , int n ) { vector < int > v ( arr , arr + n ) ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == v [ 0 ] ) { ( arr [ i ] == v [ 1 ] ) ? cout << arr [ i ] : cout << -1 ; cout << " ▁ " ; continue ; } auto it = lower_bound ( v . begin ( ) , v . end ( ) , arr [ i ] ) ; if ( it != v . end ( ) && * ( it + 1 ) == arr [ i ] ) cout << arr [ i ] << " ▁ " ; else cout << * ( it - 1 ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 6 , 11 , 7 , 8 , 20 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPrevGreater ( arr , n ) ; return 0 ; }
Find smallest and largest element from square matrix diagonals | C ++ 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 ; take new smallest value ; take new largest value ; Condition for secondary diagonal is mat [ n - 1 - i ] [ i ] 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 ; const int n = 5 ; void diagonalsMinMax ( int mat [ n ] [ n ] ) { 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 = 0 ; i < n ; i ++ ) { if ( mat [ i ] [ i ] < principalMin ) { principalMin = mat [ i ] [ i ] ; } if ( mat [ i ] [ i ] > principalMax ) { principalMax = mat [ i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] < secondaryMin ) { secondaryMin = mat [ n - 1 - i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] > secondaryMax ) { secondaryMax = mat [ n - 1 - i ] [ i ] ; } } cout << " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " << principalMin << " STRNEWLINE " ; cout << " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " << principalMax << " STRNEWLINE " ; cout << " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " << secondaryMin << " STRNEWLINE " ; cout << " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " << secondaryMax ; } int main ( ) { int matrix [ n ] [ n ] = { { 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 ) ; }
Check if array can be sorted with one swap | A linear CPP program to check if array becomes sorted after one swap ; Find counts and positions of elements that are out of order . ; If there are more than two elements are out of order . ; If all elements are sorted already ; Cases like { 1 , 5 , 3 , 4 , 2 } We swap 5 and 2. ; Cases like { 1 , 2 , 4 , 3 , 5 } ; Now check if array becomes sorted for cases like { 4 , 1 , 2 , 3 } ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSorted ( int n , int arr [ ] ) { int first = 0 , second = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { count ++ ; if ( first == 0 ) first = i ; else second = i ; } } if ( count > 2 ) return false ; if ( count == 0 ) return true ; if ( count == 2 ) swap ( arr [ first - 1 ] , arr [ second ] ) ; else if ( count == 1 ) swap ( arr [ first - 1 ] , arr [ first ] ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] < arr [ i - 1 ] ) return false ; return true ; } int main ( ) { int arr [ ] = { 1 , 4 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkSorted ( n , arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the intersection of two Matrices | CPP program to find intersection of two matrices ; Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define N  4 NEW_LINE #define M  4 NEW_LINE using namespace std ; void printIntersection ( int A [ ] [ N ] , int B [ ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] == B [ i ] [ j ] ) cout << A [ i ] [ j ] << " ▁ " ; else cout << " * ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int A [ M ] [ N ] = { { 2 , 4 , 6 , 8 } , { 1 , 3 , 5 , 7 } , { 8 , 6 , 4 , 2 } , { 7 , 5 , 3 , 1 } } ; int B [ M ] [ N ] = { { 2 , 3 , 6 , 8 } , { 1 , 3 , 5 , 2 } , { 8 , 1 , 4 , 2 } , { 3 , 5 , 4 , 1 } } ; printIntersection ( A , B ) ; return 0 ; }
Count Triplets such that one of the numbers can be written as sum of the other two | C ++ program to count Triplets such that at least one of the numbers can be written as sum of the other two ; Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int arr [ ] , int n ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = max ( max_val , arr [ i ] ) ; int freq [ max_val + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( int i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( int i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( int i = 1 ; i <= max_val ; i ++ ) { for ( int j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << ( countWays ( arr , n ) ) ; return 0 ; }
Count pairs from two arrays having sum equal to K | C ++ implementation of above approach . ; Function to return the count of pairs having sum equal to K ; Initialize pairs to 0 ; create map of elements of array A1 ; count total pairs ; Every element can be part of at most one pair . ; return total pairs ; Driver program ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int A1 [ ] , int A2 [ ] , int n1 , int n2 , int K ) { int res = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n1 ; ++ i ) m [ A1 [ i ] ] ++ ; for ( int i = 0 ; i < n2 ; ++ i ) { int temp = K - A2 [ i ] ; if ( m [ temp ] != 0 ) { res ++ ; m [ temp ] -- ; } } return res ; } int main ( ) { int A1 [ ] = { 1 , 1 , 3 , 4 , 5 , 6 , 6 } ; int A2 [ ] = { 1 , 4 , 4 , 5 , 7 } , K = 10 ; int n1 = sizeof ( A1 ) / sizeof ( A1 [ 0 ] ) ; int n2 = sizeof ( A2 ) / sizeof ( A2 [ 0 ] ) ; cout << countPairs ( A1 , A2 , n1 , n2 , K ) ; return 0 ; }
Count pairs in an array such that frequency of one is at least value of other | C ++ program to find number of ordered pairs ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOrderedPairs ( int A [ ] , int n ) { int orderedPairs = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; ++ i ) m [ A [ i ] ] ++ ; for ( auto entry : m ) { int X = entry . first ; int Y = entry . second ; for ( int j = 1 ; j <= Y ; j ++ ) { if ( m [ j ] >= X ) orderedPairs ++ ; } } return orderedPairs ; } int main ( ) { int A [ ] = { 1 , 1 , 2 , 2 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countOrderedPairs ( A , n ) ; return 0 ; }
Longest subarray with elements having equal modulo K | C ++ implementation of above approach ; function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestSubarray ( int arr [ ] , int n , int k ) { int count = 1 ; int max_length = 1 ; int prev_mod = arr [ 0 ] % k ; for ( int i = 1 ; i < n ; i ++ ) { int curr_mod = arr [ i ] % k ; if ( curr_mod == prev_mod ) { count ++ ; } else { max_length = max ( max_length , count ) ; count = 1 ; prev_mod = curr_mod ; } } return max ( max_length , count ) ; } int main ( ) { int arr [ ] = { 4 , 9 , 7 , 18 , 29 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 11 ; cout << LongestSubarray ( arr , n , k ) ; return 0 ; }