Python Check whether the given List forms Contiguous Distinct SubArray or Not (original) (raw)

Last Updated : 26 Apr, 2023

You are given an array consisting of elements in the form A1, A2, A3…….An. The task is to find whether the array can be formed as a Contiguous Distinct Sub Array or Not. You need to find whether the array can be converted to contiguous sub-arrays that consist of similar elements and there are a distinct number of each element.

The elements once encountered should not appear later in the array as it would not be contiguous

Example:

Input: [ 1 1 3 6 6 6 ]
Output: YES
Explanation:
The given elements of the can be converted to Contiguous Distinct Set Array in the form of [1, 1] [3] [6, 6, 6]and also
no. of 1’s = 2
no. of 3’s = 1
no. of 6’s = 3
which are distinct

Input:[ 1 1 3 5 2 2 2 3 ]
Output: NO
Explanation:
The given elements of the cannot be converted to Contiguous Distinct Set Array as sub array [3 5 2 2 2 3]
violates the condition(elements need to be contiguous) 3 again appears after 5 and 2.

Input **:**[9 9 4 4 4 1 6 6]
Output: NO
Explanation:
The given elements of the cannot be converted to Contiguous Distinct Set Array
It is of the form [9, 9] [4, 4, 4] [1] [6, 6] So the elements are present contiguous
But the no. of 9’s=2 which is also equal to the no. of 6’s=2
Hence the no.s of different elements of the array are not Distinct
hence the Answer is NO

Solution:

Python3

from collections import Counter

def contig_distinct_setarr(l, n):

`` c = Counter(l)

`` a = list ( set (l))

`` b = []

`` flag = True

`` for j in c.values():

`` if j not in b:

`` b.append(j)

`` else :

`` print ( "NO" )

`` flag = False

`` break

`` if (flag ! = False ):

`` i, k = 0 , 0

`` while k< len (a):

`` cou = c[a[i]]

`` x = l.index(a[i])

`` temp = (l[x:x + cou])

`` if len (temp) ! = c[a[i]]:

`` print ( "NO" )

`` flag = False

`` break

`` k + = 1

`` i + = 1

`` if flag = = True :

`` print ( "YES" )

n = 6

l = [ 1 , 1 , 3 , 6 , 6 , 6 ]

contig_distinct_setarr(l, n)

Output:

YES

Another approach without using any module –

In this approach, we will not use any external module like collections. We will simply use a dictionary to store the occurrence of each element along with the element. The elements will work as the key of the dictionary and its respective occurrences will act as the value. Then we will use a variable to store all the values in a list format, then convert the list into a set and compare their lengths. Now if the length is the same it means none of the elements have the same number of occurrences in the given list.

Example –

**Input:**[ 1 1 3 6 6 6 ]
Output: YES
**Explanation:**We will first initialize a blank dictionary to count the occurrence of each element and store them as key:value pair. We will iterate over the given array once and check if the element is already inside of that dictionary or not. If not then we will add it into the dictionary and make the count as 1, otherwise we will just increase the count.

For this given array –

FIrst Iteration – 1 gets into the blank dictionary and the count becomes 1 i.e {1:1}

Second Iteration – 1 is already present in the dictionary so the count increases by 1 i.e {1:2}

Third Iteration – new element 3 gets inserted into the dictionary with count 1 i.e {1:2,3:1}

Fourth Iteration – new element 6 gets inserted into the dictionary with count 1 i.e {1:2,3:1,6:1}

Fifth and Sixth Iteration – The count of 6 incease once each iteration and final dictionary becomes –

{1:2,3:1,6:3}

Now if we see carefully the values of the dictionary is 2,1 and 3 which are distinct. Therefore our program returns True or YES.

Python3

def count_occurrence(arr : list ()) - > bool :

`` dic = {}

`` for i in arr:

`` if i not in dic:

`` dic[i] = 1

`` else :

`` dic[i] + = 1

`` vals = list (dic.values())

`` set_vals = set (vals)

`` return len (vals) = = len (set_vals)

lst_1 = [ 9 , 9 , 4 , 4 , 4 , 1 , 6 , 6 ]

lst_2 = [ 1 , 1 , 3 , 6 , 6 , 6 ]

print (count_occurrence(lst_1))

print (count_occurrence(lst_2))

Time Complexity – O(n), where n is the size of the list

Space Complexity – O(n), space used to create dictionary

Similar Reads