Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, July 23, 2017

Subarray with given sum Geeks solution


Problem :


Given an unsorted array of non-negative integers, find a continuous sub-array which adds to a given number


Idea :


Initialize a variable start which tracks on the index from where sum is going to be founded.

If sum is greater than required then remove starting elements until sum is less than required , if sum found print index.


Code:


import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
st:
while(t-->0)
{
   int n=ab.nextInt();
   int k=ab.nextInt();
   int arr[]=new int[n];
   for(int i=0;i<n;i++)
   arr[i]=ab.nextInt();
   int sum=arr[0],start=0;
   for(int i=1;i<=n;i++)
   {
       while(sum>k && start<i)
       sum-=arr[start++];
       if(sum==k)
       {
           System.out.println(start+1+" "+(i));
           continue st;
       }
       if(i<n)
       sum+=arr[i];
   }
   System.out.println("-1");
}
}
}

0 Comments:

Post a Comment

Stats