Open In App

Minimum index of element with maximum multiples in Array

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
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: 1
Explanation:

  • 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 =2

Input: n = 3, arr[] = {1, 4, 8}
Output: 0
Explanation: 

  • 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));


Output

1






Time Complexity: O(n)
Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads