Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4408 |

@sanjay05 | 3762 |

@mantu_singh | 3666 |

@gfgaccount | 3605 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 953 |

@kanewilliamson123 | 894 |

@pradeeppatidar1999 | 868 |

@akhileshkumar562002 | 862 |

@wallflowe101 | 826 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@nikhilkushwah280 | 796 |

Complete Leaderboard |

Given an unsorted array **A **of size **N** that contains only non-negative integers, find a continuous sub-array which adds to a given number **S**.

**Example 1:**

**Input:
**N = 5, S = 12
A[] = {1,2,3,7,5}
**Output: **2 4**
Explanation: **The sum of elements
from 2nd position to 4th position
is 12.

**Example 2:**

**Input:
**N = 10, S = 15
A[] = {1,2,3,4,5,6,7,8,9,10}
**Output: **1 5**
Explanation: **The sum of elements
from 1st position to 5th position
is 15.

**Your Task:**

You don't need to read input or print anything. The task is to complete the function **subarraySum**() which takes arr, N and S as input parameters and returns a list containing the starting and ending positions of the first such occurring subarray from the left where sum equals to S. The two indexes in the list should be according to 1-based indexing. If no such subarray is found, return an array consisting only one element that is -1.

**Expected Time Complexity: **O(N)

**Expected Auxiliary Space: **O(1)

**Constraints:**

1 <= N <= 10^{5}

1 <= A_{i} <= 10^{10}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Subarray with given sum

...