Given a sequence of integers of even length ‘n’, the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where ‘i’ is the index.
The operation here is to replace any element of the sequence with any element.
Input : n=4 ; Array : 3, 1, 3, 2 Output : 1 If we change the last element to '1' then, the sequence will become 3, 1, 3, 1 (satisfying the condition) So, only 1 replacement is required. Input : n=6 ; Array : 105 119 105 119 105 119 Output : 0 As the sequence is already in the required state. So, no replacement of elements is required.
Approach : As we see that the indices 0, 2, …, n-2 are connected independently and 1, 3, 5, …, n are connected independently and must have the same value. So,
- We have to find the most occurring number in both the sequences (even and odd) by storing the numbers and their frequency in a map.
- Then every other number of that sequence will have to be replaced with the most occurring number in the same sequence.
- Finally, the count of the replacements from the previous step will be the answer.
Below is the implementation of the above approach :
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum changes required to make a Catalan Sequence
- Find the final sequence of the array after performing given operations
- k-th missing element in increasing sequence which is not present in a given sequence
- Longest sub-sequence with minimum LCM
- Minimum operations required to remove an array
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Form minimum number from given sequence
- Minimum no. of operations required to make all Array Elements Zero
- Find minimum length sub-array which has given sub-sequence in it
- Minimum number of operations required to delete all elements of the array
- Minimum number of given operations required to reduce the array to 0 element
- Minimum operations required to make all the array elements equal
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum increment or decrement operations required to make the array sorted
- Arrange numbers to form a valid sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.