package com.fishercoder.solutions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 747. Largest Number Greater Than Twice of Others
 *
 * 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.

 Example 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.

 Example 2:
 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 _747 {

  public static class Solution1 {
    public int dominantIndex(int[] nums) {
      Map<Integer, Integer> map = new HashMap<>();
      int max;
      int secondMax;
      for (int i = 0; i < nums.length; i++) {
        map.put(nums[i], i);
      }
      Arrays.sort(nums);
      max = nums[nums.length - 1];
      secondMax = nums[nums.length - 2];
      if (max >= 2 * secondMax) {
        return map.get(max);
      } else {
        return -1;
      }
    }
  }

  public static class Solution2 {
    public int dominantIndex(int[] nums) {
      int max = Integer.MIN_VALUE;
      int maxIndex = -1;
      for (int i = 0; i < nums.length; i++) {
        if (nums[i] > max) {
          max = nums[i];
          maxIndex = i;
        }
      }
      for (int i = 0; i < nums.length; i++) {
        if (nums[i] * 2 > max && i != maxIndex) {
          return -1;
        }
      }
      return maxIndex;
    }
  }
}

@@ -107,794 +107,8 @@
 p;%0A%0A
-/**%0A * 747. Largest Number Greater Than Twice of Others%0A *%0A * In a given integer array nums, there is always exactly one largest element.%0A * Find whether the largest element in the array is at least twice as much as every other number in the array.%0A * If it is, return the index of the largest element, otherwise return -1.%0A%0A Example 1:%0A Input: nums = %5B3, 6, 1, 0%5D%0A Output: 1%0A Explanation: 6 is the largest integer, and for every other number in the array x,%0A 6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.%0A%0A Example 2:%0A Input: nums = %5B1, 2, 3, 4%5D%0A Output: -1%0A Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.%0A%0A Note:%0A nums will have a length in the range %5B1, 50%5D.%0A Every nums%5Bi%5D will be an integer in the range %5B0, 99%5D.%0A */%0A
 publ
@@ -124,16 +124,18 @@
 _747 %7B%0A%0A
+  
   public
@@ -160,24 +160,28 @@
 tion1 %7B%0A    
+ 
+   
 public int d
@@ -213,16 +213,22 @@
 %7B%0A      
+ 
+     
 Map%3CInte
@@ -266,24 +266,30 @@
 %3C%3E();%0A      
+   
+   
 int max;%0A   
@@ -287,24 +287,30 @@
  max;%0A      
+     
+ 
 int secondMa
@@ -308,24 +308,30 @@
  secondMax;%0A
+      
       for (i
@@ -356,32 +356,40 @@
 .length; i++) %7B%0A
+        
         map.put(
@@ -407,24 +407,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 Arrays.s
@@ -446,16 +446,22 @@
 (nums);%0A
+      
       ma
@@ -487,16 +487,22 @@
 h - 1%5D;%0A
+      
       se
@@ -534,16 +534,22 @@
 h - 2%5D;%0A
+      
       if
@@ -578,24 +578,32 @@
 ) %7B%0A        
+  
+      
 return map.g
@@ -617,16 +617,22 @@
 ;%0A      
+  
+    
 %7D else %7B
@@ -632,32 +632,40 @@
  else %7B%0A        
+    
+    
 return -1;%0A     
@@ -669,21 +669,35 @@
     
+   
+   
 %7D%0A    
+ 
+   
 %7D%0A  
+ 
+ 
 %7D%0A%0A
+  
   pu
@@ -726,24 +726,28 @@
 tion2 %7B%0A    
+ 
+   
 public int d
@@ -775,24 +775,30 @@
 ms) %7B%0A      
+ 
+     
 int max = In
@@ -814,16 +814,22 @@
 _VALUE;%0A
+      
       in
@@ -847,24 +847,30 @@
 = -1;%0A      
+     
+ 
 for (int i =
@@ -889,32 +889,40 @@
 .length; i++) %7B%0A
+        
         if (nums
@@ -936,32 +936,42 @@
 ax) %7B%0A          
+       
+   
 max = nums%5Bi%5D;%0A 
@@ -979,16 +979,26 @@
         
+     
+     
 maxIndex
@@ -1007,34 +1007,34 @@
  i;%0A        
+  
-%7D%0A
       %7D%0A
       for (i
@@ -1021,22 +1021,42 @@
       %7D%0A
+            %7D%0A    
       
+  
 for (int
@@ -1087,32 +1087,40 @@
  i++) %7B%0A        
+      
+  
 if (nums%5Bi%5D * 2 
@@ -1147,32 +1147,42 @@
 ex) %7B%0A          
+     
+     
 return -1;%0A     
@@ -1184,24 +1184,46 @@
         
+        %7D%0A            
 %7D%0A      
 %7D%0A      
@@ -1210,26 +1210,24 @@
     %7D%0A      
-%7D%0A
       return
@@ -1241,16 +1241,22 @@
 ex;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 747