Hello Guys,
I’ve been trying to solve this problem ‘sum of pairs’ http://www.codechef.com/problems/PAIRSUM, I’m done with the code and
I’ve checked it with tons and tons of inputs, it seems to work quite fine and i’ve read the question a thousand times to see if i’ve made any mistake! I’m just not able to figure out whats wrong with the code ! , i’m getting RUNTIME ERROR (NZEC) , for some odd reason i’m getting an exception thrown during run time ! i’ve tried a lot to fix it ! it just gets me down i’m not able to find the problem , I’ll be glad if any of you guys could provide some assistance !
Thanks in advance.
public class sumofpairs {
public static void main(String[] args) throws Exception {
InputStream input = System.in;
InputReaders br = new InputReaders(input);
int length, i, j, index, temp, max = 0;
length = br.readInt();
ArrayList<Integer> sums = new ArrayList<Integer>();
int[] numbers = new int[length];
int[] count = new int[5050];
for (i = 0; i < length; i++) {
numbers[i] = br.readInt();
}
for (i = 0; i < length; i++) {
for (j = i + 1; j < length; j++) {
temp = numbers[i] + numbers[j];
if ((index = contains(sums, temp)) == -1) {
sums.add(temp);
} else {
count[index]++;
if (max < count[index])
max = count[index];
}
}
}
max = max * 2 + 2;
System.out.println(max);
}
public static int contains(ArrayList<Integer> sum, int key) {
int index;
Iterator<Integer> i = sum.iterator();
for (index = 0; i.hasNext(); index++) {
if (i.next() == key)
return index;
}
return -1;
}
}
class InputReaders {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReaders(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}