Heights of Frustration :(

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 :frowning: 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);
}

}

In your

main

method, maybe you need to use

System.exit(0)

when the program is done. Maybe your class has to be named

Main

instead of

sumofpairs

.

If none of the above works, maybe your program throws an exception when it is being tested.

If none of the above works, you could try using the

java.util.Scanner

class instead of your

InputReaders

.

You could replace the throws Exception with throws IOException to know whether the Runtime error is caused while input or while code execution further you can implement try-catch blocks to localize the point of exception in your code.

Your class cannot be public, use

class sumofpairs {
    ...
}

:wink:

Thanks for your response, but while uploading the program i always change the name to Main and upload… it still shows runtime error !
and also i never use Scanner coz its much slower plus its sure to give a TLE ! and since i’m using java, i don’t think i’ll explicitly have to give System.exit(0) and i’ve never done that before when i had my solutions accepted by the codechef compiler !!
Still not able to figure out whats wrong !! :frowning:

java experts !!! kindly tell me the problem with the code :frowning:

Thanks for your response,
Sure i’ll try that out :slight_smile:

yeah but my previous codes have always been accepted even though i used ‘public’,
But still thanks for your response and i’ll sure try that out !