# C#LeetCode刷题之#747-至少是其他数字两倍的最大数（ Largest Number At Least Twice of Others）

nums 的长度范围在[1, 50].

In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Input: nums = [3, 6, 1, 0]

Output: 1

Explanation: 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: nums = [1, 2, 3, 4]

Output: -1

Explanation: 4 isn’t at least as big as twice the value of 3, so we return -1.

Note:

nums will have a length in the range [1, 50].
Every nums[i] will be an integer in the range [0, 99].

```public class Program {

public static void Main(string[] args) {
int[] cost = null;

cost = new int[] { 10, 15, 20 };
var res = DominantIndex(cost);
Console.WriteLine(res);

}

private static int DominantIndex(int[] nums) {
//记录最大值和索引，最大值也可以不要，只存索引
int max = int.MinValue;
int index = -1;
for(int i = 0; i < nums.Length; i++) {
if(nums[i] > max) {
max = nums[i];
index = i;
}
}
//i不用和自己比较，另外注意不要用除法，否则要处理数组中的0
for(int i = 0; i < nums.Length; i++) {
if(i != index && max < 2 * nums[i]) {
return -1;
}
}
return index;
}

}```

`-1`

(1)