Minimum index of element with maximum multiples in Array
Last Updated :
19 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findMinIndex( int n, vector< int >& arr)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i]]++;
}
int res;
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 res;
}
int main()
{
int n = 4;
vector< int > arr = { 2, 1, 3, 6 };
cout << findMinIndex(n, arr) << endl;
return 0;
}
|
Java
import java.util.HashMap;
public class Main {
static int findMinIndex( int n, int [] arr)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
mp.put(arr[i], mp.getOrDefault(arr[i], 0 ) + 1 );
}
int res = 0 ;
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 res;
}
public static void main(String[] args)
{
int n = 4 ;
int [] arr = { 2 , 1 , 3 , 6 };
System.out.println(findMinIndex(n, arr));
}
}
|
Python3
def findMinIndex(n, arr):
mp = {}
for i in range (n):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
res = None
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 res
if __name__ = = "__main__" :
n = 4
arr = [ 2 , 1 , 3 , 6 ]
print (findMinIndex(n, arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int FindMinIndex( int n, List< int > arr)
{
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]]++;
}
int res = 0;
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 res;
}
public static void Main( string [] args)
{
int n = 4;
List< int > arr = new List< int >{ 2, 1, 3, 6 };
Console.WriteLine(FindMinIndex(n, arr));
}
}
|
Javascript
function findMinIndex(n, arr) {
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);
}
}
let res;
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 res;
}
let n = 4;
let arr = [2, 1, 3, 6];
console.log(findMinIndex(n, arr));
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...