Given an array of integers of length n. Our task is to return the index of the max element if the it is at least twice as much as every other number in the array. If the max element does not satisfy the condition return -1.

**Examples:**

Input : arr = {3, 6, 1, 0} Output : 1 Here, 6 is the largest integer, and for every other number in the array x, 6 is more than twice as big as x. The index of value 6 is 1, so we return 1. Input : arr = {1, 2, 3, 4} Output : -1 4 isn't at least as big as twice the value of 3, so we return -1.

**Approach : ** Scan through the array to find the unique largest element **m**, keeping track of it’s index **maxIndex**. Scan through the array again. If we find some **x != m** with **m < 2*x**, we should return **-1**. Otherwise, we should return **maxIndex**.

## C++

`// CPP program for Maximum of ` `// the array which is at least ` `// twice of other elements of ` `// the array. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// index of Max element ` `// that satisfies the ` `// condition ` `int` `findIndex(` `int` `arr[], ` `int` `len) { ` ` ` ` ` `// Finding index of ` ` ` `// max of the array ` ` ` `int` `maxIndex = 0; ` ` ` `for` `(` `int` `i = 0; i < len; ++i) ` ` ` `if` `(arr[i] > arr[maxIndex]) ` ` ` `maxIndex = i; ` ` ` ` ` `// Returns -1 if the ` ` ` `// max element is not ` ` ` `// twice of the i-th ` ` ` `// element. ` ` ` `for` `(` `int` `i = 0; i < len; ++i) ` ` ` `if` `(maxIndex != i && ` ` ` `arr[maxIndex] < 2 * arr[i]) ` ` ` `return` `-1; ` ` ` ` ` `return` `maxIndex; ` `} ` ` ` `// Driver function ` `int` `main(){ ` ` ` `int` `arr[] = {3, 6, 1, 0}; ` ` ` `int` `len = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout<<(findIndex(arr, len)); ` `} ` ` ` `// This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Java

`// Java program for Maximum of the array ` `// which is at least twice of other elements ` `// of the array. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GfG { ` ` ` ` ` `// Function to find the index of Max element ` ` ` `// that satisfies the condition ` ` ` `public` `static` `int` `findIndex(` `int` `[] arr) { ` ` ` ` ` `// Finding index of max of the array ` ` ` `int` `maxIndex = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; ++i) ` ` ` `if` `(arr[i] > arr[maxIndex]) ` ` ` `maxIndex = i; ` ` ` ` ` `// Returns -1 if the max element is not ` ` ` `// twice of the i-th element. ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; ++i) ` ` ` `if` `(maxIndex != i && arr[maxIndex] < ` `2` `* arr[i]) ` ` ` `return` `-` `1` `; ` ` ` ` ` `return` `maxIndex; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String argc[]){ ` ` ` `int` `[] arr = ` `new` `int` `[]{` `3` `, ` `6` `, ` `1` `, ` `0` `}; ` ` ` `System.out.println(findIndex(arr)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program for Maximum of ` `# the array which is at least twice ` `# of other elements of the array. ` ` ` `# Function to find the index of Max ` `# element that satisfies the condition ` `def` `findIndex(arr): ` ` ` ` ` `# Finding index of max of the array ` ` ` `maxIndex ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `,` `len` `(arr)): ` ` ` `if` `(arr[i] > arr[maxIndex]): ` ` ` `maxIndex ` `=` `i ` ` ` ` ` `# Returns -1 if the max element is not ` ` ` `# twice of the i-th element. ` ` ` `for` `i ` `in` `range` `(` `0` `,` `len` `(arr)): ` ` ` `if` `(maxIndex !` `=` `i ` `and` ` ` `arr[maxIndex] < (` `2` `*` `arr[i])): ` ` ` `return` `-` `1` ` ` ` ` `return` `maxIndex ` ` ` ` ` `# Driver code ` `arr ` `=` `[` `3` `, ` `6` `, ` `1` `, ` `0` `] ` `print` `(findIndex(arr)) ` ` ` `# This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program for Maximum of the array ` `// which is at least twice of other elements ` `// of the array. ` `using` `System; ` ` ` `class` `GfG { ` ` ` ` ` `// Function to find the index of Max element ` ` ` `// that satisfies the condition ` ` ` `public` `static` `int` `findIndex(` `int` `[] arr) { ` ` ` ` ` `// Finding index of max of the array ` ` ` `int` `maxIndex = 0; ` ` ` `for` `(` `int` `i = 0; i < arr.Length; ++i) ` ` ` `if` `(arr[i] > arr[maxIndex]) ` ` ` `maxIndex = i; ` ` ` ` ` `// Returns -1 if the max element is not ` ` ` `// twice of the i-th element. ` ` ` `for` `(` `int` `i = 0; i < arr.Length; ++i) ` ` ` `if` `(maxIndex != i && arr[maxIndex] < 2 * arr[i]) ` ` ` `return` `-1; ` ` ` ` ` `return` `maxIndex; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[]{3, 6, 1, 0}; ` ` ` `Console.WriteLine(findIndex(arr)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for Maximum of ` `// the array which is at least ` `// twice of other elements of ` `// the array. ` ` ` `// Function to find the ` `// index of Max element ` `// that satisfies the ` `// condition ` `function` `findIndex(` `$arr` `, ` `$len` `) ` `{ ` ` ` ` ` `// Finding index of ` ` ` `// max of the array ` ` ` `$maxIndex` `= 0; ` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$len` `; ++` `$i` `) ` ` ` `if` `(` `$arr` `[` `$i` `] > ` `$arr` `[` `$maxIndex` `]) ` ` ` `$maxIndex` `= ` `$i` `; ` ` ` ` ` `// Returns -1 if the ` ` ` `// max element is not ` ` ` `// twice of the i-th ` ` ` `// element. ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$len` `; ++` `$i` `) ` ` ` `if` `(` `$maxIndex` `!= ` `$i` `and` ` ` `$arr` `[` `$maxIndex` `] < 2 * ` `$arr` `[` `$i` `]) ` ` ` `return` `-1; ` ` ` ` ` `return` `$maxIndex` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(3, 6, 1, 0); ` `$len` `= ` `count` `(` `$arr` `); ` ` ` `echo` `findIndex(` `$arr` `, ` `$len` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity**:

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.

## Recommended Posts:

- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Strings from an array which are not prefix of any other string
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Minimum window size containing atleast P primes in every window of given range
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Find the element before which all the elements are smaller than it, and after which all are greater
- Count of array elements which are greater than all elements on its left
- Count of lists which are not a subset of any other given lists
- Maximum number of envelopes that can be put inside other bigger envelopes
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Find the maximum element in an array which is first increasing and then decreasing
- Maximum subsequence sum from a given array which is a perfect square
- Count subsequences which contains both the maximum and minimum array element
- Count subarrays which contains both the maximum and minimum array element
- Remove elements from the array which appear more than k times

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.