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. :)