2784. Check if Array is Good LeetCode Solution

In this guide, you will get 2784. Check if Array is Good LeetCode Solution with the best time and space complexity. The solution to Check if Array is Good problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Array is Good solution in C++
  4. Check if Array is Good solution in Java
  5. Check if Array is Good solution in Python
  6. Additional Resources
2784. Check if Array is Good LeetCode Solution image

Problem Statement of Check if Array is Good

You are given an integer array nums. We consider an array good if it is a permutation of an array base[n].
base[n] = [1, 2, …, n – 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n – 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3].
Return true if the given array is good, otherwise return false.
Note: A permutation of integers represents an arrangement of these numbers.

Example 1:

Input: nums = [2, 1, 3]
Output: false
Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.

See also  1518. Water Bottles LeetCode Solution

Example 2:

Input: nums = [1, 3, 3, 2]
Output: true
Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
Example 3:

Input: nums = [1, 1]
Output: true
Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
Example 4:

Input: nums = [3, 4, 4, 1, 2, 1]
Output: false
Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.

Constraints:

1 <= nums.length <= 100
1 <= num[i] <= 200

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2784. Check if Array is Good LeetCode Solution in C++

class Solution {
 public:
  bool isGood(vector<int>& nums) {
    constexpr int kMax = 200;
    const int n = nums.size() - 1;
    vector<int> count(kMax + 1);

    for (const int num : nums)
      ++count[num];

    return all_of(count.begin() + 1, count.begin() + n, [](int c) {
      return c == 1;
    }) && count[n] == 2;
  }
};
/* code provided by PROGIEZ */

2784. Check if Array is Good LeetCode Solution in Java

class Solution {
  public boolean isGood(int[] nums) {
    final int kMax = 200;
    final int n = nums.length - 1;
    int[] count = new int[kMax + 1];

    for (final int num : nums)
      ++count[num];

    return (n == 0 || Arrays.stream(count, 1, n).allMatch(c -> c == 1)) && count[n] == 2;
  }
}
// code provided by PROGIEZ

2784. Check if Array is Good LeetCode Solution in Python

class Solution:
  def isGood(self, nums: list[int]) -> bool:
    n = len(nums) - 1
    count = collections.Counter(nums)
    return all(count[i] == 1 for i in range(1, n)) and count[n] == 2
# code by PROGIEZ

Additional Resources

See also  2389. Longest Subsequence With Limited Sum LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.