Divisible Sum Pairs

Alok :

Though it will be weird to see this question but I really need to understand some core concepts while I continue my journey of coding. I have a problem which is on Hackerrank it goes like : DIVISIBLE SUM PAIRS

I will anyhow give the problem statement here:

Problem Statement

Given an array, where we have to find the number of pairs divisible by the the given number k, and there is one more condition to it, which is : the arr[i] < arr[j] from those pairs.

Example

For example, ar=[1,2,3,4,5,6] and k=5. Our three pairs meeting the criteria are [1,4] [2,3] and [4,6].

Code

Before I publish my code, I would like to tell you that my code has passed all the test cases, and it is accepted to move ahead to the next challenge, but there is a glitch which I'm trying to figure out, which is there in the code.

static int divisibleSumPairs(int n, int k, int[] ar) {
    int count = 0;
    for(int i=0; i<ar.length; i++){
      for(int j=i+1; j<ar.length; j++){
            if(((ar[i]+ar[j])%k)==0){
                if(i < j)
                   count++;
            }
        }
    }
    return count;
}

In here when I do this if(i < j) count++, it gives me the correct result, but as soon as I do this if(ar[i] < a[j]) count++, it is supposedly giving me a wrong answer.

Can anybody help me clearing this out, like what is left. Since I know the check arr[i] < arr[j] should give the correct result. I don't want to proceed with the wrong knowledge.

EDITS

Since I have understood what I was doing wrong. And I have one edits in my code that is not starting the inner loop with 1, since it will start off with 1 every time the inner loop finishes, and again runs. I thank everyone who helped me clearing this and making my concepts strong enough to deal the questions like this.

I personally thank Mike 'Pomax' Kamermans, Ricola, and xerx593 for clearing out my doubts, and giving me the core concepts of looping through the elements. It will help me in future, and I won't repeat that thing again. :)

Ricola :

I just checked your link and the condition given in the question statement is

Find and print the number of (i,j) pairs where i < j and ar[i] + ar[j] is divisible by k.

Which is simply the number of unordered pairs of elements for which the sum is divisible by k.

However you wrote

there is one more condition to it, which is : the arr[i] < arr[j] from those pairs.

I seems like you have misread the question. And it explains why the i<j condition works whereas arr[i] < arr[j] doesn't.


Now that you know that you only need the unordered pairs, no need to iterate j from 1 to ar.length. Since you need j > i, every j between 1 and i (inclusive) is useless. You can simplify your code to:

static int divisibleSumPairs(int n, int k, int[] ar) {
    int count = 0;
    for(int i=0; i<ar.length-1; i++){
      for(int j=i+1; j<ar.length; j++){
            if(((ar[i]+ar[j])%k)==0){
                   count++;
            }
        }
    }
    return count;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111320&siteId=1