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[0] + 1, arr[0] + 2, arr[0] + 3, and so on
- If first element is left unchanged then the whole sequence should look like arr[0], arr[0] + 1, arr[0] + 2, arr[0] + 3, and so on.
- If first element is decremented then the whole sequence should look like arr[0]-1, arr[0], arr[0] + 1, arr[0] + 2, arr[0] + 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++
#include <bits/stdc++.h>
using namespace std;
bool ispossible( int arr[], int n)
{
int possibility1[n];
for ( int i = 0; i < n; i++)
possibility1[i] = i + arr[0];
int possibility2[n];
for ( int i = 0; i < n; i++)
possibility2[i] = i + arr[0] + 1;
int possibility3[n];
for ( int i = 0; i < n; i++)
possibility3[i] = i + arr[0] - 1;
bool canposs1 = true , canposs2 = true ;
bool canposs3 = true ;
for ( int i = 0; i < n; i++) {
if ( abs (possibility1[i] - arr[i]) > 1) {
canposs1 = false ;
break ;
}
}
for ( int i = 0; i < n; i++) {
if ( abs (possibility2[i] - arr[i]) > 1) {
canposs2 = false ;
break ;
}
}
for ( int i = 0; i < n; i++) {
if ( abs (possibility3[i] - arr[i]) > 1) {
canposs3 = false ;
break ;
}
}
if (canposs1 || canposs2 || canposs3) {
return true ;
}
return false ;
}
int main()
{
int N = 4;
int arr[N] = { 1, 2, 3, 7 };
if (ispossible(arr, N)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static boolean ispossible( int arr[], int n)
{
int [] possibility1 = new int [n];
for ( int i = 0 ; i < n; i++)
possibility1[i] = i + arr[ 0 ];
int [] possibility2 = new int [n];
for ( int i = 0 ; i < n; i++)
possibility2[i] = i + arr[ 0 ] + 1 ;
int [] possibility3 = new int [n];
for ( int i = 0 ; i < n; i++)
possibility3[i] = i + arr[ 0 ] - 1 ;
Boolean canposs1 = true , canposs2 = true ;
Boolean canposs3 = true ;
for ( int i = 0 ; i < n; i++) {
if (Math.abs(possibility1[i] - arr[i]) > 1 ) {
canposs1 = false ;
break ;
}
}
for ( int i = 0 ; i < n; i++) {
if (Math.abs(possibility2[i] - arr[i]) > 1 ) {
canposs2 = false ;
break ;
}
}
for ( int i = 0 ; i < n; i++) {
if (Math.abs(possibility3[i] - arr[i]) > 1 ) {
canposs3 = false ;
break ;
}
}
if (canposs1 || canposs2 || canposs3) {
return true ;
}
return false ;
}
public static void main (String[] args)
{
int N = 4 ;
int arr[] = { 1 , 2 , 3 , 7 };
if (ispossible(arr, N)) {
System.out.print( "YES" );
}
else {
System.out.print( "NO" );
}
}
}
|
Python3
def ispossible(arr, n):
possibility1 = []
for i in range ( 0 , n):
possibility1.append(i + arr[ 0 ])
possibility2 = [];
for i in range ( 0 ,n):
possibility2.append(i + arr[ 0 ] + 1 )
possibility3 = [];
for i in range ( 0 ,n):
possibility3.append(i + arr[ 0 ] - 1 )
canposs1 = 1
canposs2 = 1
canposs3 = 1
for i in range ( 0 ,n):
if (((possibility1[i] - arr[i]) > 1 ) or ((possibility1[i] - arr[i]) < - 1 )):
canposs1 = 0
break
for i in range ( 0 ,n):
if (((possibility2[i] - arr[i]) > 1 ) or ((possibility2[i] - arr[i]) < - 1 )):
canposs2 = 0
break
for i in range ( 0 ,n):
if (((possibility3[i] - arr[i]) > 1 ) or ((possibility3[i] - arr[i]) < - 1 )):
canposs3 = 0
break
if (canposs1 or canposs2 or canposs3):
return 1
else :
return 0
N = 4 ;
arr = [ 1 , 2 , 3 , 7 ]
z = ispossible(arr, 4 )
if (z = = 1 ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static bool ispossible( int [] arr, int n)
{
int [] possibility1 = new int [n];
for ( int i = 0; i < n; i++)
possibility1[i] = i + arr[0];
int [] possibility2 = new int [n];
for ( int i = 0; i < n; i++)
possibility2[i] = i + arr[0] + 1;
int [] possibility3 = new int [n];
for ( int i = 0; i < n; i++)
possibility3[i] = i + arr[0] - 1;
bool canposs1 = true , canposs2 = true ;
bool canposs3 = true ;
for ( int i = 0; i < n; i++) {
if (Math.Abs(possibility1[i] - arr[i]) > 1) {
canposs1 = false ;
break ;
}
}
for ( int i = 0; i < n; i++) {
if (Math.Abs(possibility2[i] - arr[i]) > 1) {
canposs2 = false ;
break ;
}
}
for ( int i = 0; i < n; i++) {
if (Math.Abs(possibility3[i] - arr[i]) > 1) {
canposs3 = false ;
break ;
}
}
if (canposs1 || canposs2 || canposs3) {
return true ;
}
return false ;
}
public static int Main()
{
int N = 4;
int [] arr = new int [] { 1, 2, 3, 7 };
if (ispossible(arr, N)) {
Console.Write( "YES" );
}
else {
Console.Write( "NO" );
}
return 0;
}
}
|
Javascript
<script>
const ispossible = (arr, n) => {
let possibility1 = new Array(n).fill(0);
for (let i = 0; i < n; i++)
possibility1[i] = i + arr[0];
let possibility2 = new Array(n).fill(0);
for (let i = 0; i < n; i++)
possibility2[i] = i + arr[0] + 1;
let possibility3 = new Array(n);
for (let i = 0; i < n; i++)
possibility3[i] = i + arr[0] - 1;
let canposs1 = true , canposs2 = true ;
let canposs3 = true ;
for (let i = 0; i < n; i++) {
if (Math.abs(possibility1[i] - arr[i]) > 1) {
canposs1 = false ;
break ;
}
}
for (let i = 0; i < n; i++) {
if (Math.abs(possibility2[i] - arr[i]) > 1) {
canposs2 = false ;
break ;
}
}
for (let i = 0; i < n; i++) {
if (Math.abs(possibility3[i] - arr[i]) > 1) {
canposs3 = false ;
break ;
}
}
if (canposs1 || canposs2 || canposs3) {
return true ;
}
return false ;
}
let N = 4;
let arr = [1, 2, 3, 7];
if (ispossible(arr, N)) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
11 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...