 GeeksforGeeks App
Open App Browser
Continue

# Check if Array elements can be made consecutive by performing given operations

Given an array arr[] of size N, the task is to check if it is possible to make all array elements consecutive by performing any of the following operations:

• Leave the element unchanged (arr[i] = arr[i])
• Increment the element by 1
• Decrement the element by 1

Note: For every array element, one of the above operations can be applied only once.

Examples:

Input: N = 2, arr[] = {2, 4}
Output: YES
Explanation: This array can be converted into {3, 4} or {2, 3}
It is possible to get the consecutive sequence. So the output will be “YES”.

Input: N = 4, arr[] = {1, 2, 3, 7}
Output: NO
Explanation: There is no way to make the elements consecutive using above operations.
So the output is “NO”.

Approach: The problem can be solved based on the below observation.

Observations:

There are only three possibilities:

• If first element is incremented then the whole sequence should look like arr + 1, arr + 2, arr + 3, and so on
• If first element is left unchanged then the whole sequence should look like arr, arr + 1, arr + 2, arr + 3, and so on.
• If first element is decremented then the whole sequence should look like arr-1, arr, arr + 1, arr + 2, arr + 3, and so on.

If it is possible to convert the given array in any of the above three possibilities then the output will be “YES” else “NO“.

Follow the below steps to solve the problem:

• Make three possible arrays based on whether the first element gets decremented, incremented, or left unchanged.
• One by one check for each possibility if it is possible to convert the given array into a possible array by changing the value of the array element by +1, 0, or -1.
• If it is possible to convert an array in any of the three forms then the answer is “YES” else “NO”.

Below is the implementation for the above approach:

## C++

 `// C++ code to implement the above approach.` `#include ``using` `namespace` `std;` `// Function to check if it is possible``// to make the array elements consecutive``bool` `ispossible(``int` `arr[], ``int` `n)``{` `    ``// If the first element left unchanged``    ``// The valid sequence should look like``    ``// arr, arr+1, arr+2 ...``    ``int` `possibility1[n];``    ``for` `(``int` `i = 0; i < n; i++)``        ``possibility1[i] = i + arr;` `    ``// If  incremented the first element``    ``// The valid sequence should look like``    ``// arr+1, arr+2, arr+3 ...``    ``int` `possibility2[n];``    ``for` `(``int` `i = 0; i < n; i++)``        ``possibility2[i] = i + arr + 1;` `    ``// If  decremented the first element``    ``// The valid sequence should look like``    ``// arr-1, arr-2, arr-3 ...``    ``int` `possibility3[n];``    ``for` `(``int` `i = 0; i < n; i++)``        ``possibility3[i] = i + arr - 1;` `    ``// Now check if it is possible to convert``    ``// array in any of the above mentioned``    ``// three possibilities, which were,``    ``// either increment, decrement or``    ``// left unchanged. So the gap between``    ``// current and desired should not be``    ``// greater than 2``    ``bool` `canposs1 = ``true``, canposs2 = ``true``;``    ``bool` `canposs3 = ``true``;` `    ``// Is it possible to convert array``    ``// in possibility 1``    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(``abs``(possibility1[i] - arr[i]) > 1) {``            ``canposs1 = ``false``;``            ``break``;``        ``}``    ``}` `    ``// Is it possible to convert array``    ``// in possibility 2``    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(``abs``(possibility2[i] - arr[i]) > 1) {``            ``canposs2 = ``false``;``            ``break``;``        ``}``    ``}` `    ``// Is it possible to convert array``    ``// in possibility 3``    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(``abs``(possibility3[i] - arr[i]) > 1) {``            ``canposs3 = ``false``;``            ``break``;``        ``}``    ``}` `    ``// If any one is possible``    ``// then "YES" else "NO"``    ``if` `(canposs1 || canposs2 || canposs3) {``        ``return` `true``;``    ``}``    ``return` `false``;``}` `// Drivers code``int` `main()``{``    ``int` `N = 4;``    ``int` `arr[N] = { 1, 2, 3, 7 };` `    ``// Function call``    ``if` `(ispossible(arr, N)) {``        ``cout << ``"YES"``;``    ``}``    ``else` `{``        ``cout << ``"NO"``;``    ``}``    ``return` `0;``}`

## Java

 `/*package whatever //do not write package name here */` `import` `java.io.*;``// Java program for above approach``class` `GFG``{` `  ``// Function to check if it is possible``  ``// to make the array elements consecutive``  ``public` `static` `boolean` `ispossible(``int` `arr[], ``int` `n)``  ``{` `    ``// If the first element left unchanged``    ``// The valid sequence should look like``    ``// arr, arr+1, arr+2 ...``    ``int``[] possibility1 = ``new` `int``[n];``    ``for` `(``int` `i = ``0``; i < n; i++)``      ``possibility1[i] = i + arr[``0``];` `    ``// If  incremented the first element``    ``// The valid sequence should look like``    ``// arr+1, arr+2, arr+3 ...``    ``int``[] possibility2 = ``new` `int``[n];``    ``for` `(``int` `i = ``0``; i < n; i++)``      ``possibility2[i] = i + arr[``0``] + ``1``;` `    ``// If  decremented the first element``    ``// The valid sequence should look like``    ``// arr-1, arr-2, arr-3 ...``    ``int``[] possibility3 = ``new` `int``[n];``    ``for` `(``int` `i = ``0``; i < n; i++)``      ``possibility3[i] = i + arr[``0``] - ``1``;` `    ``// Now check if it is possible to convert``    ``// array in any of the above mentioned``    ``// three possibilities, which were,``    ``// either increment, decrement or``    ``// left unchanged. So the gap between``    ``// current and desired should not be``    ``// greater than 2``    ``Boolean canposs1 = ``true``, canposs2 = ``true``;``    ``Boolean canposs3 = ``true``;` `    ``// Is it possible to convert array``    ``// in possibility 1``    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``if` `(Math.abs(possibility1[i] - arr[i]) > ``1``) {``        ``canposs1 = ``false``;``        ``break``;``      ``}``    ``}` `    ``// Is it possible to convert array``    ``// in possibility 2``    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``if` `(Math.abs(possibility2[i] - arr[i]) > ``1``) {``        ``canposs2 = ``false``;``        ``break``;``      ``}``    ``}` `    ``// Is it possible to convert array``    ``// in possibility 3``    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``if` `(Math.abs(possibility3[i] - arr[i]) > ``1``) {``        ``canposs3 = ``false``;``        ``break``;``      ``}``    ``}` `    ``// If any one is possible``    ``// then "YES" else "NO"``    ``if` `(canposs1 || canposs2 || canposs3) {``      ``return` `true``;``    ``}``    ``return` `false``;``  ``}` `  ``public` `static` `void` `main (String[] args)``  ``{``    ``int` `N = ``4``;``    ``int` `arr[] = { ``1``, ``2``, ``3``, ``7` `};` `    ``// Function call``    ``if` `(ispossible(arr, N)) {``      ``System.out.print(``"YES"``);``    ``}``    ``else` `{``      ``System.out.print(``"NO"``);``    ``}``  ``}``}` `// This code is contributed by shinjanpatra`

## Python3

 `# Python code to implement the above approach.``# Function to check if it is possible``# to make the array elements consecutive``def` `ispossible(arr, n):``  ` `    ``# If the first element left unchanged``    ``# The valid sequence should look like``    ``# arr, arr+1, arr+2 ...``    ``possibility1 ``=` `[]``    ``for` `i ``in` `range``(``0``, n):``        ``possibility1.append(i``+``arr[``0``])` `    ``# If  incremented the first element``    ``# The valid sequence should look like``    ``# arr+1, arr+2, arr+3 ...``    ``possibility2 ``=` `[];``    ``for` `i ``in` `range``(``0``,n):``        ``possibility2.append(i``+``arr[``0``]``+``1``)` `    ``# If  decremented the first element``    ``# The valid sequence should look like``    ``# arr-1, arr-2, arr-3 ...``    ``possibility3 ``=` `[];``    ``for` `i ``in` `range``(``0``,n):``        ``possibility3.append(i``+``arr[``0``]``-``1``)` `    ``# Now check if it is possible to convert``    ``# array in any of the above mentioned``    ``# three possibilities, which were,``    ``# either increment, decrement or``    ``# left unchanged. So the gap between``    ``# current and desired should not be``    ``# greater than 2``    ``canposs1 ``=` `1``    ``canposs2 ``=` `1``    ``canposs3 ``=` `1` `    ``# Is it possible to convert array``    ``# in possibility 1``    ``for` `i ``in` `range``(``0``,n):``        ``if` `(((possibility1[i] ``-` `arr[i]) > ``1``) ``or` `((possibility1[i] ``-` `arr[i]) < ``-``1``)):``            ``canposs1 ``=` `0``            ``break` `    ``# Is it possible to convert array``    ``# in possibility 2``    ``for` `i ``in` `range``(``0``,n):``        ``if` `(((possibility2[i] ``-` `arr[i]) > ``1``) ``or` `((possibility2[i] ``-` `arr[i]) < ``-``1``)):``            ``canposs2 ``=` `0``            ``break` `    ``# Is it possible to convert array``    ``# in possibility 3``    ``for` `i ``in` `range``(``0``,n):``        ``if` `(((possibility3[i] ``-` `arr[i]) > ``1``) ``or` `((possibility3[i] ``-` `arr[i]) < ``-``1``)):``            ``canposs3 ``=` `0``            ``break` `    ``# If any one is possible``    ``# then "YES" else "NO"``    ``if` `(canposs1 ``or` `canposs2 ``or` `canposs3):``        ``return` `1``    ``else``:``        ``return` `0` `# Driver code``N ``=` `4``;``arr ``=` `[ ``1``, ``2``, ``3``, ``7` `]` `# Function call``z ``=` `ispossible(arr, ``4``)``if` `(z ``=``=` `1``):``  ``print``(``"YES"``)``else``:``  ``print``(``"NO"``)``  ` `  ``# This code is contributed by ashishsingh13122000`

## C#

 `// C# program for above approach``using` `System;` `class` `GFG {``    ``// Function to check if it is possible``    ``// to make the array elements consecutive``    ``static` `bool` `ispossible(``int``[] arr, ``int` `n)``    ``{` `        ``// If the first element left unchanged``        ``// The valid sequence should look like``        ``// arr, arr+1, arr+2 ...``        ``int``[] possibility1 = ``new` `int``[n];``        ``for` `(``int` `i = 0; i < n; i++)``            ``possibility1[i] = i + arr;` `        ``// If  incremented the first element``        ``// The valid sequence should look like``        ``// arr+1, arr+2, arr+3 ...``        ``int``[] possibility2 = ``new` `int``[n];``        ``for` `(``int` `i = 0; i < n; i++)``            ``possibility2[i] = i + arr + 1;` `        ``// If  decremented the first element``        ``// The valid sequence should look like``        ``// arr-1, arr-2, arr-3 ...``        ``int``[] possibility3 = ``new` `int``[n];``        ``for` `(``int` `i = 0; i < n; i++)``            ``possibility3[i] = i + arr - 1;` `        ``// Now check if it is possible to convert``        ``// array in any of the above mentioned``        ``// three possibilities, which were,``        ``// either increment, decrement or``        ``// left unchanged. So the gap between``        ``// current and desired should not be``        ``// greater than 2``        ``bool` `canposs1 = ``true``, canposs2 = ``true``;``        ``bool` `canposs3 = ``true``;` `        ``// Is it possible to convert array``        ``// in possibility 1``        ``for` `(``int` `i = 0; i < n; i++) {``            ``if` `(Math.Abs(possibility1[i] - arr[i]) > 1) {``                ``canposs1 = ``false``;``                ``break``;``            ``}``        ``}` `        ``// Is it possible to convert array``        ``// in possibility 2``        ``for` `(``int` `i = 0; i < n; i++) {``            ``if` `(Math.Abs(possibility2[i] - arr[i]) > 1) {``                ``canposs2 = ``false``;``                ``break``;``            ``}``        ``}` `        ``// Is it possible to convert array``        ``// in possibility 3``        ``for` `(``int` `i = 0; i < n; i++) {``            ``if` `(Math.Abs(possibility3[i] - arr[i]) > 1) {``                ``canposs3 = ``false``;``                ``break``;``            ``}``        ``}` `        ``// If any one is possible``        ``// then "YES" else "NO"``        ``if` `(canposs1 || canposs2 || canposs3) {``            ``return` `true``;``        ``}``        ``return` `false``;``    ``}` `    ``public` `static` `int` `Main()``    ``{``        ``int` `N = 4;``        ``int``[] arr = ``new` `int``[] { 1, 2, 3, 7 };` `        ``// Function call``        ``if` `(ispossible(arr, N)) {``            ``Console.Write(``"YES"``);``        ``}``        ``else` `{``            ``Console.Write(``"NO"``);``        ``}``        ``return` `0;``    ``}``}` `// This code is contributed by Taranpreet`

## Javascript

 ``

Output

`NO`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up