Java query for given code! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java query for given code!

Heres my solution to find an subarray for a given sum. But its showing error. Whats the problem in my code? I want outpit with this particular approach only. https://code.sololearn.com/c5JYp89Jgqqz/?ref=app

1st May 2019, 12:52 PM
Abhay Jindal
Abhay Jindal - avatar
1 Answer
+ 2
Change this: if(sum>resultSum){ sum=0; left=temp; temp++; } In your code you increment temp and give it to left --> left starts at 2 after the first round but it should start at 1. And maybe it would be a good idea to add the condition left < right in your while loop --> left = right --> no sum found -> while(sum <= resultSum && left <= right)
1st May 2019, 1:40 PM
Denise Roßberg
Denise Roßberg - avatar