Check if array elements are consecutive

Here given code implementation process.

/*
    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)
	{
		Consecutive task = new Consecutive();
		// 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;
		task.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 = arr2.length;
		task.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 = arr3.length;
		task.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
// 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()
{
	Consecutive *task = new Consecutive();
	// 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]);
	task->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]);
	task->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]);
	task->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
// 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)
	{
		Consecutive task = new Consecutive();
		// 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;
		task.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 = arr2.Length;
		task.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 = arr3.Length;
		task.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
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()
{
	$task = new Consecutive();
	// 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);
	$task->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 = count($arr2);
	$task->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 = count($arr3);
	$task->checkConsecutive($arr3, $n);
}
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()
{
	var task = new Consecutive();
	// 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;
	task.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 = arr2.length;
	task.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 = arr3.length;
	task.checkConsecutive(arr3, n);
}
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() :
	task = Consecutive()
	#  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)
	task.checkConsecutive(arr1, n)
	#  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)
	task.checkConsecutive(arr2, n)
	#  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)
	task.checkConsecutive(arr3, n)

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() 
	task = Consecutive.new()
	#  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
	task.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 = arr2.length
	task.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 = arr3.length
	task.checkConsecutive(arr3, n)
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;
		task.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 = arr2.length;
		task.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 = arr3.length;
		task.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
/*
    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()
{
	let task: Consecutive = Consecutive();
	// 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;
	task.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 = arr2.count;
	task.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 = arr3.count;
	task.checkConsecutive(arr3, n);
}
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
{
	val task: Consecutive = Consecutive();
	// 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();
	task.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 = arr2.count();
	task.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 = arr3.count();
	task.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


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.

New Comment







© 2021, kalkicode.com, All rights reserved