I've the below code.
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
public class Scalar {
public static void main(String args[]) throws Exception {
Scanner sc=new Scanner(new File("D:/GC/Scalar/A-small.in"));
int testcases=Integer.parseInt(sc.next());
System.out.println(testcases);
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
int size=Integer.parseInt(sc.next());
System.out.println(size);
for(int i=0;i<size;i++)
{
a.add(sc.nextBigInteger());
}
for(int i=0;i<size;i++)
{
b.add(sc.nextBigInteger());
}
Collections.sort(a);
System.out.println(a.size());
System.out.println(a);
Collections.sort(b,Collections.reverseOrder());
System.out.println(b);
BigInteger sum;
for(int i=0;i<a.size();i++){
sum=a.get(i).multiply(b.get(i));
sum=sum.add(sum);
}
}
}
}
and the below content in a text file.
1000
3
1 -5 3
-2 1 4
5
5 4 3 1 2
1 1 0 1 0
7
677 463 -569 516 401 -998 882
890 588 959 909 948 -617 -655
8
-912 937 167 366 -222 -397 190 -216
354
Here i'm trying to sort the first array and the second in reverse order and then do sum and product, here i took only 2 cases and the 1000 in above input is total number of test cases, and the single number lines denote the array size, and in my program to be sure that the size of the array matches the size given, i'm printing the size, here in the first case, the input is of size 3 and i get it correct but in the second case the input is of size 5 but i'm getting it 8 as size of array, and below is the output that i get.
1000
3
3
[-5, 1, 3]
[4, 1, -2]
5
8
[-5, 1, 1, 2, 3, 3, 4, 5]
[4, 1, 1, 1, 1, 0, 0, -2]
please let me know where am i going wrong.
Thanks
This is the problem:
ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();
for(int j=0;j<2;j++){
// Stuff
}
You're reusing the same lists for each test - so you're adding the values from the second test to the lists which already contain the data for the first test.
Options:
Given that you don't logically want to keep the lists from one test to another, I'd just change the code to:
for(int j = 0; j < 2; j++){
List<BigInteger> a = new ArrayList<BigInteger>();
List<BigInteger> b = new ArrayList<BigInteger>();
// Populate the lists, etc.
}
Note that this also makes it easier to extract the whole operation of "read the size, read the data, sort it" into a separate method.
Also, I suspect you want to change your loop to:
for(int j = 0; j < testcases; j++) {
... otherwise you'll stop after two tests.
If you're using Java 7, you can use type inference to make the ArrayList
creation simpler, too:
List<BigInteger> a = new ArrayList<>();
... and you might even consider extracting the "read n
numbers into a list" into a separate method, so that your code would look something like:
int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.
Also consider using Scanner.nextInt()
instead of your explicit Integer.parseInt
calls.
See more on this question at Stackoverflow