• + 0 comments

    C# Solution: Explanation: we sort the array, and after the array is in a non descending order, we apply the two pointer method, "i" points to the current number and "j" starts from the end and works its way backwards until the number "ar[i] + ar[j] < k" (after that there is no reason to continue, because none of the pairs will be divisible by k)

            ar.Sort();
            int answer = 0;
            int i = 0;
            int j = n-1;
            while (i < j) {
                if ((ar[i] + ar[j])%k == 0) {
                    answer++;
                }
                if (ar[i] + ar[j] < k || j==i+1) {
                    i++;
                    j=n-1;
                } else {
                    j--; 
                }
            }
            return answer;