Showing:
Handle Score
@Ibrahim Nash 5761
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4944
@saiujwal13083 4506
@sanjay05 3762
@marius_valentin_dragoi 3516
@sushant_a 3459
@verma_ji 3341
@KshamaGupta 3318
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 .

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).

Constraints:
2 ≤ N ≤ 106

### Editorial

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