Last Updated: 20 Aug, 2025

Most Frequent Adjacent Pairs

Moderate
Asked in company
Airtel

Problem statement

You are given an array 'arr' of length 'n', consisting of integers. An adjacent pair in the array is a sequence of two consecutive elements, i.e., {arr[i], arr[i+1]} for 0 <= i < n-1.

Your task is to find the adjacent pair that appears most frequently in the array. If there are multiple pairs with the same maximum frequency, you can return any one of them. The output should be the two integers forming the most frequent pair.


Input Format:
The first line of input contains an integer 'n', representing the size of the array.

The second line contains 'n' space-separated integers, representing the elements of the array 'arr'.


Output Format:
Print two space-separated integers representing the most frequent adjacent pair. For instance, if the most frequent pair is {x, y}, the output should be x y.


Note:
If multiple pairs share the same highest frequency, any of these pairs is an acceptable answer.

An array of size 'n' will have 'n-1' adjacent pairs.