Help in Subarray with given Sum

Ques link :
Subarray with given sum | Practice | GeeksforGeeks

I want to solve this question by STL only and mainly by unordered_map.
According to me solution is in O(N) only.
But I am getting TLE. I think find() function’s time complexity going from O(1) to worst case!!?

Here is my attempt:
Online Compiler and IDE - GeeksforGeeks

So how to solve this question efficiently w/o Sliding Window technique?

unordered_map::find is O(N) in the worst case:

and some challenge authors will exploit this.

1 Like

@ssjgz so there is no way to easily do this with unordered_map! we need to switch over to sliding window?? or there is any other easy method?

Dunno :slight_smile: Why not just try a std::map?

1 Like

@ssjgz bro map also giving tle!! i am so confused now!!

10^7 is way too much for a map.
Hint : All elements are positive.

1 Like

actually i am a new to cp… so don’t know many of the cool algos so can you pls guide me? @everule1