Minimum index of element with maximum multiples in Array Last Updated : 19 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of length n, the task is to calculate the min index of the element which has maximum elements present in the form of {2 * arr[i], 3 * arr[i], 4 * arr[i], 5 * arr[i]}. Examples: Input: n = 4. arr[] = {2, 1, 3, 6}Output: 1Explanation: For 2, {2*2, 3*2, 4*2, 5*2} => {4, 6, 8, 10}, Here count of present elements in an array are {0, 1, 0, 0} => 0+1+0+0 = 1 For 1, {2*1, 3*1, 4*1, 5*1} => {2, 3, 4, 5}, Here count of present elements in an array are {1, 1, 0, 0} => 1+1+0+0 =2Input: n = 3, arr[] = {1, 4, 8}Output: 0Explanation: For 1 count is 1, For 4 count is 1, and For 8 count is 0.Here index 0 and 1 have similar counts. So, we have to choose the min index as an output. Therefore, output is 0. Approach: This can be solved with the following idea: Using the map data structure, store the occurrence of each element. Below are the steps involved in the implementation of the code: In this problem, we will maintain the map which counts the occurrences of the element and stores it in the map. We traverse the array and store the maximum friend elements in the count variable.The minimum index with maximum friend elements is stored in the res variable.Then we return the res variable.Below is the implementation of the above approach: C++ // C++ code for the above approach: #include <bits/stdc++.h> using namespace std; int findMinIndex(int n, vector<int>& arr) { // Map to store the occurrences of // the arr elements unordered_map<int, int> mp; for (int i = 0; i < n; i++) { mp[arr[i]]++; } // Store the min index int res; // Maintain the maximum elements int max = 0; for (int i = 0; i < n; i++) { int count = 0; for (int j = 2; j <= 5; j++) { count += mp[j * arr[i]]; } if (count > max) { max = count; res = i; } } // Return the resultant min index return res; } // Driver code int main() { int n = 4; vector<int> arr = { 2, 1, 3, 6 }; // Function call cout << findMinIndex(n, arr) << endl; return 0; } Java // JAVA code for the above approach: import java.util.HashMap; public class Main { static int findMinIndex(int n, int[] arr) { // Map to store the occurrences of the arr elements HashMap<Integer, Integer> mp = new HashMap<>(); for (int i = 0; i < n; i++) { mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1); } // Store the min index int res = 0; // Maintain the maximum elements int max = 0; for (int i = 0; i < n; i++) { int count = 0; for (int j = 2; j <= 5; j++) { count += mp.getOrDefault(j * arr[i], 0); } if (count > max) { max = count; res = i; } } // Return the resultant min index return res; } // Driver code public static void main(String[] args) { int n = 4; int[] arr = { 2, 1, 3, 6 }; // Function call System.out.println(findMinIndex(n, arr)); } } // This code is contributed by shivamgupta0987654321 Python3 def findMinIndex(n, arr): # Dictionary to store the occurrences of the arr elements mp = {} for i in range(n): mp[arr[i]] = mp.get(arr[i], 0) + 1 # Store the min index res = None # Maintain the maximum elements max_count = 0 for i in range(n): count = 0 for j in range(2, 6): count += mp.get(j * arr[i], 0) if count > max_count: max_count = count res = i # Return the resultant min index return res # Driver code if __name__ == "__main__": n = 4 arr = [2, 1, 3, 6] # Function call print(findMinIndex(n, arr)) # This code is contributed by shivamgupta310570 C# using System; using System.Collections.Generic; public class GFG { static int FindMinIndex(int n, List<int> arr) { // Dictionary to store the occurrences of the arr // elements Dictionary<int, int> mp = new Dictionary<int, int>(); for (int i = 0; i < n; i++) { if (!mp.ContainsKey(arr[i])) mp[arr[i]] = 0; mp[arr[i]]++; } // Store the min index int res = 0; // Maintain the maximum elements int max = 0; for (int i = 0; i < n; i++) { int count = 0; for (int j = 2; j <= 5; j++) { if (mp.ContainsKey(j * arr[i])) count += mp[j * arr[i]]; } if (count > max) { max = count; res = i; } } // Return the resultant min index return res; } // Driver code public static void Main(string[] args) { int n = 4; List<int> arr = new List<int>{ 2, 1, 3, 6 }; // Function call Console.WriteLine(FindMinIndex(n, arr)); } } // This code is contributed by rambabuguphka JavaScript function findMinIndex(n, arr) { // Map to store the occurrences of // the arr elements let mp = new Map(); for (let i = 0; i < n; i++) { if (mp.has(arr[i])) { mp.set(arr[i], mp.get(arr[i]) + 1); } else { mp.set(arr[i], 1); } } // Store the min index let res; // Maintain the maximum elements let max = 0; for (let i = 0; i < n; i++) { let count = 0; for (let j = 2; j <= 5; j++) { if (mp.has(j * arr[i])) { count += mp.get(j * arr[i]); } } if (count > max) { max = count; res = i; } } // Return the resultant min index return res; } // Driver code let n = 4; let arr = [2, 1, 3, 6]; console.log(findMinIndex(n, arr)); Output1Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Minimum index of element with maximum multiples in Array R riddhishah2222 Follow Improve Article Tags : DSA Arrays Map Practice Tags : ArraysMap Similar Reads Find the element having maximum premutiples in the array Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th 8 min read Program to find the minimum (or maximum) element of an array Given an array, write functions to find the minimum and maximum elements in it. Examples:Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of ar 14 min read Maximize score by multiplying elements of given Array with given multipliers Given two arrays array[] and multipliers[] of size N and M where N is always greater than equal to M. There are M operations to be performed. In each operation, choose multiplier[i] and an element from the array arr[] either from the start or the end let's say K then add multiplier[i]*K to the total 9 min read Find minimum value of K to maximise sum of elements on indices that are multiples of K Given an array arr[] of N integers, the task is to find the minimum value of K such that the sum of elements on indices that are multiples of K is the maximum possible. Example: Input: arr[] = {-3, 4}Output: 2Explanation: For the given array, it the value of K = 1, then the multiples of K are {1, 2} 10 min read Find maximum element among the elements with minimum frequency in given Array Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency. Examples: Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50. Input: arr[] = {3, 2, 5, 13 min read Maximum of minimums of every window size in a given array Given an integer array arr[] of size n, the task is to find the maximum of the minimums for every window size in the given array, where the window size ranges from 1 to n.Example:Input: arr[] = [10, 20, 30]Output: [30, 20, 10]Explanation: First element in output indicates maximum of minimums of all 14 min read Maximum number of multiples in an array before any element Given an array arr[], the task is to find the maximum number of indices j < i such that (arr[j] % arr[i]) = 0 among all the array elements. Example: Input: arr[] = {8, 1, 28, 4, 2, 6, 7} Output: 3 No of multiples for each element before itself - N(8) = 0 () N(1) = 1 (8) N(28) = 0 () N(4) = 2 (28, 6 min read Maximize the sum of modulus with every Array element Given an array A[] consisting of N positive integers, the task is to find the maximum possible value of: F(M) = M % A[0] + M % A[1] + .... + M % A[N -1] where M can be any integer value Examples: Input: arr[] = {3, 4, 6} Output: 10 Explanation: The maximum sum occurs for M = 11. (11 % 3) + (11 % 4) 4 min read Remove minimum elements from array so that max <= 2 * min Given an array arr, the task is to remove minimum number of elements such that after their removal, max(arr) <= 2 * min(arr). Examples: Input: arr[] = {4, 5, 3, 8, 3} Output: 1 Remove 8 from the array. Input: arr[] = {1, 2, 3, 4} Output: 1 Remove 1 from the array. Approach: Let us fix each value 6 min read Find all numbers that divide maximum array elements Given an array of N numbers, the task is to print all the numbers greater than 1 which divides the maximum of array elements. Examples: Input: a[] = {6, 6, 12, 18, 13} Output: 2 3 6 All the numbers divide the maximum of array elements i.e., 4 Input: a[] = {12, 15, 27, 20, 40} Output: 2 3 4 5 Approac 7 min read Like