Skip to content

Commit 7580751

Browse files
Solution of 747 (LC No.) is added
1 parent 3c2bdd3 commit 7580751

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
id: largest-number-atleast-twice-of-others
3+
title: Largest Number At Least Twice of Others
4+
sidebar_label: 747-Largest-Number-Atleast-Twice-Of-Other
5+
tags:
6+
- Array
7+
- Sorting
8+
---
9+
10+
## Problem Description
11+
You are given an integer array `nums` where the largest integer is unique.
12+
13+
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.
14+
15+
### Example
16+
17+
**Example 1:**
18+
19+
20+
```
21+
Input: nums = [3,6,1,0]
22+
Output: 1
23+
Explanation: 6 is the largest integer.
24+
For every other number in the array x, 6 is at least twice as big as x.
25+
The index of value 6 is 1, so we return 1.
26+
```
27+
**Example 2:**
28+
```
29+
Input: nums = [1,2,3,4]
30+
Output: -1
31+
Explanation: 4 is less than twice the value of 3, so we return -1.
32+
```
33+
### Constraints
34+
35+
- `0 <= nums[i] <= 100`
36+
37+
## Solution Approach
38+
39+
### Intuition:
40+
41+
To efficiently determine Largest Number At Least Twice of Others
42+
## Solution Implementation
43+
44+
### Code In Different Languages:
45+
46+
<Tabs>
47+
<TabItem value="JavaScript" label="JavaScript" default>
48+
<SolutionAuthor name="@Ishitamukherjee2004"/>
49+
```javascript
50+
51+
class Solution {
52+
dominantIndex(nums) {
53+
let cnt = 0, index = -1;
54+
let maxi = -Infinity, smax = -Infinity;
55+
for(let i = 0; i < nums.length; i++){
56+
if(nums[i] >= maxi){
57+
smax = maxi;
58+
maxi = nums[i];
59+
index = i;
60+
} else if(nums[i] < maxi && nums[i] >= smax){
61+
smax = nums[i];
62+
}
63+
}
64+
if(maxi >= 2 * smax) return index;
65+
return -1;
66+
}
67+
}
68+
69+
70+
71+
72+
73+
```
74+
75+
</TabItem>
76+
<TabItem value="TypeScript" label="TypeScript">
77+
<SolutionAuthor name="@Ishitamukherjee2004"/>
78+
```typescript
79+
class Solution {
80+
dominantIndex(nums: number[]): number {
81+
let cnt = 0, index = -1;
82+
let maxi = -Infinity, smax = -Infinity;
83+
for(let i = 0; i < nums.length; i++){
84+
if(nums[i] >= maxi){
85+
smax = maxi;
86+
maxi = nums[i];
87+
index = i;
88+
} else if(nums[i] < maxi && nums[i] >= smax){
89+
smax = nums[i];
90+
}
91+
}
92+
if(maxi >= 2 * smax) return index;
93+
return -1;
94+
}
95+
}
96+
97+
98+
99+
100+
```
101+
102+
</TabItem>
103+
<TabItem value="Python" label="Python">
104+
<SolutionAuthor name="@Ishitamukherjee2004"/>
105+
```python
106+
class Solution:
107+
def dominantIndex(self, nums):
108+
cnt = 0
109+
index = -1
110+
maxi = float('-inf')
111+
smax = float('-inf')
112+
for i in range(len(nums)):
113+
if nums[i] >= maxi:
114+
smax = maxi
115+
maxi = nums[i]
116+
index = i
117+
elif nums[i] < maxi and nums[i] >= smax:
118+
smax = nums[i]
119+
if maxi >= 2 * smax:
120+
return index
121+
return -1
122+
123+
124+
125+
126+
127+
```
128+
129+
</TabItem>
130+
<TabItem value="Java" label="Java">
131+
<SolutionAuthor name="@Ishitamukherjee2004"/>
132+
```java
133+
public class Solution {
134+
public int dominantIndex(int[] nums) {
135+
int cnt = 0, index = -1;
136+
int maxi = Integer.MIN_VALUE, smax = Integer.MIN_VALUE;
137+
for(int i = 0; i < nums.length; i++){
138+
if(nums[i] >= maxi){
139+
smax = maxi;
140+
maxi = nums[i];
141+
index = i;
142+
} else if(nums[i] < maxi && nums[i] >= smax){
143+
smax = nums[i];
144+
}
145+
}
146+
if(maxi >= 2 * smax) return index;
147+
return -1;
148+
}
149+
}
150+
151+
152+
153+
154+
```
155+
156+
</TabItem>
157+
<TabItem value="C++" label="C++">
158+
<SolutionAuthor name="@Ishitamukherjee2004"/>
159+
```cpp
160+
class Solution {
161+
public:
162+
int dominantIndex(vector<int>& nums) {
163+
int cnt=0, index = -1;
164+
int maxi = INT_MIN;
165+
int smax = INT_MIN;
166+
for(int i=0; i<nums.size(); i++){
167+
if(nums[i]>=maxi){
168+
smax = maxi;
169+
maxi = nums[i];
170+
index = i;
171+
}
172+
else if(nums[i]<maxi && nums[i]>=smax){
173+
smax = nums[i];
174+
}
175+
}
176+
if(maxi>=2*smax) return index;
177+
return -1;
178+
}
179+
};
180+
```
181+
</TabItem>
182+
</Tabs>
183+
184+
#### Complexity Analysis
185+
186+
- Time Complexity: $$O(n)$$
187+
- Space Complexity: $$O(1)$$
188+
- The time complexity is $$O(n)$$ where n is the length of the input array nums. This is because the algorithm iterates through the array once, performing a constant amount of work for each element.
189+
- The space complexity is $$O(1)$$ because we are not using any extra space.

0 commit comments

Comments
 (0)