Given an unsorted array of distinct numbers, write a function that returns true if array consists of consecutive numbers.
Examples :
Input : arr[] = {5, 4, 2, 1, 3}
Output : Yes
Input : arr[] = {2, 1, 0, -3, -1, -2}
Output : Yes
We have discussed three different approaches in below post
Check if array elements are consecutive
In above post. three methods for this problem are discussed with the best time complexity of O(n) and in O(1) extra space but that solution doesn’t handle the case of negative numbers. So, in this post, a method with time complexity of O(n) and using O(1) space will be discussed which will handle the case of negative also. An important assumption here is elements are distinct.
- Find the sum of the array.
- If given array elements are consecutive that means they are in AP. So, find min element i.e. first term of AP then calculate ap_sum = n/2 * [2a +(n-1)*d] where d = 1. So, ap_sum = n/2 * [2a +(n-1)]
- Compare both sums. Print Yes if equal, else No.
C++
#include <bits/stdc++.h>
using namespace std;
bool areConsecutives( int arr[], int n)
{
int first_term = *(min_element(arr, arr + n));
int ap_sum = (n * (2 * first_term + (n - 1) * 1)) / 2;
int arr_sum = 0;
for ( int i = 0; i < n; i++)
arr_sum += arr[i];
return ap_sum == arr_sum;
}
int main()
{
int arr[] = { 2, 1, 0, -3, -1, -2 };
int n = sizeof (arr) / sizeof (arr[0]);
areConsecutives(arr, n) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static Boolean areConsecutives( int arr[],
int n)
{
int first_term = Integer.MAX_VALUE;
for ( int j = 0 ; j < n; j++)
{
if (arr[j] < first_term)
first_term = arr[j];
}
int ap_sum = (n * ( 2 * first_term +
(n - 1 ) * 1 )) / 2 ;
int arr_sum = 0 ;
for ( int i = 0 ; i < n; i++)
arr_sum += arr[i];
return ap_sum == arr_sum;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 0 , - 3 , - 1 , - 2 };
int n = arr.length;
Boolean result = areConsecutives(arr, n);
if (result == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
import sys
def areConsecutives(arr, n):
first_term = sys.maxsize
for i in range (n):
if arr[i] < first_term:
first_term = arr[i]
ap_sum = ((n * ( 2 * first_term +
(n - 1 ) * 1 )) / / 2 )
arr_sum = 0
for i in range ( n):
arr_sum + = arr[i]
return ap_sum = = arr_sum
arr = [ 2 , 1 , 0 , - 3 , - 1 , - 2 ]
n = len (arr)
if areConsecutives(arr, n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool areConsecutives( int []arr,
int n)
{
int first_term = int .MaxValue;
for ( int j = 0; j < n; j++)
{
if (arr[j] < first_term)
first_term = arr[j];
}
int ap_sum = (n * (2 * first_term +
(n - 1) * 1)) / 2;
int arr_sum = 0;
for ( int i = 0; i < n; i++)
arr_sum += arr[i];
return ap_sum == arr_sum;
}
public static void Main()
{
int []arr = {2, 1, 0, -3, -1, -2};
int n = arr.Length;
bool result = areConsecutives(arr, n);
if (result == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function areConsecutives( $arr , $n )
{
$first_term = 9999999;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $arr [ $j ] < $first_term )
$first_term = $arr [ $j ];
}
$ap_sum = intval (( $n * (2 * $first_term +
( $n - 1) * 1)) / 2);
$arr_sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$arr_sum += $arr [ $i ];
return $ap_sum == $arr_sum ;
}
$arr = array (2, 1, 0, -3, -1, -2);
$n = count ( $arr );
$result = areConsecutives( $arr , $n );
if ( $result == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function areConsecutives(arr,n)
{
let first_term = Number.MAX_VALUE;
for (let j = 0; j < n; j++)
{
if (arr[j] < first_term)
first_term = arr[j];
}
let ap_sum = (n * (2 * first_term +
(n - 1) * 1)) / 2;
let arr_sum = 0;
for (let i = 0; i < n; i++)
arr_sum += arr[i];
return ap_sum == arr_sum;
}
let arr=[2, 1, 0, -3, -1, -2 ];
let n = arr.length;
let result = areConsecutives(arr, n);
if (result == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
Yes
Time Complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Sahil Chhabra. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!