Last Updated: 13 Oct, 2020

Consecutive elements

Moderate
Asked in companies
QuikrWalmartTesla

Problem statement

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:
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 
Then the T test cases follow.

The first line of each test case contains an integer 'N', representing the length of the array.

The next line contains 'N' single space-separated integers representing elements of the array.
Output Format :
For each test case, print “True” or “False” in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 10^5
0 <= arr[i] <= 10^9

Time Limit: 1 sec

Approaches

01 Approach

  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. For every element between the minimum and maximum number, check whether it is present in the array or not by using linear search.
  4. If it is present, then continue, otherwise return false.
  5. Finally, If we have reached the maximum element that means all elements between the minimum element and the maximum element is present thus return true.

02 Approach

  1. Sort the array in increasing order.
  2. Get the minimum element (arr[0]) and the maximum element (arr[N-1]) of the array.
  3. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  4. Traverse the array and check if the current element is 1 less than the next element or not, if it is not then the array is not consecutive, thus return false.
  5. Finally, If we have reached the end of the array that means all elements between the minimum element and the maximum element is present thus return true.

03 Approach

  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. Create a visited array that will store true at index i if (arr[i]-min) is present.
  4. Traverse the array and check if visited[arr[i]-min] is true or false, if it is true that means there is the repetition of element arr[i], hence return false.
  5. Otherwise, mark visited[arr[i]-min] false.
  6. Finally, if we have reached the end of the array that means all elements between the minimum element and the maximum element is present thus return true.

04 Approach

  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. For each element, check arr[i]-min is negative or not.
  4. If it is negative then check if arr[-arr[i]-min] is negative then it means that current element is already present, so return false.
  5. Otherwise, mark arr[-arr[i]-min] negative.
  6. Else check, if arr[arr[i]-min] is negative then it means that current element is already present, so return false.
  7. Otherwise, mark arr[arr[i]-min] negative.
  8. Finally, if we have reached the end of the array that means all elements between the minimum element and the maximum element is present thus return true.

05 Approach

  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. Create a variable arraySum of long type to store the sum of elements of the array and subtracting min from all the elements.
  4. Traverse the array and update arraySum by arr[i]-min.
  5. Now, calculate the sum of the first N-1 natural numbers using formula.
  6. If both sums are equal then return true, otherwise, return false.