Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- pair<int, int> subArrOfGivenSum(const vector<int> &nums, int target) {
- unordered_map<int, int> indexOf;
- indexOf[0] = -1;
- int prefSum = 0;
- for (int i = 0; i < (int)nums.size(); i++) {
- prefSum += nums[i];
- int diff = prefSum - target;
- if (indexOf.count(diff)) {
- return make_pair(indexOf[diff] + 1, i);
- }
- indexOf[prefSum] = i;
- }
- return make_pair(-1, -1);
- }
- int main() {
- vector<int> nums = {1, 1, 5, 2, 1, 3, 10, 2, 1};
- pair<int, int> ans = subArrOfGivenSum(nums, 21);
- cout << ans.first << ' ' << ans.second << endl;
- unordered_map<int, int> mp;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement