Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
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.
Examples :
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 :
// Java implementation of the approach import java.util.HashMap; class GFG { // Function to return the minimum replacements public static int minReplace(int a[], int n) { int i; // Map to store the frequency of // the numbers at the even indices HashMap<Integer, Integer> te = new HashMap<>(); // Map to store the frequency of // the numbers at the odd indices HashMap<Integer, Integer> to = new HashMap<>(); for (i = 0; i < n; i++) { // Checking if the index // is odd or even if (i % 2 == 0) { // If the number is already present then, // just increase the occurrence by 1 if (te.containsKey(a[i])) te.put(a[i], te.get(a[i]) + 1); else te.put(a[i], 1); } else { // If the number is already present then, // just increase the occurrence by 1 if (to.containsKey(a[i])) to.put(a[i], to.get(a[i]) + 1); else to.put(a[i], 1); } } // To store the character with // maximum frequency in even indices. int me = -1; // To store the character with // maximum frequency in odd indices. int mo = -1; // To store the frequency of the // maximum occurring number in even indices. int ce = -1; // To store the frequency of the // maximum occurring number in odd indices. int co = -1; // Iterating over Map of even indices to // get the maximum occurring number. for (Integer It : te.keySet()) { if (te.get(It) > ce) { ce = te.get(It); me = It; } } // Iterating over Map of odd indices to // get the maximum occurring number. for (Integer It : to.keySet()) { if (to.get(It) > co) { co = to.get(It); mo = It; } } // To store the final answer int res = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) { // If the index is even but // a[i] != me // then a[i] needs to be replaced if (a[i] != me) res++; } else { // If the index is odd but // a[i] != mo // then a[i] needs to be replaced if (a[i] != mo) res++; } } return res; } // Driver code public static void main(String[] args) { int n; n = 4; int a[] = { 3, 1, 3, 2 }; System.out.println(minReplace(a, n)); } } |
1
Recommended Posts:
- 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
- Minimum operations required to remove an array
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum no. of operations required to make all Array Elements Zero
- Form minimum number from given sequence
- Minimum number of given operations required to reduce the array to 0 element
- Minimum operations required to make all the array elements equal
- Minimum number of operations required to delete all elements of the array
- Find minimum length sub-array which has given sub-sequence in it
- 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
- Find the minimum number of operations required to make all array elements equal
- Longest sub-sequence of array containing Lucas numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



