codeparrot/xlcost-text-to-code huggingface dataset:
Maximum of sum of length of rectangles and squares formed by given sticks |  ; Function to find the maximum of sum of lengths of rectangles and squares formed using given set of sticks ; Stores the count of frequencies of all the array elements ; Stores frequencies which are at least 2 ; Convert all frequencies to nearest smaller even values . ; Sum of elements up to index of multiples of 4 ; Print the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSumLength ( vector < int > arr , int n ) { map < int , int > freq ; for ( int i : arr ) freq [ i ] += 1 ; map < int , int > freq_2 ; for ( auto i : freq ) { if ( freq [ i . first ] >= 2 ) freq_2 [ i . first ] = freq [ i . first ] ; } vector < int > arr1 ; for ( auto i : freq_2 ) arr1 . push_back ( ( i . first ) * ( freq_2 [ ( i . first ) ] / 2 ) * 2 ) ; sort ( arr1 . begin ( ) , arr1 . end ( ) ) ; int summ = 0 ; for ( int i : arr1 ) summ += i ; cout << summ ; } int main ( ) { vector < int > arr = { 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 } ; int n = arr . size ( ) ; findSumLength ( arr , n ) ; return 0 ; }
Maximum area rectangle by picking four sides from array | CPP program for finding maximum area possible of a rectangle ; function for finding max area ; traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; return the product of dimensions ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findArea ( int arr [ ] , int n ) { unordered_set < int > s ; int first = 0 , second = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) == s . end ( ) ) { s . insert ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } return ( first * second ) ; } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findArea ( arr , n ) ; return 0 ; }
Print root to leaf paths without using recursion | C ++ program to Print root to leaf path WITHOUT using recursion ; A binary tree ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Function to print root to leaf path for a leaf using parent nodes stored in map ; start from leaf node and keep on pushing nodes into stack till root node is reached ; Start popping nodes from stack and print them ; An iterative function to do preorder traversal of binary tree and print root to leaf path without using recursion ; Corner Case ; Create an empty stack and push root to it ; Create a map to store parent pointers of binary tree nodes ; parent of root is NULL ; Pop all items one by one . Do following for every popped item a ) push its right child and set its parent pointer b ) push its left child and set its parent pointer Note that right child is pushed first so that left is processed first ; Pop the top item from stack ; If leaf node encountered , print Top To Bottom path ; Push right & left children of the popped node to stack . Also set their parent pointer in the map ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } void printTopToBottomPath ( Node * curr , map < Node * , Node * > parent ) { stack < Node * > stk ; while ( curr ) { stk . push ( curr ) ; curr = parent [ curr ] ; } while ( ! stk . empty ( ) ) { curr = stk . top ( ) ; stk . pop ( ) ; cout << curr -> data << " ▁ " ; } cout << endl ; } void printRootToLeaf ( Node * root ) { if ( root == NULL ) return ; stack < Node * > nodeStack ; nodeStack . push ( root ) ; map < Node * , Node * > parent ; parent [ root ] = NULL ; while ( ! nodeStack . empty ( ) ) { Node * current = nodeStack . top ( ) ; nodeStack . pop ( ) ; if ( ! ( current -> left ) && ! ( current -> right ) ) printTopToBottomPath ( current , parent ) ; if ( current -> right ) { parent [ current -> right ] = current ; nodeStack . push ( current -> right ) ; } if ( current -> left ) { parent [ current -> left ] = current ; nodeStack . push ( current -> left ) ; } } } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 8 ) ; root -> right = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 2 ) ; printRootToLeaf ( root ) ; return 0 ; }
Game of replacing array elements | CPP program for Game of Replacement ; Function return which player win the game ; Create hash that will stores all distinct element ; Traverse an array element ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int playGame ( int arr [ ] , int n ) { unordered_set < int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash . insert ( arr [ i ] ) ; return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Player ▁ " << playGame ( arr , n ) << " ▁ Wins " << endl ; return 0 ; }
Length of longest strict bitonic subsequence | C ++ implementation to find length of longest strict bitonic subsequence ; function to find length of longest strict bitonic subsequence ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last / first element of that subsequence ; arrays to store the length of increasing and decreasing subsequences which end at them or start from them ; to store the length of longest strict bitonic subsequence ; traverse the array elements from left to right ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' inc ' ; update arr [ i ] subsequence length in ' inc ' and in len_inc [ ] ; traverse the array elements from right to left ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' dcr ' ; update arr [ i ] subsequence length in ' dcr ' and in len_dcr [ ] ; calculating the length of all the strict bitonic subsequence ; required longest length strict bitonic subsequence ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longLenStrictBitonicSub ( int arr [ ] , int n ) { unordered_map < int , int > inc , dcr ; int len_inc [ n ] , len_dcr [ n ] ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( inc . find ( arr [ i ] - 1 ) != inc . end ( ) ) len = inc [ arr [ i ] - 1 ] ; inc [ arr [ i ] ] = len_inc [ i ] = len + 1 ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { int len = 0 ; if ( dcr . find ( arr [ i ] - 1 ) != dcr . end ( ) ) len = dcr [ arr [ i ] - 1 ] ; dcr [ arr [ i ] ] = len_dcr [ i ] = len + 1 ; } for ( int i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } int main ( ) { int arr [ ] = { 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Longest ▁ length ▁ strict ▁ bitonic ▁ subsequence ▁ = ▁ " << longLenStrictBitonicSub ( arr , n ) ; return 0 ; }
Selection Sort VS Bubble Sort |  ; Driver code
#include <iostream> NEW_LINE using namespace std ; void Selection_Sort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { int min_index = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( arr [ j ] < arr [ min_index ] ) min_index = j ; } swap ( arr [ i ] , arr [ min_index ] ) ; } } int main ( ) { int n = 5 ; int arr [ 5 ] = { 2 , 0 , 1 , 4 , 3 } ; Selection_Sort ( arr , n ) ; cout << " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Selection ▁ Sort ▁ is ▁ : ▁ " ; for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Last seen array element ( last appearance is earliest ) | C ++ program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastSeenElement ( int a [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] = i ; int res_ind = INT_MAX , res ; for ( auto x : hash ) { if ( x . second < res_ind ) { res_ind = x . second ; res = x . first ; } } return res ; } int main ( ) { int a [ ] = { 2 , 1 , 2 , 2 , 4 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << lastSeenElement ( a , n ) ; return 0 ; }
Selection Sort VS Bubble Sort |  ; Driver code
#include <iostream> NEW_LINE using namespace std ; void Bubble_Sort ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) swap ( arr [ j ] , arr [ j + 1 ] ) ; } } } int main ( ) { int n = 5 ; int arr [ 5 ] = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; cout << " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ; for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Introduction to Arrays |  ; Creating an integer array named arr of size 10. ; accessing element at 0 index and setting its value to 5. ; access and print value at 0 index we get the output as 5.
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int arr [ 10 ] ; arr [ 0 ] = 5 ; cout << arr [ 0 ] ; return 0 ; }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | C ++ implementation of the above approach ; Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Stores all the array elements in sorted order ; Traverse the array ; Insert current element into multiset ; Perform each operation ; Stores smallest element of ms ; Stores the largest element of ms ; Stores updated value of smallest element of ms by given operations ; Stores updated value of largest element of ms by given operations ; If the value of a + b less than the sum of smallest and largest array element ; Erase the smallest element ; Erase the largest element ; Insert the updated value of the smallest element ; Insert the updated value of the smallest element ; Stores sum of array elements ; Traverse the multiset ; Update ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimum_possible_sum ( int arr [ ] , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } multiset < int > ms ; for ( int i = 0 ; i < n ; i ++ ) { ms . insert ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = * ms . begin ( ) ; int largest_element = * ms . rbegin ( ) ; int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { ms . erase ( ms . begin ( ) ) ; ms . erase ( prev ( ms . end ( ) ) ) ; ms . insert ( a ) ; ms . insert ( b ) ; } } int ans = 0 ; for ( int x : ms ) { ans += x ; } return ans ; } int main ( ) { int arr [ ] = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimum_possible_sum ( arr , n , K ) ; return 0 ; }
Minimum cost required to connect all houses in a city | C ++ program for the above approach ; Utility function to find set of an element v using path compression technique ; If v is the parent ; Otherwise , recursively find its parent ; Function to perform union of the sets a and b ; Find parent of a and b ; If parent are different ; Swap Operation ; Update parent of b as a ; Otherwise , return 0 ; Function to create a Minimum Cost Spanning tree for given houses ; Stores adjacency list of graph ; Traverse each coordinate ; Find the Manhattan distance ; Add the edges ; Sort all the edges ; Initialize parent [ ] and size [ ] ; / Stores the minimum cost ; Finding the minimum cost ; Perform the unioun operation ; Print the minimum cost ; Driver Code ; Given houses ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > parent , size ; int find_set ( int v ) { if ( v == parent [ v ] ) return v ; return parent [ v ] = find_set ( parent [ v ] ) ; } int union_sets ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( size [ a ] < size [ b ] ) swap ( a , b ) ; parent [ b ] = a ; size [ a ] += size [ b ] ; return 1 ; } return 0 ; } void MST ( int houses [ ] [ 2 ] , int n ) { vector < pair < int , pair < int , int > > > v ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int p = abs ( houses [ i ] [ 0 ] - houses [ j ] [ 0 ] ) ; p += abs ( houses [ i ] [ 1 ] - houses [ j ] [ 1 ] ) ; v . push_back ( { p , { i , j } } ) ; } } parent . resize ( n ) ; size . resize ( n ) ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { parent [ i ] = i , size [ i ] = 1 ; } int ans = 0 ; for ( auto x : v ) { if ( union_sets ( x . second . first , x . second . second ) ) { ans += x . first ; } } cout << ans ; } int main ( ) { int houses [ ] [ 2 ] = { { 0 , 0 } , { 2 , 2 } , { 3 , 10 } , { 5 , 2 } , { 7 , 0 } } ; int N = sizeof ( houses ) / sizeof ( houses [ 0 ] ) ; MST ( houses , N ) ; return 0 ; }
Lexicographically smallest subsequence possible by removing a character from given string | C ++ program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the string ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void firstSubsequence ( string s ) { int isMax = -1 ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s . erase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; } cout << s ; } int main ( ) { string S = " geeksforgeeks " ; firstSubsequence ( S ) ; return 0 ; }
Maximize product of array by replacing array elements with its sum or product with element from another array | C ++ program for the above approach ; Function to find the largest product of array A [ ] ; Base Case ; Store all the elements of the array A [ ] ; Sort the Array B [ ] ; Traverse the array B [ ] ; Pop minimum element ; Check which operation is producing maximum element ; Insert resultant element into the priority queue ; Evaluate the product of the elements of A [ ] ; Return the maximum product ; Driver Code ; Given arrays ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largeProduct ( vector < int > A , vector < int > B , int N ) { if ( N == 0 ) return 0 ; priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < N ; i ++ ) pq . push ( A [ i ] ) ; sort ( B . begin ( ) , B . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { int minn = pq . top ( ) ; pq . pop ( ) ; int maximized_element = max ( minn * B [ i ] , minn + B [ i ] ) ; pq . push ( maximized_element ) ; } int max_product = 1 ; while ( pq . size ( ) > 0 ) { max_product *= pq . top ( ) ; pq . pop ( ) ; } return max_product ; } int main ( ) { vector < int > A = { 1 , 1 , 10 } ; vector < int > B = { 1 , 1 , 1 } ; int N = 3 ; cout << largeProduct ( A , B , N ) ; }
Print all root to leaf paths with there relative positions | C ++ program to print all root to leaf paths with there relative position ; tree structure ; function create new node ; store path information ; Prints given root to leaf path with underscores ; Find the minimum horizontal distance value in current root to leaf path ; find minimum horizontal distance ; print the root to leaf path with " _ " that indicate the related position ; current tree node ; print underscore ; print current key ; a utility function print all path from root to leaf working of this function is similar to function of " Print _ vertical _ order " : Print paths of binary tree in vertical order www . geeksforgeeks . org / print - binary - tree - vertical - order - set - 2 / https : ; base case ; leaf node ; add leaf node and then print path ; store current path information ; call left sub_tree ; call left sub_tree ; base case ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_PATH_SIZE  1000 NEW_LINE struct Node { char data ; Node * left , * right ; } ; Node * newNode ( char data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } struct PATH { int Hd ; char key ; } ; void printPath ( vector < PATH > path , int size ) { int minimum_Hd = INT_MAX ; PATH p ; for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; minimum_Hd = min ( minimum_Hd , p . Hd ) ; } for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; int noOfUnderScores = abs ( p . Hd - minimum_Hd ) ; for ( int i = 0 ; i < noOfUnderScores ; i ++ ) cout << " _ ▁ " ; cout << p . key << endl ; } cout << " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = " << endl ; } void printAllPathsUtil ( Node * root , vector < PATH > & AllPath , int HD , int order ) { if ( root == NULL ) return ; if ( root -> left == NULL && root -> right == NULL ) { AllPath [ order ] = ( PATH { HD , root -> data } ) ; printPath ( AllPath , order + 1 ) ; return ; } AllPath [ order ] = ( PATH { HD , root -> data } ) ; printAllPathsUtil ( root -> left , AllPath , HD - 1 , order + 1 ) ; printAllPathsUtil ( root -> right , AllPath , HD + 1 , order + 1 ) ; } void printAllPaths ( Node * root ) { if ( root == NULL ) return ; vector < PATH > Allpaths ( MAX_PATH_SIZE ) ; printAllPathsUtil ( root , Allpaths , 0 , 0 ) ; } int main ( ) { Node * root = newNode ( ' A ' ) ; root -> left = newNode ( ' B ' ) ; root -> right = newNode ( ' C ' ) ; root -> left -> left = newNode ( ' D ' ) ; root -> left -> right = newNode ( ' E ' ) ; root -> right -> left = newNode ( ' F ' ) ; root -> right -> right = newNode ( ' G ' ) ; printAllPaths ( root ) ; return 0 ; }
Minimum steps required to rearrange given array to a power sequence of 2 | C ++ program to implement the above approach ; Function to calculate the minimum steps required to convert given array into a power sequence of 2 ; Sort the array in ascending order ; Calculate the absolute difference between arr [ i ] and 2 ^ i for each index ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minsteps ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += abs ( arr [ i ] - pow ( 2 , i ) ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 8 , 2 , 10 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minsteps ( arr , n ) << endl ; return 0 ; }
Block swap algorithm for array rotation |  ; Prototype for utility functions ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int size ) ; void swap ( int arr [ ] , int fi , int si , int d ) ; void leftRotate ( int arr [ ] , int d , int n ) { if ( d == 0 d == n ) return ; if ( n - d == d ) { swap ( arr , 0 , n - d , d ) ; return ; } if ( d < n - d ) { swap ( arr , 0 , n - d , d ) ; leftRotate ( arr , d , n - d ) ; } else { swap ( arr , 0 , d , n - d ) ; leftRotate ( arr + n - d , 2 * d - n , d ) ; } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } void swap ( int arr [ ] , int fi , int si , int d ) { int i , temp ; for ( i = 0 ; i < d ; i ++ ) { temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; return 0 ; }
Cost required to empty a given array by repeated removal of maximum obtained by given operations | C ++ program for the above approach ; Function to find the total cost of removing all array elements ; Sort the array in descending order ; Stores the total cost ; Contribution of i - th greatest element to the cost ; Remove the element ; If negative ; Add to the final cost ; Return the cost ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCost ( int * a , int n ) { sort ( a , a + n , greater < int > ( ) ) ; int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int p = a [ j ] - j ; a [ j ] = 0 ; if ( p < 0 ) { p = 0 ; continue ; } count += p ; } return count ; } int main ( ) { int arr [ ] = { 1 , 6 , 7 , 4 , 2 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findCost ( arr , N ) ; return 0 ; }
Block swap algorithm for array rotation | C ++ code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B
void leftRotate ( int arr [ ] , int d , int n ) { int i , j ; if ( d == 0 d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }
Count of index pairs with equal elements in an array | Set 2 | C ++ program for the above approach ; Function that count the pairs having same elements in the array arr [ ] ; Hash map to keep track of occurences of elements ; Traverse the array arr [ ] ; Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer ; Return the result ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int ans = 0 ; unordered_map < int , int > count ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ arr [ i ] ] != 0 ) ans += count [ arr [ i ] ] ; count [ arr [ i ] ] ++ ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }