Very simple solution with O(1) space


  • 0
    N
        public bool CheckSubarraySum(int[] nums, int k) 
        {
            if(nums.Length < 2)
            return false;
            
            int sum=0;
            for(int i=0; i< nums.Length; i++)
            {
                sum = nums[i];
                for(int j= i+1; j< nums.Length; j++)
                {
                    sum+=nums[j];
                    
                    if(k==0)
                    {
                        return sum == 0;
                    }
                    
                    else if(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.