heck if array contains contiguous integers with duplicates allowed

Easy Accuracy: 65.11% Submissions: 761 Points: 2


Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”.

Example 1:

​Input : arr[ ] = {5, 2, 3, 6, 4, 4, 6, 6}
Output : Yes
Explanation:
The elements  of array form a contiguous 
set of integers which is {2, 3, 4, 5, 6} 
so the output is "Yes".


Example 2:

Input : arr[ ] = {10, 14, 10, 12, 12, 
                              13, 15} 
Output : No



Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function areElementsContiguous() that takes an array (arr), sizeOfArray (n), and return the true if it is a set of contiguous integers else print false. The driver code takes care of the printing.



class Solution:

    def areElementsContiguous (self, arr, n):

        arr = sorted(arr)

        j = arr[0]

        for i in arr:

            if(j == i):

                j+=1

    

        if(j >= arr[n-1]):

            return 1

        else:

            return 0


Comments

Popular posts from this blog

Large Factorial of array

LEARN SOMMETHING INTRESTING

Value equal to index value