Table of Contents

# Continuous Subarray Sum – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Given an integer array `nums`

and an integer `k`

, return `true`

*if *`nums`

* has a continuous subarray of size at least two whose elements sum up to a multiple of*

`k`

*, or*

`false`

*otherwise*.

An integer `x`

is a multiple of `k`

if there exists an integer `n`

such that `x = n * k`

. `0`

is **always** a multiple of `k`

.

**Example 1:**

Input:nums = [23,2,4,6,7], k = 6Output:trueExplanation:[2, 4] is a continuous subarray of size 2 whose elements sum up to 6.

**Example 2:**

Input:nums = [23,2,6,4,7], k = 6Output:trueExplanation:[23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.

**Example 3:**

Input:nums = [23,2,6,4,7], k = 13Output:false

**Constraints:**

`1 <= nums.length <= 10`

^{5}`0 <= nums[i] <= 10`

^{9}`0 <= sum(nums[i]) <= 2`

^{31}- 1`1 <= k <= 2`

^{31}- 1

# C++ Continuous Subarray Sum LeetCode Solution

```
``````
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = 0, pre = 0;
unordered_set<int> modk;
for (int i = 0; i < n; ++i) {
sum += nums[i];
int mod = k == 0 ? sum : sum % k;
if (modk.count(mod)) return true;
modk.insert(pre);
pre = mod;
}
return false;
}
};
```

# Java Continuous Subarray Sum LeetCode Solution

```
``````
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>(){{put(0,-1);}};;
int runningSum = 0;
for (int i=0;i<nums.length;i++) {
runningSum += nums[i];
if (k != 0) runningSum %= k;
Integer prev = map.get(runningSum);
if (prev != null) {
if (i - prev > 1) return true;
}
else map.put(runningSum, i);
}
return false;
}
```

# Python 3 Continuous Subarray Sum LeetCode Solution

```
``````
class Solution():
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
dic = {0:-1}
summ = 0
for i, n in enumerate(nums):
if k != 0:
summ = (summ + n) % k
else:
summ += n
if summ not in dic:
dic[summ] = i
else:
if i - dic[summ] >= 2:
return True
return False
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1