Python – Check whether the given List forms Contiguous Distinct Sub-Array or Not

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


Input:[ 1 1 3 6 6 6 ]
Output: YES
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
The given elements of the cannot be converted to Contiguous Distinct Set Array as sub arrray [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
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







from collections import Counter
# function to check contiguous 
# distinct set arrray
def contig_distinct_setarr(l, n):
    c = Counter(l)
    a = list(set(l))
    b =[]
    flag = True
    for j in c.values():
        # iterating and moving it to another
        # array if already present we print NO
        # when it finds no. of different elements 
        # to be equal if no. of x's = no. of y's
        # So we break of the loop
        if j not in b:
            flag = False
    # If already there are similar elements
    # in c.values()- the count of elements
    # flag = False and we dont need to check
    # the below condition If not flag = False 
    # then the iterate through the array loop
    if (flag != False):
        i, k = 0, 0
        # for each elements in set a
        # cou stores the count of a[i]
        while k<len(a):
            cou = c[a[i]]
            x = l.index(a[i])
            # here we extract thecontiguous
            # sub array of length equal to 
            # the count of the element 
            temp =(l[x:x + cou])
            # if the number of elements of
            # subsequences is not equal to
            # the value of element in the 
            # dictionary print NO
            if len(temp) != c[a[i]]:
                flag = False
            k+= 1
            i+= 1
        # if we have iterated all over the array 
        # and the condition is satisfied we print
        # YES
        if flag == True:
# initialize the array and its length
n = 6
l =[1, 1, 3, 6, 6, 6]
contig_distinct_setarr(l, n)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.