Python - Minimum identical consecutive Subarray


The problem we have is to find the minimum identical consecutive subarray in the given array and implement this algorithm using Python. So we will use basic functionalities of Python to get the desired result.

Understanding the Problem

In the given problem statement we are required to find the shortest identical consecutive subarray present in the given input array. So we will be having an array with some repetitive items and we have to show the subarray with minimum repetitive items count. For example: suppose we have an array as [0, 0, 2, 2, 2, 3, 3, 3, 3, 5, 5, 5], so in this array 0 is a number which is occurring two times in the given array and other numbers are occurring three and four times. So the result will be 0.

Logic for The Above Problem

To solve this problem we will use the collections module in Python. With the help of this module we will use defaultdict class and make a dictionary of the given array. So by using this dictionary we will be able to find the minimum and maximum values of the array. After that we will find the minimum frequency item in the array and return the item which is having minimum frequency.

What is the collections module in Python?

In Python, there is a collections module which is used to implement specialized container datatypes to provide alternatives to the built in containers like dict, list, set and tuple. Some useful data structures present in this module are namedtuple, counter, defaultdict and ordereddict. So we will be using defaultdict in this program.

The defaultdict will simply create any items which we are trying to access. The defaultdict is also a dictionary-like object and also provides methods which are provided by a dictionary. But the difference between these two is that it takes the first argument as a default data type for the dictionary. The defaultdict never raises a KeyError. It gives a default value for the key that does not exist. For example −

Example

from collections import defaultdict

def default_value():
   return "Not available"
   
# Define the dict
dictry = defaultdict(default_value)
dictry["x"] = 10
dictry["y"] = 20

print(dictry["x"])
print(dictry["y"])
print(dictry["z"])

Output

10
20
Not available

Algorithm

  • Step 1 − As we have to find out the minimum identical consecutive subarray number. So as we have discussed above, first we will import the defaultdict class from the collections module available in python.

  • Step 2 − After importing the necessary modules we will initialize the array items and give the name of the array as array_items. This array will contain some repetitive items to find the minimum identical subarray.

  • Step 3 − And then we will create a dictionary to keep track of the frequency of the repetitive items. We have to get the minimum frequent item as a result.

  • Step 4 − Now we have the frequency of each item present in the array so in this step we need to find the minimum values in the above dictionary.

  • Step 5 − As a result we will find the minimum item with having minimum frequency in the frequency dictionary.

Example

from collections import defaultdict

# initializing the array list
array_items =  [0, 0, 2, 2, 2, 3, 3, 3, 3, 5, 5, 5]

# printing original list
print("The original list is : " + str(array_items))

# create a dictionary for keep tracking of frequency
frequency = defaultdict(int)
for item in array_items:
   frequency[item] += 1

# Get the minimum value in the above dictionary
min_value = min(frequency.values())

# Now find the item with minimum frequency
for item, count in frequency.items():
   if count == min_value:
      min_item = item
      break

# print the result
print("Minimum identical consecutive Subarray number is : " + str(min_item))

Output

The original list is : [0, 0, 2, 2, 2, 3, 3, 3, 3, 5, 5, 5]
Minimum identical consecutive Subarray number is : 0

Complexity

So the time complexity to find the minimum identical consecutive subarray is O(n), in which n is the number of items present in the given input array. The reason for this is that the code iterates the item of the array n times and also we have created a dictionary of size n.

Conclusion

The conclusion is that we have created a code to get the minimum identical consecutive subarray using the collections module present in Python. This is the straightforward method to get the required result with the time complexity of O(n).

Updated on: 16-Oct-2023

21 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements