Finding the largest subarray with equal number of 0's and 1's

后端 未结 10 1943
我在风中等你
我在风中等你 2020-12-31 11:30

So, I have an array containing only 0\'s and 1\'s. I have to find out the largest subarray containing equal number of 0\'s and 1\'s. One can be a naive approach have complex

相关标签:
10条回答
  • 2020-12-31 12:17

    Hash map implementation in O(n) time.

    int count=0, max_length=0;
            unordered_map<int,int> mp;
            mp[0] = -1;
    
            for(int i=0; i<nums.size(); i++)
            {
                if(nums[i] == 0) count += -1;
                if(nums[i] == 1) count += 1;
    
                if(mp.find(count) != mp.end())
                    max_length = max(max_length, i-mp[count]);
                else
                    mp[count] = i;                        
            }
    
            return max_length;
    

    Hope this code block helps.

    0 讨论(0)
  • 2020-12-31 12:19
    int maxLen(int nums[], int n)
    {   
        unordered_map<int, int> m;
        m[0] = -1;
        int ans = 0, cur = 0;
        for (int i = 0; i < n; ++i) {
            cur += nums[i] ? 1 : -1;
            if (m.count(cur))
                ans = max(ans, i - m[cur]);
            else
                m[cur] = i;
        }
        return ans;
    }
    
    0 讨论(0)
  • 2020-12-31 12:19
    class Solution {
    public:
        int findMaxLength(vector<int>& nums) {
            int n=nums.size();
            if(n<=1) return 0;
            vector<int> arr(n,-1);
            arr[0]= (nums[0]==0)? -1:1 ;
    
            for(int i=1;i<n;i++)
            {
                arr[i] = arr[i-1] + ((nums[i]==0)? -1 : 1) ;
            }
    
            int sol=0;
            unordered_map<int,int> mp;
            for(int i=0;i<n;i++)
            {
                if(arr[i]==0) sol = i+1;
                else
                {
                    if(mp.find(arr[i])==mp.end())
                    {
                        mp[arr[i]]=i;
                    }
                    else
                    {
                        sol=max(sol,i-mp[arr[i]]);
                    }
                }
            }
            return sol;
        }
    };
    
    0 讨论(0)
  • 2020-12-31 12:21

    Inspired from @templatetypedef algorithm i wrote code in python, hope it can be helpful for someone.

    def check_max_length(input_array):
        length = len(input_array)
        mapping_dict = {}
        max_length = 0
        for i in range(length):
            if input_array[i] not in mapping_dict.keys():
                mapping_dict[input_array[i]] = i
            else:
                max_length = max(max_length,i-mapping_dict[input_array[i]])
        return max_length
    
    def find_max_substring_length(input_string):
        def_array = [0]
        zero_count = 0
        one_count = 0
        # difference between number of zeroes and ones
        def_zero_one = 0
        for i in range(len(input_string)):
            if input_string[i] == '1':
                one_count+=1
            else:
                zero_count+=1
    
            def_array.append(one_count-zero_count)
        max_substring = check_max_length(def_array)
    return max_substring
    
    input_string = '1000100'
    substring_length = find_max_substring_length(input_string)
    print(substring_length) // will give result as 2
    
    0 讨论(0)
提交回复
热议问题