Given an array, find whether it is possible to obtain an array having distinct neighbouring elements by swapping two neighbouring array elements.
Examples:
Input : 1 1 2
Output : YES
swap 1 (second last element) and 2 (last element),
to obtain 1 2 1, which has distinct neighbouring
elements .
Input : 7 7 7 7
Output : NO
We can't swap to obtain distinct elements in
neighbor .
To obtain an array having distinct neighbouring elements is possible only, when the frequency of most occurring element is less than or equal to half of size of array i.e ( <= (n+1)/2 ). To make it more clear consider different examples
1st Example :
a[] = {1, 1, 2, 3, 1}
We can obtain array {1, 2, 1, 3, 1} by
swapping (2nd and 3rd) element from array a.
Here 1 occurs most and its frequency is
3 . So that 3 <= ((5+1)/2) .
Hence, it is possible.
Below is the implementation of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
void distinctAdjacentElement( int a[], int n)
{
map< int , int > m;
for ( int i = 0; i < n; ++i)
m[a[i]]++;
int mx = 0;
for ( int i = 0; i < n; ++i)
if (mx < m[a[i]])
mx = m[a[i]];
if (mx > (n + 1) / 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
int main()
{
int a[] = { 7, 7, 7, 7 };
int n = sizeof (a) / sizeof (a[0]);
distinctAdjacentElement(a, n);
return 0;
}
|
Python3
def distantAdjacentElement(a, n):
m = dict ()
for i in range (n):
if a[i] in m:
m[a[i]] + = 1
else :
m[a[i]] = 1
mx = 0
for i in range (n):
if mx < m[a[i]]:
mx = m[a[i]]
if mx > (n + 1 ) / / 2 :
print ( "NO" )
else :
print ( "YES" )
if __name__ = = "__main__" :
a = [ 7 , 7 , 7 , 7 ]
n = len (a)
distantAdjacentElement(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void distinctAdjacentElement( int [] a, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; ++i)
{
if (m.ContainsKey(a[i]))
{
int x = m[a[i]] + 1;
m[a[i]] = x;
}
else
{
m[a[i]] = 1;
}
}
int mx = 0;
for ( int i = 0; i < n; ++i)
{
if (mx < m[a[i]])
{
mx = m[a[i]];
}
}
if (mx > (n + 1) / 2)
{
Console.WriteLine( "NO" );
}
else
{
Console.WriteLine( "YES" );
}
}
public static void Main( string [] args)
{
int [] a = new int [] {7, 7, 7, 7};
int n = 4;
distinctAdjacentElement(a, n);
}
}
|
Java
import java.io.*;
import java.util.HashMap;
import java.util.Map;
class GFG {
static void distinctAdjacentElement( int a[], int n)
{
HashMap<Integer,Integer> m = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; ++i){
if (m.containsKey(a[i])){
int x = m.get(a[i]) + 1 ;
m.put(a[i],x);
}
else {
m.put(a[i], 1 );
}
}
int mx = 0 ;
for ( int i = 0 ; i < n; ++i)
if (mx < m.get(a[i]))
mx = m.get(a[i]);
if (mx > (n + 1 ) / 2 )
System.out.println( "NO" );
else
System.out.println( "YES" );
}
public static void main (String[] args) {
int a[] = { 7 , 7 , 7 , 7 };
int n = 4 ;
distinctAdjacentElement(a, n);
}
}
|
Javascript
<script>
function distinctAdjacentElement(a, n) {
let m = new Map();
for (let i = 0; i < n; ++i) {
m[a[i]]++;
if (m.has(a[i])) {
m.set(a[i], m.get(a[i]) + 1)
} else {
m.set(a[i], 1)
}
}
let mx = 0;
for (let i = 0; i < n; ++i)
if (mx < m.get(a[i]))
mx = m.get(a[i]);
if (mx > Math.floor((n + 1) / 2))
document.write( "NO" + "<br>" );
else
document.write( "YES<br>" );
}
let a = [7, 7, 7, 7];
let n = a.length;
distinctAdjacentElement(a, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jul, 2022
Like Article
Save Article