We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all 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];
}