time limit exceeded in contiguous array
Anonymous User
135

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

public int findMaxLength(int[] nums) {
        int max = 0;

        for (int i = 0; i < nums.length-1; i++) {
            int z = 0;
            int o = 0;
            int sub = 0;
            if(nums[i] == 0) z++;
            else o++;
            sub++;
            for (int j = i+1; j < nums.length; j++) {
                if(nums[j] == 0) z++;
                else o++;
                sub++;
                if(max < sub && z == o){
                    max = sub;
                }
            }
        }
        return max;
    }

what can be improved to tackle the time limit exceeded issue

Comments (3)