## Amazon Interview Question

SDE-3s**Country:**India

**Interview Type:**In-Person

```
private int[] findSubSum(int[] a, int s) {
List<Integer> resIds = new ArrayList<>();
int t = 0;
int b = 0;
int c = 0;
for (int i = 0; i < a.length; i++) {
t += a[i]; c++;
if (t == s) {
return new int []{b,c};
} else if (t > s) {
t = c = 0;
i = b; // next loop will advance i
b = b + 1;
}
}
return null;
}
```

```
typedef struct
{
int len;
int *arr;
}subarry;
subarry res;
subarry* find_sub_sum(int arr[],int sum,int len)
{
int temp=0;
int *r_arr,r_i=0;
quick_sort(arr,0,len-1);
r_arr = (int *) malloc(sizeof(int)*len);
for(int i=len-1;i>=0;i--)
{
if(arr[i] > sum)
continue;
if(arr[i]+temp <= sum)
{
temp = arr[i]+temp;
*(r_arr+r_i)=arr[i];
r_i++;
}
}
if(temp == sum)
{
res.len = r_i;
res.arr = r_arr;
return &res;
}
return NULL;
}
void main()
{
int arr[6] = {1,2,3,4,5,6},len = 6;
int sum = 22;
subarry *val;
val = find_sub_sum(&arr,sum,len);
if(val == NULL)
{
printf("subarray not found \n");
return;
}
else
{
for(int i = 0;i<val->len;i++)
{
printf("%d ",*(val->arr+i));
}
printf("\n");
}
}
```

private int[] findSubSum(int[] a, int s) {

- Vu Kien April 12, 2019List<Integer> resIds = new ArrayList<>();

int t = 0;

int b = 0;

int c = 0;

for (int i = 0; i < a.length; i++) {

t += a[i]; c++;

if (t == s) {

return new int []{b,c};

} else if (t > s) {

t = c = 0;

i = b; // next loop will advance i

b = b + 1;

}

}

return null;

}