Given an array A[] of size N, the task is to find the minimum number of moves required to make array elements equal where you can make adjacent elements equal in one move.
Examples:
Input: A = {1, 6, 5, 1, 7, 1}, N = 6
Output: 3
Explanation: Replace 6 with 1, 5 with 1, and then at last replace 7 with 1. Replacing all elements with 1 will give the answer 3 which is the minimum number of moves required to make an array of identical elements.Input: A = {1, 1, 1, 1, 1}, N = 5
Output: 0
Explanation: The array elements are already identical.
Approach: This can be solved with the following idea:
Using map data structure, store the frequency of each element. Among them find the element having the maximum frequency.
Below are the steps involved in the implementation of the code:
- The element which needs to be identical must be selected greedily. Let this element be X.
- The element with maximum frequency will be the element that must be filled in an entire array.
- The reason is that element with higher frequency already owns a large number of identical slots in different indexes.
- Store the frequency count of each element in a map.
- Let mx be the frequency of the maximum repeating element in an array.
- The answer will be N-mx as there is N-mx number of indexes to be replaced by X.
The code for the above approach is given below:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the answer int makeElementEqual(vector<int> A, int N) { // Create an unordered_map to count // the frequency of elements in array unordered_map<int, int> mp; // Traverse the array and increment // the element count in map for (int i : A) { mp[i]++; } // Store the maximum freuency int mx = -1; for (auto i : mp) { mx = max(i.second, mx); } return N - mx; } // Driver Code int main() { vector<int> A = { 1, 6, 5, 1, 7, 1 }; int N = 6; // Function call int res = makeElementEqual(A, N); cout << res << "\n"; return 0; }
Java
// Java program for the above approach import java.util.*; public class Main { // Function to find the answer public static int makeElementEqual(ArrayList<Integer> A, int N) { // Create a HashMap to count // the frequency of elements in array HashMap<Integer, Integer> map = new HashMap<>(); // Traverse the array and increment // the element count in map for (int i : A) { map.put(i, map.getOrDefault(i, 0) + 1); } // Store the maximum frequency int mx = -1; for (Map.Entry<Integer, Integer> entry : map.entrySet()) { mx = Math.max(entry.getValue(), mx); } return N - mx; } // Driver Code public static void main(String[] args) { ArrayList<Integer> A = new ArrayList<>( Arrays.asList(1, 6, 5, 1, 7, 1)); int N = 6; // Function call int res = makeElementEqual(A, N); System.out.println(res); } }
Python3
def makeElementEqual(A, N): # Create a dictionary to count the frequency of elements in the list mp = {} # Traverse the list and increment the count of each element in the dictionary for i in A: if i in mp: mp[i] += 1 else: mp[i] = 1 # Store the maximum frequency mx = -1 for i in mp: mx = max(mp[i], mx) # Return the difference between the length of the list and the maximum frequency return N - mx # Driver Code if __name__ == '__main__': A = [1, 6, 5, 1, 7, 1] N = 6 # Function call res = makeElementEqual(A, N) print(res)
C#
using System; using System.Collections.Generic; public class Program { // Function to find the answer static int MakeElementEqual(List<int> A, int N) { // Create a dictionary to count the frequency of // elements in array Dictionary<int, int> dict = new Dictionary<int, int>(); // Traverse the array and increment the element // count in dictionary foreach(int i in A) { if (dict.ContainsKey(i)) dict[i]++; else dict.Add(i, 1); } // Store the maximum frequency int mx = -1; foreach(KeyValuePair<int, int> kvp in dict) { mx = Math.Max(kvp.Value, mx); } return N - mx; } // Driver Code static void Main(string[] args) { List<int> A = new List<int>{ 1, 6, 5, 1, 7, 1 }; int N = 6; // Function call int res = MakeElementEqual(A, N); Console.WriteLine(res); } }
Javascript
// Function to find the answer function makeElementEqual(A, N) { // Create a map to count the frequency // of elements in the array let mp = new Map(); // Traverse the array and increment the // element count in the map for (let i of A) { mp.set(i, (mp.get(i) || 0) + 1); } // Store the maximum frequency let mx = -1; for (let i of mp) { mx = Math.max(i[1], mx); } return N - mx; } // Driver Code let A = [ 1, 6, 5, 1, 7, 1 ]; let N = 6; // Function call let res = makeElementEqual(A, N); console.log(res); //This code is contributed by Tushar Rokade
3
Time Complexity:
Auxiliary Space:
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!