Skip to content

Commit

Permalink
Merge pull request #44 from accodes21/main
Browse files Browse the repository at this point in the history
Added Leetcode problem no 747
  • Loading branch information
iamdestinychild authored Oct 4, 2023
2 parents 5176dac + 5df6572 commit dc144db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 LeetCode/Largest-Number-At-Least-Twice-of-Others/Problem.md
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.
```

0 comments on commit dc144db

Please sign in to comment.