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 :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum operations required to remove an array
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of operations required to delete all elements of the array
- 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 number of given operations required to convert a permutation into an identity permutation
- Find the minimum number of operations required to make all array elements equal
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number of given operations required to reduce the array to 0 element
- Minimum increment or decrement operations required to make the array sorted
- Minimum increment/decrement operations required to make Median as X
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Minimum pair merge operations required to make Array non-increasing
- Minimum number of operations required to return to the main folder
- Minimum changes required to make a Catalan Sequence
- Minimum steps required to rearrange given array to a power sequence of 2
- Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.