commit 321f23b35ece51614268505a7e41d6baffc88a1e
parent 3b6e0155b13343f7535d698236a92bc8ecbd0947
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 14 May 2026 09:01:08 +0300
2784. check if array is good
Diffstat:
2 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/check_arr_good.c b/check_arr_good.c
@@ -0,0 +1,32 @@
+// 2784. Check if Array is Good
+#include "leetcode.h"
+
+/*
+ * 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]'. return
+ * true if the given array is good, otherwise return false. note: a permutation
+ * of integers represents an arrangement of these numbers.
+ */
+
+int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
+
+bool isGood(int *nums, int numsSize) {
+ qsort(nums, numsSize, sizeof(int), cmp);
+ for (int i = 0; i < numsSize - 1; i++)
+ if (nums[i] != i + 1)
+ return false;
+ return nums[numsSize - 1] == numsSize - 1;
+}
+
+int main() {
+ int n1[] = {2, 1, 3}, n2[] = {1, 3, 3, 2}, n3[] = {1, 1};
+ bool r1 = isGood(n1, ARRAY_SIZE(n1));
+ bool r2 = isGood(n2, ARRAY_SIZE(n2));
+ bool r3 = isGood(n3, ARRAY_SIZE(n3));
+ printf("%d\n", r1);
+ assert(r1 == false);
+ printf("%d\n", r2);
+ assert(r2 == true);
+ printf("%d\n", r3);
+ assert(r3 == true);
+}
diff --git a/check_arr_good.py b/check_arr_good.py
@@ -0,0 +1,24 @@
+# 2784. 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]'. return
+true if the given array is good, otherwise return false. note: a permutation
+of integers represents an arrangement of these numbers.
+"""
+
+
+class Solution:
+ def isGood(self, nums: List[int]) -> bool:
+ n = len(nums)
+ nums = sorted(nums)
+ expect = list(range(1, n)) + [n - 1]
+ return expect == nums
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isGood(nums=[2, 1, 3]))
+ print(obj.isGood(nums=[1, 3, 3, 2]))
+ print(obj.isGood(nums=[1, 1]))
+ print(obj.isGood(nums=[3, 4, 4, 1, 2, 1]))