Very naive (O(n^2)) and easy-to-understand solution using cumulative sum and moving window


  • 0
    T
    class Solution {
        public boolean checkSubarraySum(int[] nums, int k) {
            for(int i = 0; i < nums.length; i++)
            {
                int sum = nums[i];
                for(int j = i+1; j < nums.length; j++)
                {
                    sum = sum + nums[j];
                    if (sum == 0 && k == 0) return true;
                    if (k != 0 && sum % k == 0) return true;
                }
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.