Code throwing index out of bounds exception

https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1#

class Solution
{
//Function to find a continuous sub-array which adds up to a given number.
static ArrayList subarraySum(int[] arr, int n, int s)
{
int i=0;
int j=0;
int k=0;
int sum=arr[i];
while(sum!=s){

        if(sum<s){
            j++;
            sum+=arr[j];
            if(sum==s)break;
        }
        else{
            sum-=arr[i];
            i++;
            if(sum==s)break;
        }
        
        
    
    }
    ArrayList<Integer> ans=new ArrayList<Integer>();
    ans.add(i+1);
    ans.add(j+1);
    return ans;

}

}

hi @v13verma
refer this -->

hi @v13verma
is it clear now??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.