Adjacent element arrangement in Circular Array
Last Updated :
29 Nov, 2023
Given a circular array arr[] of size N where the last element is adjacent to the first element, the task is to arrange an array where the adjacent element’s absolute difference is 1. Return “YES” Otherwise “NO“.
Examples:
Input: N = 6, arr[] = {1, 1, 2, 2, 2, 3}
Output: YES
Explanation: {2, 1, 2, 1, 2, 3} is one of the possible rearrangements.
Approach: This can be solved with the following idea:
Check the parity of each element, and store them in different vectors. Check whether the absolute difference between even and odd elements is 1 or not.
Below are the steps involved:
- Initialize two vectors even and odd.
- Store even elements in even and odd in odd vectors.
- If the size of both vectors is different, Return “NO“.
- Check for each odd [i] – even [i], the difference is one or not, for each i.
- Return “YES“, if 1 for everyone.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
string check( int n, int arr[])
{
vector< int > odd;
vector< int > even;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
even.push_back(arr[i]);
}
else {
odd.push_back(arr[i]);
}
}
if (odd.size() != even.size()) {
return "NO";
}
for ( int i = 0; i < odd.size(); i++) {
if ( abs (odd[i] - even[i]) != 1) {
return "NO";
}
}
return "YES";
}
int main()
{
int N = 6;
int arr[] = { 1, 1, 2, 2, 2, 3 };
cout << check(N, arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
static String check( int n, int [] arr) {
List<Integer> odd = new ArrayList<>();
List<Integer> even = new ArrayList();
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
even.add(arr[i]);
} else {
odd.add(arr[i]);
}
}
if (odd.size() != even.size()) {
return "NO" ;
}
for ( int i = 0 ; i < odd.size(); i++) {
if (Math.abs(odd.get(i) - even.get(i)) != 1 ) {
return "NO" ;
}
}
return "YES" ;
}
public static void main(String[] args) {
int N = 6 ;
int [] arr = { 1 , 1 , 2 , 2 , 2 , 3 };
System.out.println(check(N, arr));
}
}
|
Python3
def check(n, arr):
odd = []
even = []
for i in range (n):
if arr[i] % 2 = = 0 :
even.append(arr[i])
else :
odd.append(arr[i])
if len (odd) ! = len (even):
return "NO"
for i in range ( len (odd)):
if abs (odd[i] - even[i]) ! = 1 :
return "NO"
return "YES"
if __name__ = = "__main__" :
N = 6
arr = [ 1 , 1 , 2 , 2 , 2 , 3 ]
print (check(N, arr))
|
C#
using System;
using System.Collections.Generic;
public class Program
{
static string Check( int n, int [] arr)
{
List< int > odd = new List< int >();
List< int > even = new List< int >();
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
{
even.Add(arr[i]);
}
else
{
odd.Add(arr[i]);
}
}
if (odd.Count != even.Count)
{
return "NO" ;
}
for ( int i = 0; i < odd.Count; i++)
{
if (Math.Abs(odd[i] - even[i]) != 1)
{
return "NO" ;
}
}
return "YES" ;
}
public static void Main( string [] args)
{
int N = 6;
int [] arr = { 1, 1, 2, 2, 2, 3 };
Console.WriteLine(Check(N, arr));
}
}
|
Javascript
function check(n, arr) {
let odd = [];
let even = [];
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 0) {
even.push(arr[i]);
}
else {
odd.push(arr[i]);
}
}
if (odd.length !== even.length) {
return "NO" ;
}
for (let i = 0; i < odd.length; i++) {
if (Math.abs(odd[i] - even[i]) !== 1) {
return "NO" ;
}
}
return "YES" ;
}
const N = 6;
const arr = [1, 1, 2, 2, 2, 3];
console.log(check(N, arr));
|
Time Complexity: O(N)
Auxilairy Space: O(N)
Share your thoughts in the comments
Please Login to comment...