Skip to content

Commit d7bda02

Browse files
authored
Merge pull request neetcode-gh#1432 from MukulGaur/add-496-Next-Greater-Element-I
create 496-Next-Greater-Element-I.java
2 parents f6a3248 + e666953 commit d7bda02

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

java/496-Next-Greater-Element-I.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
3+
4+
int[] res = new int[nums1.length];
5+
int counter=0;
6+
7+
for(int i: nums1){
8+
res[counter++]=ans(i, nums2);
9+
}
10+
11+
return res;
12+
13+
}
14+
15+
private int ans(int i, int[] nums){
16+
for(int n=0; n<nums.length; n++){
17+
if(nums[n]==i){
18+
for(int j=n+1; j<nums.length; j++){
19+
if(nums[j]>i)
20+
return nums[j];
21+
}
22+
}
23+
}
24+
return -1;
25+
}
26+
}

0 commit comments

Comments
 (0)