# Find the index of the element in an array which divides most elements before it

Given an array arr, the task is to find the index of the element in an array which divides most elements before it

Examples:

```Input: arr = {5, 2, 1, 4, 5, 8, 2}
Output: 6
Explanation
arr = 2
it divides 2, 4, and 8 (3 elements)

Input: arr = {8, 1, 28, 4, 1, 6, 7}
Output: 4
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Maintain a map.
• For each arr[i] update the count variable by looking into map for ar[i] and insert all divisor of ar[i] into map.
• Update maxElement if cnt > maxx.
• Finally return the index with maxElement.

Below is the implementation of above approach:

 `// C++ program find the index of the element ` `// in an array which divides ` `// most elements before it ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to get the max element ` `// divisible by arr[i] ` `int` `maxElement(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``map<``int``, ``int``> mp; ` `    ``int` `maxx = -1, maxElement = -1; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``int` `num = arr[i]; ` `        ``int` `cnt = 0; ` ` `  `        ``// Update count for A[i] ` `        ``if` `(mp.find(num) != mp.end()) { ` `            ``cnt += mp[num]; ` `        ``} ` ` `  `        ``// Generate Divisor For A[i] ` `        ``for` `(``int` `j = 1; j * j <= num; j++) { ` `            ``if` `(num % j == 0) { ` `                ``mp[j]++; ` `                ``if` `(j != num / j) ` `                    ``mp[num / j]++; ` `            ``} ` `        ``} ` ` `  `        ``// Update Max Element ` `        ``if` `(cnt > maxx) { ` `            ``maxElement = i; ` `            ``maxx = cnt; ` `        ``} ` `    ``} ` ` `  `    ``return` `maxElement; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `arr[] = { 5, 2, 1, 4, 5, 8, 2 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << maxElement(arr, n) << ``'\n'``; ` ` `  `    ``return` `0; ` `} `

 `// Java program find the index of the element ` `// in an array which divides ` `// most elements before it ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to get the max element ` `    ``// divisible by arr[i] ` `    ``static` `int` `maxElement(``int` `arr[], ``int` `n)  ` `    ``{ ` ` `  `        ``HashMap mp = ``new` `HashMap(); ` `        ``int` `maxx = -``1``, maxElement = -``1``; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``int` `num = arr[i]; ` `            ``int` `cnt = ``0``; ` ` `  `            ``// Update count for A[i] ` `            ``if` `(mp.containsKey(num)) ` `            ``{ ` `                ``cnt += mp.get(num); ` `            ``} ` ` `  `            ``// Generate Divisor For A[i] ` `            ``for` `(``int` `j = ``1``; j * j <= num; j++) ` `            ``{ ` `                ``if` `(num % j == ``0``)  ` `                ``{ ` `                    ``if` `(mp.containsKey(j)) ` `                        ``mp.put(j, mp.get(j) + ``1``); ` `                    ``else` `                        ``mp.put(j, ``1``); ` `                    ``if` `(j != num / j) ` `                        ``if` `(mp.containsKey(num / j)) ` `                            ``mp.put(num / j, mp.get(num / j) + ``1``); ` `                        ``else` `                            ``mp.put(num / j, ``1``); ` `                ``} ` `            ``} ` ` `  `            ``// Update Max Element ` `            ``if` `(cnt > maxx) ` `            ``{ ` `                ``maxElement = i; ` `                ``maxx = cnt; ` `            ``} ` `        ``} ` ` `  `        ``return` `maxElement; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` ` `  `        ``int` `arr[] = { ``5``, ``2``, ``1``, ``4``, ``5``, ``8``, ``2` `}; ` `        ``int` `n = arr.length; ` ` `  `        ``System.out.print(maxElement(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

 `# Python3 program find the index of the element ` `# in an array which divides ` `# most elements before it ` ` `  `# Function to get the max element ` `# divisible by arr[i] ` `def` `maxElement(arr, n): ` ` `  `    ``mp ``=` `dict``() ` `    ``maxx ``=` `-``1` `    ``maxElement ``=` `-``1` ` `  `    ``for` `i ``in` `range``(n): ` `        ``num ``=` `arr[i] ` `        ``cnt ``=` `0` ` `  `        ``# Update count for A[i] ` `        ``if` `(num ``in` `mp): ` `            ``cnt ``+``=` `mp[num] ` ` `  `        ``# Generate Divisor For A[i] ` `        ``j ``=` `1` ` `  `        ``while` `j ``*` `j <``=` `num: ` `            ``if` `(num ``%` `j ``=``=` `0``): ` `                ``mp[j] ``=` `mp.get(j, ``0``) ``+` `1` `                ``if` `(j !``=` `num ``/``/` `j): ` `                    ``mp[num ``/``/` `j] ``=` `mp.get(num``/``/``j, ``0``) ``+` `1` `            ``j ``+``=` `1` ` `  `        ``# Update Max Element ` `        ``if` `(cnt > maxx): ` `            ``maxElement ``=` `i ` `            ``maxx ``=` `cnt ` ` `  `    ``return` `maxElement ` ` `  `# Driver code ` `arr ``=` `[``5``, ``2``, ``1``, ``4``, ``5``, ``8``, ``2``] ` `n ``=` `len``(arr) ` ` `  `print``(maxElement(arr, n)) ` ` `  `# This code is contributed by mohit kumar 29 `

 `// C# program find the index of the element ` `// in an array which divides ` `// most elements before it ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to get the max element ` `    ``// divisible by arr[i] ` `    ``static` `int` `maxElement(``int` `[]arr, ``int` `n)  ` `    ``{ ` ` `  `        ``Dictionary<``int``, ``int``> mp = ``new` `Dictionary<``int``, ``int``>(); ` `        ``int` `maxx = -1, maxElement = -1; ` ` `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``int` `num = arr[i]; ` `            ``int` `cnt = 0; ` ` `  `            ``// Update count for A[i] ` `            ``if` `(mp.ContainsKey(num)) ` `            ``{ ` `                ``cnt += mp[num]; ` `            ``} ` ` `  `            ``// Generate Divisor For A[i] ` `            ``for` `(``int` `j = 1; j * j <= num; j++) ` `            ``{ ` `                ``if` `(num % j == 0)  ` `                ``{ ` `                    ``if` `(mp.ContainsKey(j)) ` `                        ``mp[j] = mp[j] + 1; ` `                    ``else` `                        ``mp.Add(j, 1); ` `                    ``if` `(j != num / j) ` `                        ``if` `(mp.ContainsKey(num / j)) ` `                            ``mp[num / j] = mp[num / j] + 1; ` `                        ``else` `                            ``mp.Add(num / j, 1); ` `                ``} ` `            ``} ` ` `  `            ``// Update Max Element ` `            ``if` `(cnt > maxx) ` `            ``{ ` `                ``maxElement = i; ` `                ``maxx = cnt; ` `            ``} ` `        ``} ` ` `  `        ``return` `maxElement; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` ` `  `        ``int` `[]arr = { 5, 2, 1, 4, 5, 8, 2 }; ` `        ``int` `n = arr.Length; ` ` `  `        ``Console.Write(maxElement(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:
```6
```

Time Complexity: O(N√max(Arr))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :