Subarray with given sum
PROBLEM :
Given an unsorted array of non-negative integers, find a continuous sub-array which adds to a given number.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.
Output:
Corresponding to each test case, in a new line, print the starting and ending positions of first such occuring subarray if sum equals to subarray, else print -1.
Note: Position of 1st element of the array should be considered as 1.
Expected Time Complexity: O(n)
Constraints:
1 = T = 100
1 = N = 100
1 = an array element = 200
Example:
Input:
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output:
2 4
1 5
Explanation :
In first test case, sum of elements from 2nd position to 4th position is 12
In second test case, sum of elements from 1st position to 5th position is 15
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
// Following solution work for Negative Nos too
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no,target ;
cin>>no>>target ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
unordered_map<int,int> map ;
int sum=0 ;
bool flag=true ;
for(int i=0;i<no;i++)
{
sum+=arr[i] ;
if(sum==target)
{
flag=false ;
cout<<1<<" "<<i+1 ;
break ;
}
if(map.find(sum-target)!=map.end())
{
flag=false ;
cout<<map[sum-target]+2<<" "<<i+1 ;
break ;
}
map[sum]=i ;
}
if(flag)
cout<<-1 ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Given an unsorted array of non-negative integers, find a continuous sub-array which adds to a given number.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.
Output:
Corresponding to each test case, in a new line, print the starting and ending positions of first such occuring subarray if sum equals to subarray, else print -1.
Note: Position of 1st element of the array should be considered as 1.
Expected Time Complexity: O(n)
Constraints:
1 = T = 100
1 = N = 100
1 = an array element = 200
Example:
Input:
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output:
2 4
1 5
Explanation :
In first test case, sum of elements from 2nd position to 4th position is 12
In second test case, sum of elements from 1st position to 5th position is 15
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
// Following solution work for Negative Nos too
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no,target ;
cin>>no>>target ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
unordered_map<int,int> map ;
int sum=0 ;
bool flag=true ;
for(int i=0;i<no;i++)
{
sum+=arr[i] ;
if(sum==target)
{
flag=false ;
cout<<1<<" "<<i+1 ;
break ;
}
if(map.find(sum-target)!=map.end())
{
flag=false ;
cout<<map[sum-target]+2<<" "<<i+1 ;
break ;
}
map[sum]=i ;
}
if(flag)
cout<<-1 ;
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Comments
Post a Comment