I am asking this question in relation to the following problem : https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1
Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K.
Count pairs with given sum in O(n) time and O(n) space.
Given n = 4, k = 6, arr = [1 5 7 1]
This is part of my code:
#define MOD 1000007
int getPairsCount(int arr[], int n, int k) {
    // long long int h[MOD] = {0}; // This is the one I used originally
    // but it given 3 as the answer for the input n = 4, k = 6, arr = [1 5 7 1],
    
    unordered_map<long long, long long> h; // But when using map, it gives correct output as 2
    long long int count = 0;
    
    for(int i=0;i<n;i++){
        h[arr[i]]+=1;
    }
    
    for(int i=0;i<n;i++){
        count+=h[k - arr[i]];
        
        if(k == 2*arr[i])count--;
    }
    
    return (count/2);
    }
};
Anyone please explain why there is a difference.
MOD was chosen based on the max number arr[i] can have (arr[i]<=10^6).
even using memset to set all values to 0 didn't work.
Then why there is a difference in using a map and array as hash?
 
     
    