 Jason arora - 4 years ago 299
Java Question

# Output for finding Sum of the subarray do not come as Expected

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.

``````  Input:
``````

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 .

``````Ouput
``````

Output exactly T lines, each containing the answer to the corresponding test case .

``````Constraints:
1 <= T <= 10
1 <= N , Costi <= 106
1 <= X <= 1012

1
5 12
1
3
4
5
2

YES
``````

MyApproach

I followed what is said to take the input and took the subarry sum for every element.But I am not getting Expected output.

``````public static void counCondition()
{
boolean b1=false;
int Sum=0;
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int i=1;i<=T;i++)
{
sc.nextLine();
String nextLine = sc.nextLine();
String[] input = nextLine.split("\\s+");
int p = Integer.parseInt(input);
int c = Integer.parseInt(input);
int Cost[]=new int[p];
for(int j=0;j<Cost.length;j++)
{
Cost[j]=sc.nextInt();
}
for(int k=0;k<Cost.length;k++)
{
for(int l=k;l<p;l++)
{
Sum=Sum+Cost[l];
if(Sum>c)
{
break;
}
else if(Sum==c)
{
b1=true;
break;
}
}
}

if(b1==true)
{
System.out.println("YES");

}
else
{
System.out.println("NO");
}
}
sc.close();
}

**Output**
1
5  12
1
2
3
4
5
6
7
8
9
10
NO
``````

I am not expecting this output.

Can anyone guide me why? Anonymous

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 `nextLine()`. 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+");
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download