Sort by

recency

|

2433 Discussions

|

  • + 0 comments

    def divisibleSumPairs(n, k, ar): count = 0 for i in range(n): for j in range(i + 1, n): if (ar[i] + ar[j]) % k == 0: count += 1 return count

  • + 0 comments

    The topic "Divisible Sum Pairs" is an interesting one, especially for those diving into problem-solving and algorithms. It focuses on finding pairs in an array that are divisible by a given number, which can sharpen your math and programming logic. By the way, speaking of staying sharp, if you’re juggling coding and chores, don’t forget to tackle those house cleaning Glenside PA tasks—or better yet, hire some help! A clean space is as satisfying as solving a tricky problem. This blend of focus and organization might just inspire your next big breakthrough!

  • + 0 comments
    int divisibleSumPairs(int n, int k, int ar_count, int *ar)
    {
        int no = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if ((ar[i] + ar[j]) % k == 0)
                {
                    no++;
                }
            }
        }
        return no;
    }
    
  • + 0 comments

    Best Solution in O(n) without data structure Overhead: Lang: Java

    import java.io.IOException;

    public class Main{ public static void main(String[] args) throws IOException { Reader reader = new Reader(); int n = reader.readInt(); int k = reader.readInt(); int ret = 0; int[] remainders = new int[k];

        //O(n)
        for (int i = 0; i < n; i++) {
            int rIndex = reader.readInt() % k;
            //Special Case
            if(rIndex == 0) {
                ret += remainders[0];
            }
            //Where k is even
            else if (rIndex == k - rIndex) {
                ret += remainders[k / 2];
            }
    
            //general case
            else {
                ret += remainders[k - rIndex];
            }
    
            remainders[rIndex]++;
        }
    
        System.out.println(ret);
    }
    

    }

  • + 1 comment

    Here is my c++ solution, you can whatch the explanation here: https://youtu.be/aDv1iUSgnd8

    Solution 1 : O(n^2)

    int divisibleSumPairs(int n, int k, vector<int> ar) {
        int result = 0;
        for(int i = 0; i < ar.size() - 1; i++){
            for(int j = i + 1; j < ar.size(); j++){
                if( (ar[i] + ar[j]) % k == 0) result++;
            }
        }
        return result;
    }
    

    Solution 2 : O(n)

    int divisibleSumPairs(int n, int k, vector<int> ar) {
        map<int, int> mp;
        for(int i = 0; i < ar.size(); i++){
            mp[ar[i] % k]++;
        }
        int result = 0;
         for(int i = 0; i <= k/2; i++){
            if(i == 0 || i*2 == k) result += (mp[i] * (mp[i] - 1)) / 2;
            else result += mp[i] * mp[k - i]; 
        }
        return result;
    }