LeetCode 2784: Check if Array is Good – Java HashMap Solution Explained

Learn how to solve LeetCode 2784 Check if Array is Good using HashMap and counting techniques in Java. Includes brute force intuition, optimized approach, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
3 views
LinkedInGithubX
0
0
LeetCode 2784: Check if Array is Good – Java HashMap Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 2784 – Check if Array is Good is a beginner-friendly array and hashing problem that tests your understanding of:

  1. Frequency counting
  2. HashMap usage
  3. Array validation
  4. Permutation logic
  5. 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:

  1. Attention to detail
  2. Logical validation
  3. Counting techniques
  4. 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:

base[n] = [1, 2, 3, ..., n-1, n, n]

Meaning:

  1. Numbers from:
1 to n-1

appear exactly once.

  1. Number:
n

appears exactly twice.

You need to return:

true

if the given array is good, otherwise:

false

Understanding the Pattern

A valid good array must follow:

[1, 2, 3, ..., n-1, n, n]

Examples:

[1,1]
[1,2,3,3]
[1,2,3,4,4]

Invalid examples:

[1,2,2]
[1,2,4,4]
[1,1,2,2]

Key Observations

Observation 1

The maximum element determines:

n

Observation 2

Array size must be:

n + 1

because:

1 to n-1 => n-1 elements
n appears twice => 2 elements

Total = n + 1

Observation 3

Frequency conditions:

NumberFrequency
1 to n-1Exactly 1
nExactly 2

Brute Force Approach

Idea

  1. Sort array
  2. Compare with expected array
  3. Return result

Brute Force Algorithm

Step 1

Find maximum element:

n

Step 2

Create expected array:

[1,2,3,...,n,n]

Step 3

Sort both arrays and compare.

Brute Force Complexity

Time Complexity

O(N log N)

due to sorting.

Space Complexity

O(N)

Optimized HashMap Approach

Instead of sorting:

  1. Count frequencies directly
  2. Validate conditions

This makes the solution faster and cleaner.

Intuition Behind HashMap Solution

We store frequency of every number.

Then verify:

  1. Maximum element appears twice
  2. Every other number appears once
  3. Array length equals:
max + 1

Java HashMap Solution

class Solution {

public boolean isGood(int[] nums) {

if(nums.length == 1)
return false;

int maxElement = Integer.MIN_VALUE;

HashMap<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < nums.length; i++) {

map.put(nums[i],
map.getOrDefault(nums[i], 0) + 1);

maxElement = Math.max(maxElement, nums[i]);
}

int n = maxElement;

if(nums.length != n + 1) {
return false;
}

for(int i = 1; i <= n; i++) {

if(!map.containsKey(i)) {
return false;
}

if(i == n) {

if(map.get(i) != 2)
return false;
}
else {

if(map.get(i) != 1)
return false;
}
}

return true;
}
}

Dry Run

Input

nums = [1,3,3,2]

Step 1 – Find Maximum

Maximum element:

3

So:

n = 3

Step 2 – Length Check

Expected length:

n + 1 = 4

Actual length:

4

Valid.

Step 3 – Frequency Count

Frequency map:

NumberCount
11
21
32

Step 4 – Validate Conditions

  1. Numbers 1 and 2 appear once ✅
  2. Number 3 appears twice ✅

Return:

true

Edge Cases

Case 1

[1]

Invalid because:

base[1] = [1,1]

Case 2

[1,1]

Valid.

Case 3

[1,2,2]

Invalid because:

n = 2

Expected:

[1,2,2]

Actually valid.

Case 4

[3,4,4,1,2,1]

Invalid because:

length != max + 1

Optimized Alternative Using Sorting

Another clean solution:

  1. Sort array
  2. Verify:
nums[i] == i + 1

for all except last.

  1. Last two elements should be equal.

Java Sorting Solution

class Solution {

public boolean isGood(int[] nums) {

Arrays.sort(nums);

int n = nums.length - 1;

for(int i = 0; i < n; i++) {

if(nums[i] != i + 1)
return false;
}

return nums[n] == n;
}
}

Time Complexity Analysis

HashMap Solution

Time Complexity

O(N)

Space Complexity

O(N)

Sorting Solution

Time Complexity

O(N log N)

Space Complexity

O(1)

excluding sorting overhead.

HashMap vs Sorting

ApproachTime ComplexitySpace Complexity
HashMapO(N)O(N)
SortingO(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:

  1. Frequency counting
  2. Validation logic
  3. Edge case handling

Common Mistakes

1. Forgetting Length Check

Always verify:

length == max + 1

2. Ignoring Missing Numbers

Array must contain:

1 to n

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:

base[n]

always ends with:

n,n

Q2. Why should array size be n + 1?

Because:

1 to n-1 => n-1 elements
n repeated twice => 2 elements

Total = n+1

Q3. Which approach is better?

HashMap solution is faster.

Sorting solution is simpler.

Q4. Is this problem important for interviews?

Yes.

It tests:

  1. Hashing
  2. Validation logic
  3. Edge case thinking

Related Problems

After mastering this problem, practice:

  1. Contains Duplicate
  2. Find All Duplicates in an Array
  3. Valid Anagram

Conclusion

LeetCode 2784 is a great beginner-friendly hashing problem.

It teaches:

  1. Frequency counting
  2. Validation logic
  3. HashMap usage
  4. 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.

Ai Assistant Kas