-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from accodes21/main
Added Leetcode problem no 747
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+70 KB
LeetCode/Largest-Number-At-Least-Twice-of-Others/Java/Screenshot (391).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions
18
LeetCode/Largest-Number-At-Least-Twice-of-Others/Java/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public int dominantIndex(int[] nums) { | ||
int max = 0, maxIndex = 0; | ||
for(int i = 0; i<nums.length; i++){ | ||
if(max < nums[i]){ | ||
max = nums[i]; | ||
maxIndex = i; | ||
} | ||
} | ||
|
||
for(int i = 0; i<nums.length; i++){ | ||
if(maxIndex != i && nums[maxIndex] < 2 * nums[i]){ | ||
return -1; | ||
} | ||
} | ||
return maxIndex; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
LeetCode/Largest-Number-At-Least-Twice-of-Others/Problem.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# [747. Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | ||
|
||
You are given an integer array nums where the largest integer is unique. | ||
|
||
Determine 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, or return -1 otherwise. | ||
|
||
|
||
|
||
Example 1: | ||
``` | ||
Input: nums = [3,6,1,0] | ||
Output: 1 | ||
Explanation: 6 is the largest integer. | ||
For every other number in the array x, 6 is at least 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 is less than twice the value of 3, so we return -1. | ||
``` | ||
|
||
|
||
|
||
Constraints: | ||
``` | ||
2 <= nums.length <= 50 | ||
0 <= nums[i] <= 100 | ||
The largest element in nums is unique. | ||
``` |