Skip to content

Commit 21ed35a

Browse files
author
Sanajit Jana
committed
Add new problem
1 parent 140247f commit 21ed35a

File tree

3 files changed

+126
-25
lines changed

3 files changed

+126
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ This repository is licensed under the [MIT License](LICENSE).
6363

6464
## 🏆 Progress
6565

66-
- Problems solved: 38
67-
- Last updated: 30 Oct 2025, 05:25 PM UTC+05:30
66+
- Problems solved: 39
67+
- Last updated: 31 Oct 2025, 09:28 AM UTC+05:30
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [3289. The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/description/)
2+
3+
## Question Description
4+
In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.
5+
6+
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
7+
8+
---
9+
10+
## Constraints
11+
- 2 <= n <= 100
12+
- nums.length == n + 2
13+
- 0 <= nums[i] < n
14+
- The input is generated such that nums contains exactly two repeated elements.
15+
16+
---
17+
18+
## Solution 1: Sorting Approach
19+
20+
### Approach
21+
Sort the array and iterate through it to find consecutive duplicates, as the duplicates will be adjacent after sorting.
22+
23+
### Dry Run
24+
Example Input: nums = [0,1,1,0]
25+
26+
Sorted: [0,0,1,1]
27+
28+
Iterate:
29+
- i=0: nums[0]=0 == nums[1]=0, add 0, i+=2 to 2
30+
- i=2: nums[2]=1 == nums[3]=1, add 1, i+=2 to 4
31+
32+
Answer: [0,1]
33+
34+
### Code
35+
```java
36+
class Solution {
37+
public int[] getSneakyNumbers(int[] nums) {
38+
Arrays.sort(nums);
39+
int[] ans = new int[2];
40+
int index = 0;
41+
for (int i = 0; i < nums.length - 1; i++) {
42+
if (nums[i] == nums[i + 1]) {
43+
ans[index++] = nums[i];
44+
i++;
45+
}
46+
if (index == 2) {
47+
break;
48+
}
49+
}
50+
return ans;
51+
}
52+
}
53+
```
54+
55+
### Time and Space Complexity
56+
- **Time Complexity:** O(n log n) due to sorting
57+
- **Space Complexity:** O(1) extra space (sorting in place)
58+
59+
---
60+
61+
## Solution 2: HashMap Approach
62+
63+
### Approach
64+
Use a HashMap to count the frequency of each number, then iterate through the map to find numbers with count >= 2.
65+
66+
### Dry Run
67+
Example Input: nums = [0,1,1,0]
68+
69+
Map after counting: {0:2, 1:2}
70+
71+
Iterate map entries:
72+
- 0 has count 2, add 0
73+
- 1 has count 2, add 1
74+
75+
Answer: [0,1]
76+
77+
### Code
78+
```java
79+
class Solution {
80+
public int[] getSneakyNumbers(int[] nums) {
81+
HashMap<Integer, Integer> map = new HashMap<>();
82+
for (int num : nums) {
83+
map.put(num, map.getOrDefault(num, 0) + 1);
84+
}
85+
int[] ans = new int[2];
86+
int index = 0;
87+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
88+
if (entry.getValue() >= 2) {
89+
ans[index++] = entry.getKey();
90+
}
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### Time and Space Complexity
98+
- **Time Complexity:** O(n)
99+
- **Space Complexity:** O(n) for the HashMap

p/README.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,32 @@
1616

1717
0342: Power of Four - [`0342-power-of-four/`](0342-power-of-four/)
1818

19-
0812: Largest Triangle Area - [`0812-largest-triangle-area/`](0812-largest-triangle-area/)
20-
21-
1323: Maximum 69 Number - [`1323-maximum-69-number/`](1323-maximum-69-number/)
19+
0611: Valid Triangle Number - [`0611-valid-triangle-number/`](0611-valid-triangle-number/)
2220

23-
2264: Largest 3-Same-Digit Number in String - [`2264-largest-3-same-digit-number-in-string/`](2264-largest-3-same-digit-number-in-string/)
21+
0812: Largest Triangle Area - [`0812-largest-triangle-area/`](0812-largest-triangle-area/)
2422

2523
0830: Positions of Large Groups - [`0830-positions-of-large-groups/`](0830-positions-of-large-groups/)
2624

25+
0966: Vowel Spellchecker - [`0966-vowel-spellchecker/`](0966-vowel-spellchecker/)
26+
2727
0976: Largest Perimeter Triangle - [`0976-largest-perimeter-triangle/`](0976-largest-perimeter-triangle/)
2828

2929
1304: Find N Unique Integers Sum up to Zero - [`1304-find-n-unique-integers-sum-up-to-zero/`](1304-find-n-unique-integers-sum-up-to-zero/)
3030

31-
1796: Second Largest Digit in a String - [`1796-second-largest-digit-in-a-string/`](1796-second-largest-digit-in-a-string/)
32-
33-
2016: Maximum Difference Between Increasing Elements - [`2016-maximum-difference-between-increasing-elements/`](2016-maximum-difference-between-increasing-elements/)
34-
35-
2942: Find Words Containing Character - [`2942-find-words-containing-character/`](2942-find-words-containing-character/)
36-
37-
3024: Type of Triangle - [`3024-type-of-triangle/`](3024-type-of-triangle/)
38-
39-
3688: Bitwise OR of Even Numbers in an Array - [`3688-bitwise-or-of-even-numbers-in-an-array/`](3688-bitwise-or-of-even-numbers-in-an-array/)
40-
41-
3697: Compute Decimal Representation - [`3697-compute-decimal-representation/`](3697-compute-decimal-representation/)
42-
43-
3005: Count Elements With Maximum Frequency - [`3005-count-elements-with-maximum-frequency/`](3005-count-elements-with-maximum-frequency/)
44-
45-
3069: Distribute Elements Into Two Arrays I - [`3069-distribute-elements-into-two-arrays-i/`](3069-distribute-elements-into-two-arrays-i/)
46-
47-
0611: Valid Triangle Number - [`0611-valid-triangle-number/`](0611-valid-triangle-number/)
48-
49-
0966: Vowel Spellchecker - [`0966-vowel-spellchecker/`](0966-vowel-spellchecker/)
31+
1323: Maximum 69 Number - [`1323-maximum-69-number/`](1323-maximum-69-number/)
5032

5133
1518: Water Bottles - [`1518-water-bottles/`](1518-water-bottles/)
5234

5335
1535: Find the Winner of an Array Game - [`1535-find-the-winner-of-an-array-game/`](1535-find-the-winner-of-an-array-game/)
5436

5537
1733: Minimum Number of People to Teach - [`1733-minimum-number-of-people-to-teach/`](1733-minimum-number-of-people-to-teach/)
5638

39+
1796: Second Largest Digit in a String - [`1796-second-largest-digit-in-a-string/`](1796-second-largest-digit-in-a-string/)
40+
41+
2016: Maximum Difference Between Increasing Elements - [`2016-maximum-difference-between-increasing-elements/`](2016-maximum-difference-between-increasing-elements/)
42+
43+
2264: Largest 3-Same-Digit Number in String - [`2264-largest-3-same-digit-number-in-string/`](2264-largest-3-same-digit-number-in-string/)
44+
5745
2300: Successful Pairs of Spells and Potions - [`2300-successful-pairs-of-spells-and-potions/`](2300-successful-pairs-of-spells-and-potions/)
5846

5947
2348: Number of Zero Filled Subarrays - [`2348-number-of-zero-filled-subarrays/`](2348-number-of-zero-filled-subarrays/)
@@ -62,14 +50,28 @@
6250

6351
2785: Sort Vowels in a String - [`2785-sort-vowels-in-a-string/`](2785-sort-vowels-in-a-string/)
6452

53+
2942: Find Words Containing Character - [`2942-find-words-containing-character/`](2942-find-words-containing-character/)
54+
55+
3005: Count Elements With Maximum Frequency - [`3005-count-elements-with-maximum-frequency/`](3005-count-elements-with-maximum-frequency/)
56+
6557
3021: Alice and Bob Playing Flower Game - [`3021-alice-and-bob-playing-flower-game/`](3021-alice-and-bob-playing-flower-game/)
6658

59+
3024: Type of Triangle - [`3024-type-of-triangle/`](3024-type-of-triangle/)
60+
61+
3069: Distribute Elements Into Two Arrays I - [`3069-distribute-elements-into-two-arrays-i/`](3069-distribute-elements-into-two-arrays-i/)
62+
6763
3227: Vowels Game in a String - [`3227-vowels-game-in-a-string/`](3227-vowels-game-in-a-string/)
6864

65+
3289: The Two Sneaky Numbers of Digitville - [`3289-the-two-sneaky-numbers-of-digitville/`](3289-the-two-sneaky-numbers-of-digitville/)
66+
6967
3408: Design Task Manager - [`3408-design-task-manager/`](3408-design-task-manager/)
7068

7169
3484: Design Spreadsheet - [`3484-design-spreadsheet/`](3484-design-spreadsheet/)
7270

71+
3688: Bitwise OR of Even Numbers in an Array - [`3688-bitwise-or-of-even-numbers-in-an-array/`](3688-bitwise-or-of-even-numbers-in-an-array/)
72+
7373
3689: Maximum Total Subarray Value I - [`3689-maximum-total-subarray-value-i/`](3689-maximum-total-subarray-value-i/)
7474

75+
3697: Compute Decimal Representation - [`3697-compute-decimal-representation/`](3697-compute-decimal-representation/)
76+
7577
3698: Split Array With Minimum Difference - [`3698-split-array-with-minimum-difference/`](3698-split-array-with-minimum-difference/)

0 commit comments

Comments
 (0)