Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction
Last Updated :
30 May, 2022
Given an integer array arr of size N which contains distinct elements from 1 to N. The task is to check if a position in the array can be found such that all numbers from 1 to N can be counted in a clockwise direction or counterclockwise direction.
Examples:
Input: arr[] = [2, 3, 4, 5, 1]
Output: YES
Explanation:
1 2
5 3
4
If counting is start at index 4
then all numbers can be counted
from 1 to N in a clockwise order.
Input: arr[] = {1, 2, 3, 5, 4]
Output: NO
Explanation:
There is no any index in array
from which given array can count
1 to N in clockwise order or
counterclockwise order.
Approach: The above problem can be solved by observation and analysis.
- An index in the array can be found only if the count of the absolute difference between the consecutive elements greater than 1 is exactly 1, because then only it will be possible to count from 1 to N in clockwise or counterclockwise order.
- If the count of the absolute difference between the adjacent elements is more than 1 Then it will be impossible to count from 1 to N in clockwise or counterclockwise order.
Below is the basic implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_order(vector< int > arr)
{
int cnt = 0;
for ( int i = 0; i < arr.size() - 1; i++) {
if ( abs (arr[i + 1] - arr[i]) > 1)
cnt++;
}
if ( abs (arr[0] - arr[arr.size() - 1]) > 1)
cnt++;
if (cnt > 1)
return false ;
return true ;
}
int main()
{
vector< int > arr = { 2, 3, 4, 5, 1 };
if (check_order(arr))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG{
static boolean check_order( int []arr)
{
int cnt = 0 ;
for ( int i = 0 ; i < arr.length - 1 ; i++)
{
if (Math.abs(arr[i + 1 ] -
arr[i]) > 1 )
cnt++;
}
if (Math.abs(arr[ 0 ] -
arr[arr.length - 1 ]) > 1 )
cnt++;
if (cnt > 1 )
return false ;
return true ;
}
public static void main(String[] args)
{
int []arr = { 2 , 3 , 4 , 5 , 1 };
if (check_order(arr))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def check_order(arr):
cnt = 0
for i in range ( len (arr) - 1 ):
if ( abs (arr[i + 1 ] - arr[i]) > 1 ):
cnt + = 1
if ( abs (arr[ 0 ] - arr[ len (arr) - 1 ]) > 1 ):
cnt + = 1
if (cnt > 1 ):
return False
return True
arr = [ 2 , 3 , 4 , 5 , 1 ]
if (check_order(arr)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool check_order( int []arr)
{
int cnt = 0;
for ( int i = 0; i < arr.Length - 1; i++)
{
if (Math.Abs(arr[i + 1] -
arr[i]) > 1)
cnt++;
}
if (Math.Abs(arr[0] -
arr[arr.Length - 1]) > 1)
cnt++;
if (cnt > 1)
return false ;
return true ;
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 4, 5, 1 };
if (check_order(arr))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
function check_order(arr)
{
var cnt = 0;
for (i = 0; i < arr.length - 1; i++)
{
if (Math.abs(arr[i + 1] - arr[i]) > 1)
cnt++;
}
if (Math.abs(arr[0] - arr[arr.length - 1]) > 1)
cnt++;
if (cnt > 1)
return false ;
return true ;
}
var arr = [ 2, 3, 4, 5, 1 ];
if (check_order(arr))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...