The Wayback Machine - https://web.archive.org/web/20211213163227/https://www.geeksforgeeks.org/minimum-length-of-subarray-in-given-ternary-array-having-0-as-the-majority-element/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum length of subarray in given Ternary Array having 0 as the majority element

  • Last Updated : 13 Dec, 2021

Given an integer array arr[] of size n with only three types of integers 0’s, 1’s, and 2’s. Find the minimum length of the subarray of the array arr[] of length >=2, such that it has the frequency of 0’s greater than both 1’s and 2’s. If not found print -1. 

Input : arr[] = {2, 0, 2, 0, 1, 2, 2, 2}
Output : 3
Explanation: {0, 2, 0} from index [2, 4] is the subarray with frequencies of 0’s greater frequencies of 1’s and 2’s.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {2, 2, 2, 2}
Output: -1 



 

Naive Approach: This problem can be done by going through every subarray and checking the frequencies of 0’s, 1’s and 2’s and checking if the frequency of 0’s is greater than both 2’s and 1’s then storing the minimum length of subarray as the answer.

Time Complexity: O(n*n)
Auxiliary Space: O(1)

Efficient Approach: Since there are only 3 types of integers the possible subarrays of length >=2 satisfying the above condition would be 

{0, 0}, {0, 1, 0}, {0, 2, 0}, {0, 1, 2, 0}, {0, 2, 1, 0}, {0, 0, 0, 1, 1, 2, 2}, ….

The maximum possible minimum length of subarray would be 7. Any other subarray which satisfies the above condition with length > 7 would contain any of the above subarrays in it so the maximum possible length of subarray which satisfies the above condition is 7.

 Follow these steps to solve this problem:

  • Initialize a variable min_length as INT_MAX
  • Iterate in the range [0, n) using the variable i and perform the following tasks:
    • Initialize an array count[] with initial frequencies 0
    • Increment the frequency of arr[i] uisng count[arr[i]]++.
    • Iterate in the range [i+1, min(n, i+7)) using the variable j and perform the following tasks:
      • Increment the frequency of arr[j] by using count[arr[j]]++ of each subarray of size <=7.
      • If the count[0] is greater than both count[1] and count[2] and if min_length > j-i+1 then assign min_length = j-i+1
  • If min_length is equal to INT_MAX return -1.
  • Else print min_length as the answer.

Below is the implementation of the above approach.

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum
// subarray length with most 0's
int minLength(vector<int> arr)
{
    int min_len = INT_MAX;
    int n = arr.size();
  
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n; i++) {
  
        // Initialize a vector count
        // to store the frequencies
        int count[3] = { 0 };
        count[arr[i]]++;
  
        // Check all subarrays of length
        // <=7 and count the frequencies
        for (int j = i + 1; j < min(n, i + 7); j++) {
            count[arr[j]]++;
  
            // If the frequency of 0's is
            // greater than both 1's and 2's
            // then take length of minimum subarray
            if (count[0] > count[1]
                && count[0] > count[2])
                min_len = min(min_len, j - i + 1);
        }
    }
  
    // If min_len == INT_MAX we have no subarray
    // satisfying the condition return -1;
    if (min_len == INT_MAX)
        min_len = -1;
  
    return min_len;
}
  
// Driver Code
int main()
{
  
    // Initializing list of nums
    vector<int> arr = { 2, 0, 2, 0, 1, 2, 2, 2 };
  
    // Call the function and print the answer
    cout << (minLength(arr));
  
    return 0;
}
Output
3

Time Complexity: O(n)
Auxiliary Space: O(1) 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!