Java O(n^2) and O(1) space


  • 0
    A

    '''
    public class Solution {
    public boolean 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 = sum + nums[j];
                if((sum > 0 && k!=0 ) && ( sum % k == 0))
                    return true;
                else if(sum == 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.