Check if array elements are consecutive
Given an unsorted array of numbers, write a function that returns true if the array consists of consecutive numbers.
Examples:
a) If the array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
b) If the array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive numbers from 78 to 83.
c) If the array is {34, 23, 52, 12, 3}, then the function should return false because the elements are not consecutive.
d) If the array is {7, 6, 5, 5, 3, 4}, then the function should return false because 5 and 5 are not consecutive.
Method 1 (Use Sorting)
1) Sort all the elements.
2) Do a linear scan of the sorted array. If the difference between the current element and the next element is anything other than 1, then return false. If all differences are 1, then return true.
C++
#include <bits/stdc++.h>
using namespace std;
bool areConsecutive( int arr[], int n)
{
sort(arr,arr+n);
for ( int i=1;i<n;i++)
{
if (arr[i]!=arr[i-1]+1)
{
return false ;
}
}
return true ;
}
int main()
{
int arr[]= {5, 4, 2, 3, 1, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
if (areConsecutive(arr, n) == true )
cout<< " Array elements are consecutive " ;
else
cout<< " Array elements are not consecutive " ;
return 0;
}
|
Java
import java.util.Arrays;
class AreConsecutive {
boolean areConsecutive( int arr[], int n)
{
Arrays.sort(arr);
for ( int i= 1 ;i<n;i++)
{
if (arr[i]!=arr[i- 1 ]+ 1 )
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
AreConsecutive consecutive = new AreConsecutive();
int arr[] = { 5 , 4 , 2 , 3 , 1 , 6 };
int n = arr.length;
if (consecutive.areConsecutive(arr, n) == true )
System.out.println( "Array elements are consecutive" );
else
System.out.println( "Array elements are not consecutive" );
}
}
|
Python
def areConsecutive(arr, n):
arr.sort()
for i in range ( 1 ,n):
if (arr[i]! = arr[i - 1 ] + 1 ):
return False ;
return True ;
arr = [ 5 , 4 , 2 , 3 , 1 , 6 ]
n = len (arr)
if (areConsecutive(arr, n) = = True ):
print ( "Array elements are consecutive " )
else :
print ( "Array elements are not consecutive " )
|
C#
using System;
class GFG {
static bool areConsecutive( int []arr, int n)
{
Array.Sort(arr);
for ( int i=1;i<n;i++)
{
if (arr[i]!=arr[i-1]+1)
{
return false ;
}
}
return true ;
}
public static void Main()
{
int []arr = {5, 4, 2, 3, 1, 6};
int n = arr.Length;
if (areConsecutive(arr, n) == true )
Console.Write( "Array elements "
+ "are consecutive" );
else
Console.Write( "Array elements "
+ "are not consecutive" );
}
}
|
Javascript
function areConsecutive(arr, n)
{
arr.sort();
for ( var i = 1; i < n; i++)
if (arr[i]!=arr[i-1]+1)
return false ;
return true ;
}
var arr = [5, 4, 2, 3, 1, 6];
var n = arr.length;
if (areConsecutive(arr, n) == true )
console.log( "Array elements are consecutive " );
else
console.log( "Array elements are not consecutive " );
|
Output
Array elements are consecutive
Time Complexity: O(n log n)
Space Complexity: O(1)
Method 2 (Use visited array)
The idea is to check for the following two conditions. If the following two conditions are true, then return true.
1) max – min + 1 = n where max is the maximum element in the array, min is the minimum element in the array and n is the number of elements in the array.
2) All elements are distinct.
To check if all elements are distinct, we can create a visited[] array of size n. We can map the ith element of input array arr[] to the visited array by using arr[i] – min as the index in visited[].
C++
#include<stdio.h>
#include<stdlib.h>
int getMin( int arr[], int n);
int getMax( int arr[], int n);
bool areConsecutive( int arr[], int n)
{
if ( n < 1 )
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
bool *visited = ( bool *) calloc (n, sizeof ( bool ));
int i;
for (i = 0; i < n; i++)
{
if ( visited[arr[i] - min] != false )
return false ;
visited[arr[i] - min] = true ;
}
return true ;
}
return false ;
}
int getMin( int arr[], int n)
{
int min = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
int getMax( int arr[], int n)
{
int max = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr[]= {5, 4, 2, 3, 1, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
if (areConsecutive(arr, n) == true )
printf ( " Array elements are consecutive " );
else
printf ( " Array elements are not consecutive " );
getchar ();
return 0;
}
|
Java
class AreConsecutive
{
boolean areConsecutive( int arr[], int n)
{
if (n < 1 )
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
boolean visited[] = new boolean [n];
int i;
for (i = 0 ; i < n; i++)
{
if (visited[arr[i] - min] != false )
return false ;
visited[arr[i] - min] = true ;
}
return true ;
}
return false ;
}
int getMin( int arr[], int n)
{
int min = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
int getMax( int arr[], int n)
{
int max = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void main(String[] args)
{
AreConsecutive consecutive = new AreConsecutive();
int arr[] = { 5 , 4 , 2 , 3 , 1 , 6 };
int n = arr.length;
if (consecutive.areConsecutive(arr, n) == true )
System.out.println( "Array elements are consecutive" );
else
System.out.println( "Array elements are not consecutive" );
}
}
|
Python3
def areConsecutive(arr, n):
if ( n < 1 ):
return False
Min = min (arr)
Max = max (arr)
if ( Max - Min + 1 = = n):
visited = [ False for i in range (n)]
for i in range (n):
if (visited[arr[i] - Min ] ! = False ):
return False
visited[arr[i] - Min ] = True
return True
return False
arr = [ 5 , 4 , 2 , 3 , 1 , 6 ]
n = len (arr)
if (areConsecutive(arr, n) = = True ):
print ( "Array elements are consecutive " )
else :
print ( "Array elements are not consecutive " )
|
C#
using System;
class GFG {
static bool areConsecutive( int []arr, int n)
{
if (n < 1)
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
bool []visited = new bool [n];
int i;
for (i = 0; i < n; i++)
{
if (visited[arr[i] - min] != false )
return false ;
visited[arr[i] - min] = true ;
}
return true ;
}
return false ;
}
static int getMin( int []arr, int n)
{
int min = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
static int getMax( int []arr, int n)
{
int max = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void Main()
{
int []arr = {5, 4, 2, 3, 1, 6};
int n = arr.Length;
if (areConsecutive(arr, n) == true )
Console.Write( "Array elements are"
+ " consecutive" );
else
Console.Write( "Array elements are"
+ " not consecutive" );
}
}
|
Javascript
<script>
function areConsecutive(arr,n)
{
if (n < 1)
return false ;
let min = getMin(arr, n);
let max = getMax(arr, n);
if (max - min + 1 == n)
{
let visited = new Array(n);
for (let i=0;i<n;i++)
{
visited[i]= false ;
}
let i;
for (i = 0; i < n; i++)
{
if (visited[arr[i] - min] != false )
{
return false ;
}
visited[arr[i] - min] = true ;
}
return true ;
}
return false ;
}
function getMin(arr, n)
{
let min = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
function getMax(arr,n)
{
let max = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
let arr=[5, 4, 2, 3, 1, 6]
let n = arr.length;
if (areConsecutive(arr, n))
{
document.write( "Array elements are consecutive" );
}
else
{
document.write( "Array elements are not consecutive" );
}
</script>
|
PHP
<?php
function areConsecutive( $arr , $n )
{
if ( $n < 1 )
return false;
$min = getMin( $arr , $n );
$max = getMax( $arr , $n );
if ( $max - $min + 1 == $n )
{
$visited = array ();
for ( $i = 0; $i < $n ; $i ++)
{
$visited [ $i ] = false;
}
for ( $i = 0; $i < $n ; $i ++)
{
if ( $visited [ $arr [ $i ] - $min ] != false )
return false;
$visited [ $arr [ $i ] - $min ] = true;
}
return true;
}
return false;
}
function getMin( $arr , $n )
{
$min = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] < $min )
$min = $arr [ $i ];
return $min ;
}
function getMax( $arr , $n )
{
$max = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] > $max )
$max = $arr [ $i ];
return $max ;
}
$arr = array (5, 4, 2, 3, 1, 6);
$n = count ( $arr );
if (areConsecutive( $arr , $n ) == true)
echo "Array elements are consecutive " ;
else
echo "Array elements are not consecutive " ;
?>
|
Output
Array elements are consecutive
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 (Mark visited array elements as negative)
This method is O(n) time complexity and O(1) extra space, but it changes the original array, and it works only if all numbers are positive. We can get the original array by adding an extra step though. It is an extension of method 2, and it has the same two steps.
1) max – min + 1 = n where max is the maximum element in the array, min is the minimum element in the array and n is the number of elements in the array.
2) All elements are distinct.
In this method, the implementation of step 2 differs from method 2. Instead of creating a new array, we modify the input array arr[] to keep track of visited elements. The idea is to traverse the array and for each index i (where 0 ? i < n), make arr[arr[i] – min]] as a negative value. If we see a negative value again then there is repetition.
C++
#include<stdio.h>
#include<stdlib.h>
int getMin( int arr[], int n);
int getMax( int arr[], int n);
bool areConsecutive( int arr[], int n)
{
if ( n < 1 )
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
int i;
for (i = 0; i < n; i++)
{
int j;
if (arr[i] < 0)
j = -arr[i] - min;
else
j = arr[i] - min;
if (arr[j] > 0)
arr[j] = -arr[j];
else
return false ;
}
return true ;
}
return false ;
}
int getMin( int arr[], int n)
{
int min = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
int getMax( int arr[], int n)
{
int max = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr[]= {1, 4, 5, 3, 2, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
if (areConsecutive(arr, n) == true )
printf ( " Array elements are consecutive " );
else
printf ( " Array elements are not consecutive " );
getchar ();
return 0;
}
|
Java
class AreConsecutive
{
boolean areConsecutive( int arr[], int n)
{
if (n < 1 )
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
int i;
for (i = 0 ; i < n; i++)
{
int j;
if (arr[i] < 0 )
j = -arr[i] - min;
else
j = arr[i] - min;
if (arr[j] > 0 )
arr[j] = -arr[j];
else
return false ;
}
return true ;
}
return false ;
}
int getMin( int arr[], int n)
{
int min = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
int getMax( int arr[], int n)
{
int max = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void main(String[] args)
{
AreConsecutive consecutive = new AreConsecutive();
int arr[] = { 5 , 4 , 2 , 3 , 1 , 6 };
int n = arr.length;
if (consecutive.areConsecutive(arr, n) == true )
System.out.println( "Array elements are consecutive" );
else
System.out.println( "Array elements are not consecutive" );
}
}
|
Python 3
def areConsecutive(arr, n):
if ( n < 1 ):
return False
min = getMin(arr, n)
max = getMax(arr, n)
if ( max - min + 1 = = n):
for i in range (n):
if (arr[i] < 0 ):
j = - arr[i] - min
else :
j = arr[i] - min
if (arr[j] > 0 ):
arr[j] = - arr[j]
else :
return False
return True
return False
def getMin(arr, n):
min = arr[ 0 ]
for i in range ( 1 , n):
if (arr[i] < min ):
min = arr[i]
return min
def getMax(arr, n):
max = arr[ 0 ]
for i in range ( 1 , n):
if (arr[i] > max ):
max = arr[i]
return max
if __name__ = = "__main__" :
arr = [ 1 , 4 , 5 , 3 , 2 , 6 ]
n = len (arr)
if (areConsecutive(arr, n) = = True ):
print ( " Array elements are consecutive " )
else :
print ( " Array elements are not consecutive " )
|
C#
using System;
class GFG {
static bool areConsecutive( int []arr, int n)
{
if (n < 1)
return false ;
int min = getMin(arr, n);
int max = getMax(arr, n);
if (max - min + 1 == n)
{
int i;
for (i = 0; i < n; i++)
{
int j;
if (arr[i] < 0)
j = -arr[i] - min;
else
j = arr[i] - min;
if (arr[j] > 0)
arr[j] = -arr[j];
else
return false ;
}
return true ;
}
return false ;
}
static int getMin( int []arr, int n)
{
int min = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
static int getMax( int []arr, int n)
{
int max = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void Main()
{
int []arr = {5, 4, 2, 3, 1, 6};
int n = arr.Length;
if (areConsecutive(arr, n) == true )
Console.Write( "Array elements "
+ "are consecutive" );
else
Console.Write( "Array elements "
+ "are not consecutive" );
}
}
|
Javascript
<script>
function areConsecutive(arr,n)
{
if (n < 1)
return false ;
let min = getMin(arr, n);
let max = getMax(arr, n);
if (max - min + 1 == n)
{
let i;
for (i = 0; i < n; i++)
{
let j;
if (arr[i] < 0)
j = -arr[i] - min;
else
j = arr[i] - min;
if (arr[j] > 0)
arr[j] = -arr[j];
else
return false ;
}
return true ;
}
return false ;
}
function getMin(arr,n)
{
let min = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
function getMax(arr,n)
{
let max = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
let arr=[5, 4, 2, 3, 1, 6];
let n = arr.length;
if (areConsecutive(arr, n) == true )
document.write( "Array elements are consecutive" );
else
document.write( "Array elements are not consecutive" );
</script>
|
PHP
<?php
function areConsecutive( $arr , $n )
{
if ( $n < 1 )
return false;
$min = getMin( $arr , $n );
$max = getMax( $arr , $n );
if ( $max - $min + 1 == $n )
{
$i ;
for ( $i = 0; $i < $n ; $i ++)
{
$j ;
if ( $arr [ $i ] < 0)
$j = - $arr [ $i ] - $min ;
else
$j = $arr [ $i ] - $min ;
if ( $arr [ $j ] > 0)
$arr [ $j ] = - $arr [ $j ];
else
return false;
}
return true;
}
return false;
}
function getMin( $arr , $n )
{
$min = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] < $min )
$min = $arr [ $i ];
return $min ;
}
function getMax( $arr , $n )
{
$max = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] > $max )
$max = $arr [ $i ];
return $max ;
}
$arr = array (1, 4, 5, 3, 2, 6);
$n = count ( $arr );
if (areConsecutive( $arr , $n ) == true)
echo " Array elements are consecutive " ;
else
echo " Array elements are not consecutive " ;
?>
|
Output
Array elements are consecutive
Note that this method might not work for negative numbers. For example, it returns false for {2, 1, 0, -3, -1, -2}.
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
Method 4 (Using XOR property)
This method is O(n) time complexity and O(1) extra space, does not changes the original array, and it works every time.
- As elements should be consecutive, let’s find minimum element or maximum element in array.
- Now if we take xor of two same elements it will result in zero (a^a = 0).
- Suppose array is {-2, 0, 1, -3, 4, 3, 2, -1}, now if we xor all array elements with minimum element and keep increasing minimum element, the resulting xor will become 0 only if elements are consecutive
C++
#include <iostream>
#include <algorithm>
using namespace std;
bool areConsecutive( int arr[], int n)
{
int min_ele = *min_element(arr, arr+n), num = 0;
for ( int i=0; i<n; i++){
num ^= min_ele^arr[i];
min_ele += 1;
}
if (num == 0)
return 1;
return 0;
}
int main()
{
int arr[]= {1, 4, 5, 3, 2, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
if (areConsecutive(arr, n) == true )
printf ( " Array elements are consecutive " );
else
printf ( " Array elements are not consecutive " );
getchar ();
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
int getMin( int arr[], int n)
{
int min = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
int areConsecutive( int arr[], int n)
{
int min_ele = getMin(arr, n), num = 0;
for ( int i=0; i<n; i++){
num ^= min_ele^arr[i];
min_ele += 1;
}
if (num == 0)
return 1;
return 0;
}
int main()
{
int arr[]= {1, 4, 5, 3, 2, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
if (areConsecutive(arr, n) == 1)
printf ( " Array elements are consecutive " );
else
printf ( " Array elements are not consecutive " );
getchar ();
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class AreConsecutive {
boolean areConsecutive( int arr[], int n)
{
int min_ele = Arrays.stream(arr).min().getAsInt();
int num = 0 ;
for ( int i= 0 ; i<n; i++){
num = num ^ min_ele ^ arr[i];
min_ele += 1 ;
}
if (num == 0 )
return true ;
return false ;
}
public static void main(String[] args)
{
AreConsecutive consecutive = new AreConsecutive();
int arr[] = { 5 , 4 , 2 , 3 , 1 , 6 };
int n = arr.length;
if (consecutive.areConsecutive(arr, n) == true )
System.out.println( "Array elements are consecutive" );
else
System.out.println( "Array elements are not consecutive" );
}
}
|
Python3
def areConsecutive(arr, n):
min_ele = arr.index( min (arr))
num = 0
for i in range ( 0 , n):
num ^ = arr[min_ele] ^ arr[i]
arr[min_ele] + = 1
if num = = 0 :
return True
return False
if __name__ = = "__main__" :
arr = [ 1 , 4 , 5 , 3 , 2 , 6 ]
n = len (arr)
if areConsecutive(arr, n) = = True :
print ( " Array elements are consecutive " , end = ' ' )
else :
print ( " Array elements are not consecutive " , end = ' ' )
|
C#
using System;
using System.Linq;
class GFG {
static bool areConsecutive( int []arr, int n)
{
int min_ele = arr.Min();
int num = 0;
for ( int i=0; i<n; i++){
num ^= min_ele^arr[i];
min_ele += 1;
}
if (num == 0)
return true ;
return false ;
}
public static void Main()
{
int []arr = {5, 4, 2, 3, 1, 6};
int n = arr.Length;
if (areConsecutive(arr, n) == true )
Console.Write( "Array elements "
+ "are consecutive" );
else
Console.Write( "Array elements "
+ "are not consecutive" );
}
}
|
Javascript
var areConsecutive = function (arr)
{
var min_ele = Math.min.apply(Math, arr);
var num = 0;
for ( var i = 0; i < arr.length; i++){
num = num ^ min_ele ^ arr[i];
min_ele += 1;
}
if (num == 0)
return 1;
return 0;
}
arr = [1, 2, 3, 4, 5, 6];
if (areConsecutive(arr) == 1){
console.log( " Array elements are consecutive " );}
else
console.log( " Array elements are not consecutive " );
|
Output
Array elements are consecutive
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach:
We can solve this problem using a mathematical formula. If the array has consecutive numbers, then the sum of the elements should be equal to n*(n+1)/2, where n is the size of the array. We can use this formula to check if the array has consecutive numbers.
Steps of approach:
- Initialize a variable sum to 0, minVal to the first element of the array, and maxVal to the first element of the array.
- Traverse the array and Add the current element to sum.
- If the current element is less than minVal, update minVal to the current element.
- If the current element is greater than maxVal, update maxVal to the current element.
- Check if the sum of the elements is equal to n*(n+1)/2 and the difference between the minimum and maximum values is equal to n-1, where n is the size of the array.
- If the above condition is true, return true, else return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool areConsecutive( int arr[], int n)
{
int sum = 0, minVal = arr[0], maxVal = arr[0];
for ( int i = 0; i < n; i++)
{
sum += arr[i];
if (arr[i] < minVal)
{
minVal = arr[i];
}
if (arr[i] > maxVal)
{
maxVal = arr[i];
}
}
if (sum == (n * (n + 1)) / 2 && maxVal - minVal == n - 1)
{
return true ;
}
else
{
return false ;
}
}
int main()
{
int arr[] = {5, 2, 3, 1, 4};
int n = sizeof (arr) / sizeof (arr[0]);
if (areConsecutive(arr, n))
{
cout << "Array elements are consecutive" << endl;
}
else
{
cout << "Array elements are not consecutive" << endl;
}
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static boolean areConsecutive( int [] arr, int n) {
int sum = 0 ;
int minVal = arr[ 0 ];
int maxVal = arr[ 0 ];
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
if (arr[i] < minVal) {
minVal = arr[i];
}
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
if (sum == (n * (n + 1 )) / 2 && maxVal - minVal == n - 1 ) {
return true ;
} else {
return false ;
}
}
public static void main(String[] args) {
int [] arr = { 5 , 2 , 3 , 1 , 4 };
int n = arr.length;
if (areConsecutive(arr, n)) {
System.out.println( "Array elements are consecutive" );
} else {
System.out.println( "Array elements are not consecutive" );
}
}
}
|
Python3
def are_consecutive(arr):
n = len (arr)
_sum = 0
min_val = arr[ 0 ]
max_val = arr[ 0 ]
for i in range (n):
_sum + = arr[i]
if arr[i] < min_val:
min_val = arr[i]
if arr[i] > max_val:
max_val = arr[i]
if _sum = = (n * (n + 1 )) / / 2 and max_val - min_val = = n - 1 :
return True
else :
return False
def main():
arr = [ 5 , 2 , 3 , 1 , 4 ]
if are_consecutive(arr):
print ( "Array elements are consecutive" )
else :
print ( "Array elements are not consecutive" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static bool AreConsecutive( int [] arr, int n)
{
int sum = 0, minVal = arr[0], maxVal = arr[0];
for ( int i = 0; i < n; i++)
{
sum += arr[i];
if (arr[i] < minVal)
{
minVal = arr[i];
}
if (arr[i] > maxVal)
{
maxVal = arr[i];
}
}
if (sum == (n * (n + 1)) / 2 && maxVal - minVal == n - 1)
{
return true ;
}
else
{
return false ;
}
}
static void Main()
{
int [] arr = { 5, 2, 3, 1, 4 };
int n = arr.Length;
if (AreConsecutive(arr, n))
{
Console.WriteLine( "Array elements are consecutive" );
}
else
{
Console.WriteLine( "Array elements are not consecutive" );
}
}
}
|
Javascript
function areConsecutive(arr, n) {
let sum = 0;
let minVal = arr[0];
let maxVal = arr[0];
for (let i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] < minVal) {
minVal = arr[i];
}
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
if (sum === (n * (n + 1)) / 2 && maxVal - minVal === n - 1) {
return true ;
} else {
return false ;
}
}
const arr = [5, 2, 3, 1, 4];
const n = arr.length;
if (areConsecutive(arr, n))
console.log( "Array elements are consecutive" );
else
console.log( "Array elements are not consecutive" );
|
Output:
Array elements are consecutive
Time Complexity: O(n) as it involves only a single traversal of the array.
Space Complexity: O(1), as we are not using any extra space.
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Related Articles: Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
Last Updated :
16 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...