Timer is Running

Consecutive Array Elements
Submissions: 36   Accuracy:


  Difficulty: Easy   Marks: 2

Given an unsorted array arr[] of size N, the task is to check whether the array consists of consecutive numbers or not.

Example 1:

Input: N = 5, arr[] = {5, 4, 2, 1, 3}
Output: Yes
Explanation: All are consecutive elements,
according to this order 1,2,3,4 and 5.

Example 2:

Input: N = 6, arr[] = {2, 1, 0, -3, -1, -2}
Output: Yes

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function areConsecutives() that takes array arr[] and integer N as parameters and returns the boolean true if array elements are consecutive else boolean false.

Note: If the elements are consecutive driver code outputs Yes otherwise, it outputs No

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

1 ≤ N ≤ 107

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth
Author: saksham seth

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.