Introduction
LeetCode 2784 – Check if Array is Good is a beginner-friendly array and hashing problem that tests your understanding of:
- Frequency counting
- HashMap usage
- Array validation
- Permutation logic
- Edge case handling
Although the problem looks simple initially, many candidates fail because they misunderstand the exact structure of the required array.
This problem is commonly asked to test:
- Attention to detail
- Logical validation
- Counting techniques
- Hashing fundamentals
Problem Link
🔗 https://leetcode.com/problems/check-if-array-is-good/
Problem Statement
An array is considered good if it is a permutation of:
Meaning:
- Numbers from:
appear exactly once.
- Number:
appears exactly twice.
You need to return:
if the given array is good, otherwise:
Understanding the Pattern
A valid good array must follow:
Examples:
Invalid examples:
Key Observations
Observation 1
The maximum element determines:
Observation 2
Array size must be:
because:
Observation 3
Frequency conditions:
| Number | Frequency |
| 1 to n-1 | Exactly 1 |
| n | Exactly 2 |
Brute Force Approach
Idea
- Sort array
- Compare with expected array
- Return result
Brute Force Algorithm
Step 1
Find maximum element:
Step 2
Create expected array:
Step 3
Sort both arrays and compare.
Brute Force Complexity
Time Complexity
due to sorting.
Space Complexity
Optimized HashMap Approach
Instead of sorting:
- Count frequencies directly
- Validate conditions
This makes the solution faster and cleaner.
Intuition Behind HashMap Solution
We store frequency of every number.
Then verify:
- Maximum element appears twice
- Every other number appears once
- Array length equals:
Java HashMap Solution
Dry Run
Input
Step 1 – Find Maximum
Maximum element:
So:
Step 2 – Length Check
Expected length:
Actual length:
Valid.
Step 3 – Frequency Count
Frequency map:
| Number | Count |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
Step 4 – Validate Conditions
- Numbers 1 and 2 appear once ✅
- Number 3 appears twice ✅
Return:
Edge Cases
Case 1
Invalid because:
Case 2
Valid.
Case 3
Invalid because:
Expected:
Actually valid.
Case 4
Invalid because:
Optimized Alternative Using Sorting
Another clean solution:
- Sort array
- Verify:
for all except last.
- Last two elements should be equal.
Java Sorting Solution
Time Complexity Analysis
HashMap Solution
Time Complexity
Space Complexity
Sorting Solution
Time Complexity
Space Complexity
excluding sorting overhead.
HashMap vs Sorting
| Approach | Time Complexity | Space Complexity |
| HashMap | O(N) | O(N) |
| Sorting | O(N log N) | O(1) |
Interview Explanation
In interviews, explain:
A good array must follow the exact pattern [1,2,3,...,n,n]. The maximum element determines n, and frequency counting helps verify whether all required numbers appear correctly.
This demonstrates strong understanding of:
- Frequency counting
- Validation logic
- Edge case handling
Common Mistakes
1. Forgetting Length Check
Always verify:
2. Ignoring Missing Numbers
Array must contain:
completely.
3. Wrong Frequency Validation
Only maximum element should appear twice.
All others must appear once.
FAQs
Q1. Why does maximum element determine n?
Because:
always ends with:
Q2. Why should array size be n + 1?
Because:
Q3. Which approach is better?
HashMap solution is faster.
Sorting solution is simpler.
Q4. Is this problem important for interviews?
Yes.
It tests:
- Hashing
- Validation logic
- Edge case thinking
Related Problems
After mastering this problem, practice:
Conclusion
LeetCode 2784 is a great beginner-friendly hashing problem.
It teaches:
- Frequency counting
- Validation logic
- HashMap usage
- Edge case handling
The key insight is:
A good array must exactly match the structure [1,2,3,...,n,n].
Once you understand this pattern, the problem becomes straightforward and easy to implement.





