Prateek wants to give a party to his N friends on his birthday, where each friend is numbered from 1 to N. His friends are asking for a gift to come to the party, instead of giving him one. The cost of the gifts are given in the array Value where ith friend asks for a gift which has a cost Costi.
But, Prateek has only X amount of money to spend on gifts and he wants to invite his friends which are in continuous range such that sum of the cost of the gifts of those friends will be exactly equal to X.
If he can invite his friends, who can satisfy the above condition then, print YES otherwise print NO.
The first line contains a single integer T, denoting the number of test cases. In each test case, the following input will be present: - The next line contains two space-separated integers N and X, where N represents the number of friends and X represents amount of money which Prateek can spend on gifts.
- Next N line contains N integers, where ith line contains ith integer, which represents the Costi .
1 <= T <= 10
1 <= N , Costi <= 106
1 <= X <= 1012
Sample Input(Plaintext Link)
Sample Output(Plaintext Link)
public static void counCondition()
Scanner sc=new Scanner(System.in);
String nextLine = sc.nextLine();
String input = nextLine.split("\\s+");
int p = Integer.parseInt(input);
int c = Integer.parseInt(input);
int Cost=new int[p];
Can anyone guide me why?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at app.HackerEarth25Dec2015.counCondition(HackerEarth25Dec2015.java:18)
The exception is occurring because you are calling
next() method not the
next() method return the next token which
5 here not the complete line.
Also I would suggest you to use the
nextInt() from the Scanner instead of getting the String and splitting and parsing it. That would be a cleaner way of doing things.
int p = sc.nextInt(); int c = sc.nextInt();
Also your logic is also not correct. I hope you will find the logical error and fix it.
You can use the below code for fixing the issue.
sc.nextLine(); // This is to read the `\n` left from previous `nextInt()` String nextLine = sc.nextLine(); String input = nextLine.split("\\s+");