Skip to content

Commit 5d58eed

Browse files
committed
added Find Lucky Integer in an Array (easy)
1 parent dc61c05 commit 5d58eed

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Find Lucky Integer in an Array
3+
[Leetcode Link](https://leetcode.com/problems/find-lucky-integer-in-an-array/)
4+
5+
## Problem:
6+
7+
Given an array of integers `arr`, a lucky integer is an integer which has a frequency in the array equal to its value.
8+
9+
Return *a lucky integer* in the array. If there are multiple lucky integers return the **largest** of them. If there is no lucky integer return **-1**.
10+
11+
## Example:
12+
13+
```
14+
Input: arr = [2,2,3,4]
15+
Output: 2
16+
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
17+
```
18+
```
19+
Input: arr = [1,2,2,3,3,3]
20+
Output: 3
21+
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
22+
```
23+
```
24+
Input: arr = [2,2,2,3,3]
25+
Output: -1
26+
Explanation: There are no lucky numbers in the array.
27+
```
28+
```
29+
Input: arr = [5]
30+
Output: -1
31+
```
32+
```
33+
Input: arr = [7,7,7,7,7,7,7]
34+
Output: 7
35+
```
36+
37+
## Note:
38+
39+
- `1 <= arr.length <= 500`
40+
- `1 <= arr[i] <= 500`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package Easy.FindLuckyIntegerInArray;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class Solution {
11+
// Array with size 500 method
12+
// Leetcode submission: 1ms (faster than 99.45%), 39.2MB (less than 65.26%)
13+
public int findLucky(int[] arr) {
14+
// Using HashMap as frequency counter is good
15+
// but we can simply use array of size int[max] to reduce memory usage
16+
int[] frequency = new int[500];
17+
for (int num: arr) {
18+
frequency[num-1]++;
19+
}
20+
// System.out.println(Arrays.toString(frequency));
21+
// find the largest lucky number
22+
for (int i = frequency.length-1; i >= 0; i--) {
23+
if (i+1 == frequency[i]) {
24+
return i+1;
25+
}
26+
}
27+
return -1;
28+
}
29+
30+
// HashMap method
31+
// Leetcode submission: 4ms (faster than 44.68%), 39.5MB (less than 42.53%)
32+
// public int findLucky(int[] arr) {
33+
// Map<Integer, Integer> frequency = new HashMap<>();
34+
// for (int num: arr) {
35+
// if (frequency.containsKey(num)) {
36+
// frequency.put(num, frequency.get(num)+1);
37+
// } else {
38+
// frequency.put(num, 1);
39+
// }
40+
// }
41+
// // System.out.println(frequency.toString());
42+
// List<Integer> luckyNumbers = new ArrayList<>();
43+
// frequency.forEach((k,v) -> {
44+
// if (k == v) {
45+
// luckyNumbers.add(k);
46+
// }
47+
// });
48+
// try {
49+
// // System.out.println(Collections.max(luckyNumbers));
50+
// return Collections.max(luckyNumbers);
51+
// } catch (Exception e) {
52+
// return -1;
53+
// }
54+
// }
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Easy.FindLuckyIntegerInArray;
2+
3+
import java.util.Arrays;
4+
5+
public class TestDriver {
6+
public static void main(String[] args) {
7+
Solution sol = new Solution();
8+
int[] arr1 = {2, 2, 3, 4};
9+
System.out.println("Input: arr = " + Arrays.toString(arr1));
10+
System.out.println("Output: " + sol.findLucky(arr1));
11+
System.out.println();
12+
13+
int[] arr2 = {1,2,2,3,3,3};
14+
System.out.println("Input: arr = " + Arrays.toString(arr2));
15+
System.out.println("Output: " + sol.findLucky(arr2));
16+
System.out.println();
17+
18+
int[] arr3 = {2, 2, 2, 3, 3};
19+
System.out.println("Input: arr = " + Arrays.toString(arr3));
20+
System.out.println("Output: " + sol.findLucky(arr3));
21+
System.out.println();
22+
23+
int[] arr4 = {5};
24+
System.out.println("Input: arr = " + Arrays.toString(arr4));
25+
System.out.println("Output: " + sol.findLucky(arr4));
26+
System.out.println();
27+
28+
int[] arr5 = {7, 7, 7, 7, 7, 7, 7};
29+
System.out.println("Input: arr = " + Arrays.toString(arr5));
30+
System.out.println("Output: " + sol.findLucky(arr5));
31+
System.out.println();
32+
}
33+
}

Easy/ShortedUnsortedContinuousSubarray/Solution.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package Easy.ShortedUnsortedContinuousSubarray;
2+
13
import java.util.Arrays;
24

35
// Sort array and check the length of unmatching subarray

Easy/ShortedUnsortedContinuousSubarray/TestDriver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package Easy.ShortedUnsortedContinuousSubarray;
2+
13
import java.util.Arrays;
24

35
class TestDriver {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Languages used: Java and Python
2828
- [Majority Element](Easy/MajorityElement)
2929
- [Reshape the Matrix](Easy/ReshapeMatrix)
3030
- [Shortest Unsorted Continuous Subarray](Easy/ShortedUnsortedContinuousSubarray)
31+
- [Find Lucky Integer in an Array](Easy/FindLuckyIntegerInArray)
3132
- Medium
3233
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
3334
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)