Distinct Adjacent Element
Basic Accuracy: 21.45% Submissions: 126 Points: 1

Given an array arr[] of size N, the task is to check whether it is possible to obtain an array having distinct neighboring elements by swapping two neighboring array elements.

Example 1:

Input: N = 3, arr[] = {1, 1, 2}
Output:  YES
Explanation: Swap 1 (second last element) 
and 2 (last element), to obtain 1 2 1,
which has distinct neighbouring elements.

Example 2:

Input: N = 4, arr[] = {7, 7, 7, 7}
Output: NO
Explanation: We can't swap to obtain 
distinct elements in neighbor .

Your Task:
You don't need to read input or print anything. You just need to complete the function distinctAdjacentElement() that takes array arr[]  and its size N as input parameters and returns a boolean value denoting if distinct neighbours are possible or not. 
Note: The generated output is YES or NO according to the returned value.


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


2 ≤ N ≤ 106


to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Distinct Adjacent Element

Output Window