Posted on by Kalkicode
Code Array

# Check if array elements are consecutive

The problem at hand involves checking whether the elements of an array are consecutive or not. An array is said to have consecutive elements if they are in a sequence without any gaps between them. For instance, the array `[1, 2, 3, 4, 5]` has consecutive elements, while the array `[1, 3, 5, 7]` does not.

The task is to write a program that determines whether the elements of a given array are consecutive or not.

## Problem Statement

The problem requires writing a program to check whether the elements of a given array are consecutive or not.

## Explanation with Examples

Let's consider the example provided: array `[4, -2, 3, -1, 5, 1, 0, 2]`.

• Test A
• The array is `[4, -2, 3, -1, 5, 1, 0, 2]`.
• When sorted, it becomes `[-2, -1, 0, 1, 2, 3, 4, 5]`.
• There are no missing elements.
• There are no repeated elements.
• The array elements are consecutive.
• Therefore, the result is "Yes".
• Test B
• The array is `[1, 3, 5, 4]`.
• When sorted, it becomes `[1, 3, 4, 5]`.
• Element `2` is missing.
• Thus, the array elements are not consecutive.
• Therefore, the result is "No".
• Test C
• The array is `[1, 4, 3, 2, 1]`.
• When sorted, it becomes `[1, 1, 2, 3, 4]`.
• Element `1` is repeated.
• Thus, the array elements are not consecutive.
• Therefore, the result is "No".

## Idea to Solve the Problem

To solve this problem, we can follow these steps:

1. Find the minimum and maximum values in the array.
2. Calculate the sum of all elements.
3. If the difference between the maximum and minimum values plus one is equal to the array size, proceed to the next step; otherwise, the array is not consecutive.
4. Create an auxiliary array to track the occurrence of elements.
5. Traverse the given array and mark the presence of each element in the auxiliary array.
6. If any element is repeated or any missing element is detected, the array is not consecutive; otherwise, it is.

## Pseudocode

``````Function printArray(arr, size):
Loop i from 0 to size - 1:
Print arr[i] with spaces

Function checkConsecutive(arr, n):
If n is less than or equal to 0:
Return

status = 1
min = arr[0]
max = arr[0]
sum = 0

Loop i from 1 to n - 1:
If arr[i] is less than min:
Set min to arr[i]
If arr[i] is greater than max:
Set max to arr[i]

If max - min + 1 is equal to n:
Create an auxiliary array auxiliary of size n
Loop i from 0 to n - 1:
Set auxiliary[i] to 0

Loop i from 0 to n - 1:
If auxiliary[arr[i] - min] is 1:
Set status to 0
Else:
Increment auxiliary[arr[i] - min]

Else:
Set status to 0

If status is 1:
Print "Yes"
Else:
Print "No"

Main:
Define array elements
n = size of array
Call checkConsecutive(arr, n)``````

## Algorithm Explanation

1. The `checkConsecutive` function calculates the minimum and maximum values in the array, and checks if the difference between them plus one is equal to the array size.
2. If the above condition is met, an auxiliary array `auxiliary` is created and used to track the occurrence of elements.
3. The function then iterates through the array to check for repeated elements or missing elements.
4. If any repeated or missing elements are found, the status is set to `0`, indicating that the array is not consecutive.
5. If no such elements are found, the status remains `1`, indicating that the array is consecutive.

## Code Solution

``````/*
C program for
Check if array elements are consecutive
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
}
void checkConsecutive(int arr[], int n)
{
if (n <= 0)
{
return;
}
printf("\n Array : ");
printArray(arr, n);
// Auxiliary variables
int status = 1;
int min = arr[0];
int max = arr[0];
int sum = 0;
// Find min and max value in given array
for (int i = 1; i < n; ++i)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
int auxiliary[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
auxiliary[i] = 0;
}
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (int i = 0; i < n && status == 1; ++i)
{
if (auxiliary[arr[i] - min] == 1)
{
status = 0;
}
else
{
auxiliary[arr[i] - min] += 1;
}
}
}
else
{
// When number is missing
status = 0;
}
if (status == 1)
{
printf("\n Yes \n");
}
else
{
printf("\n No \n");
}
}
int main(int argc, char
const *argv[])
{
// Arrays of integers elements
int arr1[] = {
4 , -2 , 3 , -1 , 5 , 1 , 0 , 2
};
int arr2[] = {
1 , 3 , 5 , 4
};
int arr3[] = {
1 , 4 , 3 , 2 , 1
};
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
int n = sizeof(arr1) / sizeof(arr1[0]);
checkConsecutive(arr1, n);
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = sizeof(arr2) / sizeof(arr2[0]);
checkConsecutive(arr2, n);
// Test C
/*

arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = sizeof(arr3) / sizeof(arr3[0]);
checkConsecutive(arr3, n);
return 0;
}``````

#### Output

`````` Array :   4  -2  3  -1  5  1  0  2
Yes

Array :   1  3  5  4
No

Array :   1  4  3  2  1
No``````
``````/*
Java program for
Check if array elements are consecutive
*/
public class Consecutive
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void checkConsecutive(int[] arr, int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n Array : ");
printArray(arr, n);
// Auxiliary variables
boolean status = true;
int min = arr[0];
int max = arr[0];
int sum = 0;
// Find min and max value in given array
for (int i = 1; i < n; ++i)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
boolean[] auxiliary = new boolean[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
auxiliary[i] = false;
}
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (int i = 0; i < n && status == true; ++i)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
System.out.print("\n Yes \n");
}
else
{
System.out.print("\n No \n");
}
}
public static void main(String[] args)
{
// Arrays of integers elements
int[] arr1 = {
4 , -2 , 3 , -1 , 5 , 1 , 0 , 2
};
int[] arr2 = {
1 , 3 , 5 , 4
};
int[] arr3 = {
1 , 4 , 3 , 2 , 1
};
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
int n = arr1.length;
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.length;
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.length;
}
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Check if array elements are consecutive
*/
class Consecutive
{
public:
// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void checkConsecutive(int arr[], int n)
{
if (n <= 0)
{
return;
}
cout << "\n Array : ";
this->printArray(arr, n);
// Auxiliary variables
bool status = true;
int min = arr[0];
int max = arr[0];
int sum = 0;
// Find min and max value in given array
for (int i = 1; i < n; ++i)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
bool auxiliary[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
auxiliary[i] = false;
}
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (int i = 0; i < n && status == true; ++i)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
cout << "\n Yes \n";
}
else
{
cout << "\n No \n";
}
}
};
int main()
{
// Arrays of integers elements
int arr1[] = {
4 , -2 , 3 , -1 , 5 , 1 , 0 , 2
};
int arr2[] = {
1 , 3 , 5 , 4
};
int arr3[] = {
1 , 4 , 3 , 2 , 1
};
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
int n = sizeof(arr1) / sizeof(arr1[0]);
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = sizeof(arr2) / sizeof(arr2[0]);
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = sizeof(arr3) / sizeof(arr3[0]);
return 0;
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````// Include namespace system
using System;
/*
Csharp program for
Check if array elements are consecutive
*/
public class Consecutive
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void checkConsecutive(int[] arr, int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n Array : ");
this.printArray(arr, n);
// Auxiliary variables
Boolean status = true;
int min = arr[0];
int max = arr[0];
int sum = 0;
// Find min and max value in given array
for (int i = 1; i < n; ++i)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
Boolean[] auxiliary = new Boolean[n];
// Set initial value of auxiliary array
for (int i = 0; i < n; ++i)
{
auxiliary[i] = false;
}
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (int i = 0; i < n && status == true; ++i)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
Console.Write("\n Yes \n");
}
else
{
Console.Write("\n No \n");
}
}
public static void Main(String[] args)
{
// Arrays of integers elements
int[] arr1 = {
4 , -2 , 3 , -1 , 5 , 1 , 0 , 2
};
int[] arr2 = {
1 , 3 , 5 , 4
};
int[] arr3 = {
1 , 4 , 3 , 2 , 1
};
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
int n = arr1.Length;
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.Length;
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.Length;
}
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````package main
import "fmt"
/*
Go program for
Check if array elements are consecutive
*/

// Display array elements
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func checkConsecutive(arr[] int, n int) {
if n <= 0 {
return
}
fmt.Print("\n Array : ")
printArray(arr, n)
// Auxiliary variables
var status bool = true
var min int = arr[0]
var max int = arr[0]
var sum int = 0
// Find min and max value in given array
for i := 1 ; i < n ; i++ {
if min > arr[i] {
min = arr[i]
}
if max < arr[i] {
max = arr[i]
}
sum += arr[i]
}
// Check if number of elements between min to max is equal to n or not
if (max - min + 1) == n {
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
var auxiliary = make([] bool, n)
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for i := 0 ; i < n && status == true ; i++ {
if auxiliary[arr[i] - min] == true {
status = false
} else {
auxiliary[arr[i] - min] = true
}
}
} else {
// When number is missing
status = false
}
if status == true {
fmt.Print("\n Yes \n")
} else {
fmt.Print("\n No \n")
}
}
func main() {

// Arrays of integers elements
var arr1 = [] int { 4, -2, 3, -1, 5, 1, 0, 2}
var arr2 = [] int { 1, 3, 5, 4}
var arr3 = [] int { 1 , 4, 3, 2, 1}
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
var n int = len(arr1)
checkConsecutive(arr1, n)
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = len(arr2)
checkConsecutive(arr2, n)
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = len(arr3)
checkConsecutive(arr3, n)
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````<?php
/*
Php program for
Check if array elements are consecutive
*/
class Consecutive
{
// Display array elements
public	function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
public	function checkConsecutive(\$arr, \$n)
{
if (\$n <= 0)
{
return;
}
echo("\n Array : ");
\$this->printArray(\$arr, \$n);
// Auxiliary variables
\$status = true;
\$min = \$arr[0];
\$max = \$arr[0];
\$sum = 0;
// Find min and max value in given array
for (\$i = 1; \$i < \$n; ++\$i)
{
if (\$min > \$arr[\$i])
{
\$min = \$arr[\$i];
}
if (\$max < \$arr[\$i])
{
\$max = \$arr[\$i];
}
\$sum += \$arr[\$i];
}
// Check if number of elements between min to max is equal to n or not
if ((\$max - \$min + 1) == \$n)
{
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
\$auxiliary = array_fill(0, \$n, false);
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (\$i = 0; \$i < \$n && \$status == true; ++\$i)
{
if (\$auxiliary[\$arr[\$i] - \$min] == true)
{
\$status = false;
}
else
{
\$auxiliary[\$arr[\$i] - \$min] = true;
}
}
}
else
{
// When number is missing
\$status = false;
}
if (\$status == true)
{
echo("\n Yes \n");
}
else
{
echo("\n No \n");
}
}
}

function main()
{
// Arrays of integers elements
\$arr1 = array(4, -2, 3, -1, 5, 1, 0, 2);
\$arr2 = array(1, 3, 5, 4);
\$arr3 = array(1, 4, 3, 2, 1);
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
\$n = count(\$arr1);
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
\$n = count(\$arr2);
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
\$n = count(\$arr3);
}
main();``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````/*
Node JS program for
Check if array elements are consecutive
*/
class Consecutive
{
// Display array elements
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
checkConsecutive(arr, n)
{
if (n <= 0)
{
return;
}
process.stdout.write("\n Array : ");
this.printArray(arr, n);
// Auxiliary variables
var status = true;
var min = arr[0];
var max = arr[0];
var sum = 0;
// Find min and max value in given array
for (var i = 1; i < n; ++i)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
var auxiliary = Array(n).fill(false);
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
for (var i = 0; i < n && status == true; ++i)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
process.stdout.write("\n Yes \n");
}
else
{
process.stdout.write("\n No \n");
}
}
}

function main()
{
// Arrays of integers elements
var arr1 = [4, -2, 3, -1, 5, 1, 0, 2];
var arr2 = [1, 3, 5, 4];
var arr3 = [1, 4, 3, 2, 1];
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
var n = arr1.length;
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.length;
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.length;
}
main();``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````#    Python 3 program for
#    Check if array elements are consecutive
class Consecutive :
#  Display list elements
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

def checkConsecutive(self, arr, n) :
if (n <= 0) :
return

print("\n Array : ", end = "")
self.printArray(arr, n)
#  Auxiliary variables
status = True
min = arr[0]
max = arr[0]
sum = 0
i = 1
#  Find min and max value in given list
while (i < n) :
if (min > arr[i]) :
min = arr[i]

if (max < arr[i]) :
max = arr[i]

sum += arr[i]
i += 1

#  Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n) :
#  Auxiliary list which is use to check consecutive elements
#  Set initial value of auxiliary list
auxiliary = [False] * (n)
i = 0
#  This loop are check two different conditions
#  ➀  Element is repeated or not.
#  ➁  Otherwise sets the active consecutive element status.
while (i < n and status == True) :
if (auxiliary[arr[i] - min] == True) :
status = False
else :
auxiliary[arr[i] - min] = True

i += 1

else :
#  When number is missing
status = False

if (status == True) :
print("\n Yes ")
else :
print("\n No ")

def main() :
#  Arrays of integers elements
arr1 = [4, -2, 3, -1, 5, 1, 0, 2]
arr2 = [1, 3, 5, 4]
arr3 = [1, 4, 3, 2, 1]
#  Test A
#    arr = [4, -2, 3, -1, 5, 1, 0, 2]
#    // Sorted list
#          [-2 -1 0 1 2 3 4 5]
#    // No missing elements.
#    // No repeated element exists.
#    // Array elements are consecutive
#    -----------------------------------
#    Result : Yes
n = len(arr1)
#  Test B
#    arr = [1, 3, 5, 4]
#    // Sorted list
#        [1 3 4 5]
#    // element 2 are missing
#    // So list elements are not consecutive
#    -----------------------------------
#    Result : Yes
n = len(arr2)
#  Test C
#    arr = [1,4, 3, 2, 1]
#    // Sorted list
#          [1 1 2 3 4
#    // Element 1 are repeats
#    // So list elements are not consecutive
#    -----------------------------------
#    Result : Yes
n = len(arr3)

if __name__ == "__main__": main()``````

#### Output

`````` Array :   4  -2  3  -1  5  1  0  2
Yes

Array :   1  3  5  4
No

Array :   1  4  3  2  1
No``````
``````#    Ruby program for
#    Check if array elements are consecutive
class Consecutive
#  Display array elements
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

def checkConsecutive(arr, n)
if (n <= 0)
return
end

print("\n Array : ")
self.printArray(arr, n)
#  Auxiliary variables
status = true
min = arr[0]
max = arr[0]
sum = 0
i = 1
#  Find min and max value in given array
while (i < n)
if (min > arr[i])
min = arr[i]
end

if (max < arr[i])
max = arr[i]
end

sum += arr[i]
i += 1
end

#  Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
#  Auxiliary array which is use to check consecutive elements
#  Set initial value of auxiliary array
auxiliary = Array.new(n) {false}
i = 0
#  This loop are check two different conditions
#  ➀  Element is repeated or not.
#  ➁  Otherwise sets the active consecutive element status.
while (i < n && status == true)
if (auxiliary[arr[i] - min] == true)
status = false
else

auxiliary[arr[i] - min] = true
end

i += 1
end

else

#  When number is missing
status = false
end

if (status == true)
print("\n Yes \n")
else

print("\n No \n")
end

end

end

def main()
#  Arrays of integers elements
arr1 = [4, -2, 3, -1, 5, 1, 0, 2]
arr2 = [1, 3, 5, 4]
arr3 = [1, 4, 3, 2, 1]
#  Test A
#    arr = [4, -2, 3, -1, 5, 1, 0, 2]
#    // Sorted array
#          [-2 -1 0 1 2 3 4 5]
#    // No missing elements.
#    // No repeated element exists.
#    // Array elements are consecutive
#    -----------------------------------
#    Result : Yes
n = arr1.length
#  Test B
#    arr = [1, 3, 5, 4]
#    // Sorted array
#        [1 3 4 5]
#    // element 2 are missing
#    // So array elements are not consecutive
#    -----------------------------------
#    Result : Yes
n = arr2.length
#  Test C
#    arr = [1,4, 3, 2, 1]
#    // Sorted array
#          [1 1 2 3 4
#    // Element 1 are repeats
#    // So array elements are not consecutive
#    -----------------------------------
#    Result : Yes
n = arr3.length
end

main()``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No
``````
``````/*
Scala program for
Check if array elements are consecutive
*/
class Consecutive()
{
// Display array elements
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def checkConsecutive(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n Array : ");
printArray(arr, n);
// Auxiliary variables
var status: Boolean = true;
var min: Int = arr(0);
var max: Int = arr(0);
var sum: Int = 0;
var i: Int = 1;
// Find min and max value in given array
while (i < n)
{
if (min > arr(i))
{
min = arr(i);
}
if (max < arr(i))
{
max = arr(i);
}
sum += arr(i);
i += 1;
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
var auxiliary: Array[Boolean] = Array.fill[Boolean](n)(false);
var i: Int = 0;
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
while (i < n && status == true)
{
if (auxiliary(arr(i) - min) == true)
{
status = false;
}
else
{
auxiliary(arr(i) - min) = true;
}
i += 1;
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
print("\n Yes \n");
}
else
{
print("\n No \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Consecutive = new Consecutive();
// Arrays of integers elements
var arr1: Array[Int] = Array(4, -2, 3, -1, 5, 1, 0, 2);
var arr2: Array[Int] = Array(1, 3, 5, 4);
var arr3: Array[Int] = Array(1, 4, 3, 2, 1);
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
var n: Int = arr1.length;
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.length;
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.length;
}
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````
``````/*
Swift 4 program for
Check if array elements are consecutive
*/
class Consecutive
{
// Display array elements
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func checkConsecutive(_ arr: [Int], _ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Array : ", terminator: "");
self.printArray(arr, n);
// Auxiliary variables
var status: Bool = true;
var min: Int = arr[0];
var max: Int = arr[0];
var sum: Int = 0;
var i: Int = 1;
// Find min and max value in given array
while (i < n)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
i += 1;
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
var auxiliary: [Bool] = Array(repeating: false, count: n);
var i: Int = 0;
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
while (i < n && status == true)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
i += 1;
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
print("\n Yes ");
}
else
{
print("\n No ");
}
}
}
func main()
{
// Arrays of integers elements
let arr1: [Int] = [4, -2, 3, -1, 5, 1, 0, 2];
let arr2: [Int] = [1, 3, 5, 4];
let arr3: [Int] = [1, 4, 3, 2, 1];
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
var n: Int = arr1.count;
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.count;
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.count;
}
main();``````

#### Output

`````` Array :   4  -2  3  -1  5  1  0  2
Yes

Array :   1  3  5  4
No

Array :   1  4  3  2  1
No``````
``````/*
Kotlin program for
Check if array elements are consecutive
*/
class Consecutive
{
// Display array elements
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun checkConsecutive(arr: Array < Int > , n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n Array : ");
this.printArray(arr, n);
// Auxiliary variables
var status: Boolean = true;
var min: Int = arr[0];
var max: Int = arr[0];
var sum: Int = 0;
var i: Int = 1;
// Find min and max value in given array
while (i < n)
{
if (min > arr[i])
{
min = arr[i];
}
if (max < arr[i])
{
max = arr[i];
}
sum += arr[i];
i += 1;
}
// Check if number of elements between min to max is equal to n or not
if ((max - min + 1) == n)
{
// Auxiliary array which is use to check consecutive elements
// Set initial value of auxiliary array
var auxiliary: Array < Boolean > = Array(n)
{
false
};
i = 0;
// This loop are check two different conditions
// ➀  Element is repeated or not.
// ➁  Otherwise sets the active consecutive element status.
while (i < n && status == true)
{
if (auxiliary[arr[i] - min] == true)
{
status = false;
}
else
{
auxiliary[arr[i] - min] = true;
}
i += 1;
}
}
else
{
// When number is missing
status = false;
}
if (status == true)
{
print("\n Yes \n");
}
else
{
print("\n No \n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Arrays of integers elements
val arr1: Array < Int > = arrayOf(4, -2, 3, -1, 5, 1, 0, 2);
val arr2: Array < Int > = arrayOf(1, 3, 5, 4);
val arr3: Array < Int > = arrayOf(1, 4, 3, 2, 1);
// Test A
/*
arr = [4, -2, 3, -1, 5, 1, 0, 2]
// Sorted array
[-2 -1 0 1 2 3 4 5]
// No missing elements.
// No repeated element exists.
// Array elements are consecutive
-----------------------------------
Result : Yes
*/
var n: Int = arr1.count();
// Test B
/*
arr = [1, 3, 5, 4]
// Sorted array
[1 3 4 5]
// element 2 are missing
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr2.count();
// Test C
/*
arr = [1,4, 3, 2, 1]
// Sorted array
[1 1 2 3 4
// Element 1 are repeats
// So array elements are not consecutive
-----------------------------------
Result : Yes
*/
n = arr3.count();
}``````

#### Output

`````` Array :  4 -2 3 -1 5 1 0 2
Yes

Array :  1 3 5 4
No

Array :  1 4 3 2 1
No``````

## Time Complexity Analysis

The time complexity of this algorithm is O(n), where n is the number of elements in the array. This is because we iterate through the array once to find the minimum and maximum values, calculate the sum, and check for consecutive elements.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post