Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check every sublist in a list containing at least one unique element in Python
Suppose we have a list of elements called nums, we have to check whether every sublist has at least 1 element in it that occurs exactly once in the sublist or not. We have to solve this problem in linear time.
So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be True, because every sublist in nums has at least one element which has occurred only once. [[5], [10], [20], [10], [0], [5,10], [10,20], [20,10], [10,0], [5,10,20], [10,20,10], [20,10,0], [5,10,20,10], [10,20,10,0], [5,10,20,10,0]] all have at least one element whose frequency is 1.
Algorithm Steps
To solve this, we will follow these steps −
- Define a function
has_unique(). This will take left, right - if left >= right, then
- return True
- counts := a dictionary containing frequencies of each elements present in nums[from index left to right]
- if minimum frequency from counts > 1, then
- return False
- start := left
- for index in range left to right, do
- if counts[nums[index]] is same as 1, then
- if has_unique(start, index - 1) is false, then
- return False
- start := index + 1
- if has_unique(start, index - 1) is false, then
- if counts[nums[index]] is same as 1, then
- return has_unique(start, right)
- From the main method, return has_unique(0, size of nums - 1)
Example
Let us see the following implementation to get better understanding −
from collections import Counter
def solve(nums):
def has_unique(left, right):
if left >= right:
return True
counts = Counter(nums[left : right + 1])
if min(counts.values()) > 1:
return False
start = left
for index in range(left, right + 1):
if counts[nums[index]] == 1:
if not has_unique(start, index - 1):
return False
start = index + 1
return has_unique(start, right)
return has_unique(0, len(nums) - 1)
nums = [5, 10, 20, 10, 0]
print(solve(nums))
The output of the above code is −
True
How It Works
The algorithm uses a recursive approach with the following logic −
- For each sublist, count the frequency of all elements using
Counter - If no element appears exactly once (minimum frequency > 1), return False
- Otherwise, find elements that appear exactly once and use them as "dividers"
- Recursively check the segments between these unique elements
Testing with Different Examples
from collections import Counter
def solve(nums):
def has_unique(left, right):
if left >= right:
return True
counts = Counter(nums[left : right + 1])
if min(counts.values()) > 1:
return False
start = left
for index in range(left, right + 1):
if counts[nums[index]] == 1:
if not has_unique(start, index - 1):
return False
start = index + 1
return has_unique(start, right)
return has_unique(0, len(nums) - 1)
# Test cases
test_cases = [
[5, 10, 20, 10, 0],
[1, 1, 1, 1],
[1, 2, 3],
[1, 2, 1, 2]
]
for nums in test_cases:
result = solve(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
print()
The output of the above code is −
Input: [5, 10, 20, 10, 0] Output: True Input: [1, 1, 1, 1] Output: False Input: [1, 2, 3] Output: True Input: [1, 2, 1, 2] Output: False
Conclusion
This recursive algorithm efficiently checks if every sublist contains at least one unique element by using frequency counting and divide-and-conquer strategy. The approach ensures linear time complexity by processing each element once.
