Consecutive elements
Level MEDIUM
You are given an array arr of N non-negative integers, you need to return true if the array elements consist of consecutive numbers otherwise return false.
For Example: If the given array is [4,3,5] then you should return true as all the array elements are in consecutive order.
Input Format:
Output Format :
Note:
Constraints:
Sample Input 1:
Sample Output 1:
Explanation For Input 1:
Sample Input 2:
Sample Output 2:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def isConsecutive(arr, n): | |
# Write your function here | |
s=sorted(arr) | |
for i in range(len(s)-1): | |
if s[i+1]-s[i]==1: | |
pass | |
else: | |
return False | |
return True | |
# Main Code | |
t = int(input().strip()) | |
for i in range(t): | |
N = int(input().strip()) | |
arr = [int(i) for i in input().strip().split()] | |
ans = isConsecutive(arr, N) | |
if ans: | |
print("True") | |
else: | |
print("False") |
Comments
Post a Comment
Please give us your valuable feedback