Continuous Sub Array Sum (Leet Code)

bool checkSubarraySum(vector<int>& n, int k) 
{
unordered_set<int> _remainders;
int prevRemainder = 0;
int s = 0;

for (int i = 0; i < n.size(); i++)
{
s += n[i];

int remainder = k == 0 ? s : s % k;

if (_remainders.count(remainder) != 0) return true;


_remainders.insert(prevRemainder);
prevRemainder = remainder;
}


return false;

}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

OmniFlix Network — Community Update #38

API Testing with Postman — Getting Started

API vs MVC — which one to choose?

Upcasting in Java

Track Fitness Application using Huawei Health Kit

Strings in JAVA

Chingu Voyage-11 teaser & extra info

Make Oh My Zsh history play nicely with WebStorm / IntelliJ

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Satyendra Mishra

Satyendra Mishra

More from Medium

Generics & Open recursion

How to call a command or a script from BusinessWorks or BusinessWorks Container Edition

Logical vs. Syntactical Errors in Code

THE COMPILATION PROCESS in C

Source code.