1. Concatenation of Array Given an integer array nums of length n , you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 . Essentially, concatenate nums with itself. Python Snippet class Solution: def getConcatenation(self, nums: list[int]) -> list[int]: return nums + nums 2. Score of a String Given a string s , the score of a string is the sum of the absolute differences between the ASCII values of adjacent characters. Python Snippet class Solution: def scoreOfString(self, s: str) -> int: score = 0 for i in range(len(s) - 1): score += abs(ord(s[i]) - ord(s[i+1])) return score 3. Contains Duplicate Given an integer array nums , return true if any value appears at least twice in the array, and return false if every element is distinct. Python Snippet class Solution: def containsDuplicate(self, nums: list[int]) -> bool: seen = set() for num in nums: if num in seen: return True seen.add(num) return False 4. Valid Anagram Given two strings s and t , return true if t is an anagram of s , and false otherwise. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Python Snippet class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False return sorted(s) == sorted(t) # Alternative using hash map (counters) def isAnagram_map(self, s: str, t: str) -> bool: from collections import Counter return Counter(s) == Counter(t) 5. Replace Elements with Greatest Element on Right Side Given an array arr , replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1 . Python Snippet class Solution: def replaceElements(self, arr: list[int]) -> list[int]: max_so_far = -1 for i in range(len(arr) - 1, -1, -1): current_element = arr[i] arr[i] = max_so_far max_so_far = max(max_so_far, current_element) return arr 6. Length of Last Word Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Python Snippet class Solution: def lengthOfLastWord(self, s: str) -> int: s = s.strip() # Remove leading/trailing spaces if not s: return 0 last_space_index = s.rfind(' ') if last_space_index == -1: return len(s) return len(s) - last_space_index - 1 # Simpler approach def lengthOfLastWord_simple(self, s: str) -> int: words = s.split() if not words: return 0 return len(words[-1]) 7. Number of Senior Citizens You are given a list of strings details , where details[i] represents a passenger's details. Each string has a fixed format. The age is represented by characters at indices 11 and 12. Count the number of passengers strictly older than 60. Python Snippet class Solution: def countSeniors(self, details: list[str]) -> int: senior_count = 0 for detail in details: age_str = detail[11:13] age = int(age_str) if age > 60: senior_count += 1 return senior_count 8. Two Sum Given an array of integers nums and an integer target , return indices of the two numbers such that they add up to target . Assume that each input would have exactly one solution, and you may not use the same element twice. Python Snippet class Solution: def twoSum(self, nums: list[int], target: int) -> list[int]: num_map = {} # val -> index for i, num in enumerate(nums): complement = target - num if complement in num_map: return [num_map[complement], i] num_map[num] = i return [] # Should not reach here per problem statement 9. Max Consecutive Ones Given a binary array nums , return the maximum number of consecutive 1 's in the array. Python Snippet class Solution: def findMaxConsecutiveOnes(self, nums: list[int]) -> int: max_ones = 0 current_ones = 0 for num in nums: if num == 1: current_ones += 1 else: max_ones = max(max_ones, current_ones) current_ones = 0 max_ones = max(max_ones, current_ones) # Check after loop for trailing ones return max_ones 10. String Matching in an Array Given an array of string words , return all strings in words that are a substring of another word in the same array (excluding itself). Python Snippet class Solution: def stringMatching(self, words: list[str]) -> list[str]: result = [] for i, w1 in enumerate(words): for j, w2 in enumerate(words): if i != j and w1 in w2: result.append(w1) break # Found a match for w1, move to next w1 return result 11. Pascal's Triangle Given an integer numRows , return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Python Snippet class Solution: def generate(self, numRows: int) -> list[list[int]]: triangle = [] for i in range(numRows): row = [0] * (i + 1) row[0] = 1 row[i] = 1 for j in range(1, i): row[j] = triangle[i-1][j-1] + triangle[i-1][j] triangle.append(row) return triangle 12. Remove Element Given an integer array nums and an integer val , remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Return the number of elements in nums which are not equal to val . Python Snippet class Solution: def removeElement(self, nums: list[int], val: int) -> int: k = 0 # Pointer for where to place non-val elements for i in range(len(nums)): if nums[i] != val: nums[k] = nums[i] k += 1 return k 13. Unique Email Addresses Every valid email consists of a local name and a domain name. If you add periods ('.') in the local name, they are ignored. If you add a plus ('+') in the local name, everything after the first '+' is ignored. Count the number of unique email addresses. Python Snippet class Solution: def numUniqueEmails(self, emails: list[str]) -> int: unique_emails = set() for email in emails: local_name, domain_name = email.split('@') # Handle '+' if '+' in local_name: local_name = local_name.split('+')[0] # Handle '.' local_name = local_name.replace('.', '') unique_emails.add(local_name + '@' + domain_name) return len(unique_emails) 14. Can Place Flowers You have a long flowerbed in which some of the plots are planted, and some are not. Flowers cannot be planted in adjacent plots. Given an integer array flowerbed containing 0 's and 1 's, where 0 means empty and 1 means not empty, and an integer n , return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule, and false otherwise. Python Snippet class Solution: def canPlaceFlowers(self, flowerbed: list[int], n: int) -> bool: count = 0 flowerbed = [0] + flowerbed + [0] # Pad with zeros to handle edges for i in range(1, len(flowerbed) - 1): if flowerbed[i-1] == 0 and flowerbed[i] == 0 and flowerbed[i+1] == 0: flowerbed[i] = 1 # Place a flower count += 1 return count >= n 15. Maximum Difference Between Even and Odd Frequency I Given an array nums , calculate the absolute difference between the frequency of even numbers and the frequency of odd numbers. (Note: This problem name seems custom, assuming a standard interpretation of finding freq difference). Python Snippet class Solution: def maxDifferenceEvenOddFrequency(self, nums: list[int]) -> int: even_freq = 0 odd_freq = 0 for num in nums: if num % 2 == 0: even_freq += 1 else: odd_freq += 1 return abs(even_freq - odd_freq) 16. Longest Strictly Increasing or Strictly Decreasing Subarray Given an integer array nums , return the length of the longest subarray that is either strictly increasing or strictly decreasing. Python Snippet class Solution: def longestMonotonicSubarray(self, nums: list[int]) -> int: if not nums: return 0 max_len = 1 # Check for strictly increasing current_inc_len = 1 for i in range(1, len(nums)): if nums[i] > nums[i-1]: current_inc_len += 1 else: current_inc_len = 1 max_len = max(max_len, current_inc_len) # Check for strictly decreasing current_dec_len = 1 for i in range(1, len(nums)): if nums[i] 17. Maximum Ascending Subarray Sum Given an array of positive integers nums , return the maximum possible sum of an ascending subarray in nums . An array nums is ascending if nums[i] for all i . A subarray is a contiguous subsequence of the array. Python Snippet class Solution: def maxAscendingSum(self, nums: list[int]) -> int: if not nums: return 0 max_sum = nums[0] current_sum = nums[0] for i in range(1, len(nums)): if nums[i] > nums[i-1]: current_sum += nums[i] else: current_sum = nums[i] max_sum = max(max_sum, current_sum) return max_sum 18. Find Pivot Index Given an array of integers nums , calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If no such index exists, return -1 . Python Snippet class Solution: def pivotIndex(self, nums: list[int]) -> int: total_sum = sum(nums) left_sum = 0 for i, num in enumerate(nums): right_sum = total_sum - left_sum - num if left_sum == right_sum: return i left_sum += num return -1 19. Kth Distinct String in an Array A distinct string is a string that appears exactly once in an array. Given an array of strings arr and an integer k , return the $k^{th}$ distinct string in arr . If there are fewer than k distinct strings, return an empty string "" . Python Snippet class Solution: def kthDistinct(self, arr: list[str], k: int) -> str: from collections import Counter counts = Counter(arr) distinct_count = 0 for s in arr: if counts[s] == 1: distinct_count += 1 if distinct_count == k: return s return "" 20. Find All Numbers Disappeared in an Array Given an array nums of n integers where nums[i] is in the range [1, n] , return an array of all the integers in the range [1, n] that do not appear in nums . Python Snippet class Solution: def findDisappearedNumbers(self, nums: list[int]) -> list[int]: # Using a set num_set = set(nums) result = [] for i in range(1, len(nums) + 1): if i not in num_set: result.append(i) return result # In-place modification (more optimal) def findDisappearedNumbers_inplace(self, nums: list[int]) -> list[int]: for num in nums: idx = abs(num) - 1 if nums[idx] > 0: nums[idx] *= -1 result = [] for i, num in enumerate(nums): if num > 0: result.append(i + 1) return result 21. Find Missing and Repeated Values You are given a 0-indexed integer array nums of size n where 1 . Each integer from 1 to n appears exactly once except for one integer that appears twice and one that is missing. Return an integer array of size 2 where ans[0] is the number that appears twice, and ans[1] is the number that is missing. Python Snippet class Solution: def findMissingAndRepeatedValues(self, grid: list[list[int]]) -> list[int]: n = len(grid) flat_nums = [] for row in grid: flat_nums.extend(row) num_set = set() repeated = -1 for num in flat_nums: if num in num_set: repeated = num num_set.add(num) missing = -1 total_expected_sum = (n * n * (n * n + 1)) // 2 actual_sum = sum(flat_nums) # missing = expected_sum - (actual_sum - repeated) missing = repeated + (total_expected_sum - actual_sum) return [repeated, missing] 22. Word Pattern Given a pattern and a string s , find if s follows the same pattern. Here, "follows" means a full match, such that there is a one-to-one correspondence between a letter in pattern and a non-empty word in s . Python Snippet class Solution: def wordPattern(self, pattern: str, s: str) -> bool: words = s.split(' ') if len(pattern) != len(words): return False char_to_word = {} word_to_char = {} for i in range(len(pattern)): char = pattern[i] word = words[i] if char in char_to_word and char_to_word[char] != word: return False if word in word_to_char and word_to_char[word] != char: return False char_to_word[char] = word word_to_char[word] = char return True 23. Design HashSet Design a HashSet without using any built-in hash table libraries. Implement add , remove , and contains methods. Use a simple array of lists (chaining) for collision resolution. Python Snippet class MyHashSet: def __init__(self): self.size = 1000 # Choose a reasonable size for the array self.buckets = [[] for _ in range(self.size)] def _hash(self, key: int) -> int: return key % self.size def add(self, key: int) -> None: hash_key = self._hash(key) if key not in self.buckets[hash_key]: self.buckets[hash_key].append(key) def remove(self, key: int) -> None: hash_key = self._hash(key) if key in self.buckets[hash_key]: self.buckets[hash_key].remove(key) def contains(self, key: int) -> bool: hash_key = self._hash(key) return key in self.buckets[hash_key] # Usage example: # obj = MyHashSet() # obj.add(1) # obj.remove(2) # param_3 = obj.contains(1) 24. Height Checker A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Calculate the minimum number of students that need to move to get all students in non-decreasing order. Python Snippet class Solution: def heightChecker(self, heights: list[int]) -> int: expected = sorted(heights) mismatches = 0 for i in range(len(heights)): if heights[i] != expected[i]: mismatches += 1 return mismatches 25. Find Lucky Integer in an Array Given an array of integers arr , a lucky integer is an integer which has a frequency in the array equal to its value. Return the largest lucky integer in the array. If there is no lucky integer, return -1 . Python Snippet class Solution: def findLucky(self, arr: list[int]) -> int: from collections import Counter counts = Counter(arr) max_lucky = -1 for num, freq in counts.items(): if num == freq: max_lucky = max(max_lucky, num) return max_lucky 26. Special Array I You are given a 0-indexed integer array nums . Return true if nums is a special array, otherwise, return false . An array nums is special if nums[i] and nums[i+1] have different parities for all i from 0 to nums.length - 2 . Python Snippet class Solution: def isArraySpecial(self, nums: list[int]) -> bool: if len(nums) 27. Minimum Changes To Make Alternating Binary String You are given a string s consisting only of the characters '0' and '1' . In one operation, you can change any '0' to '1' or vice versa. Return the minimum number of operations needed to make s alternating. Python Snippet class Solution: def minOperations(self, s: str) -> int: n = len(s) # Case 1: Starts with '0' (e.g., "010101...") changes1 = 0 for i in range(n): if i % 2 == 0: # Should be '0' if s[i] == '1': changes1 += 1 else: # Should be '1' if s[i] == '0': changes1 += 1 # Case 2: Starts with '1' (e.g., "101010...") changes2 = 0 for i in range(n): if i % 2 == 0: # Should be '1' if s[i] == '0': changes2 += 1 else: # Should be '0' if s[i] == '1': changes2 += 1 return min(changes1, changes2) 28. Path Crossing Given a string path , where path[i] = 'N' , 'S' , 'E' or 'W' , each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane. Return true if the path crosses itself, or false otherwise. Python Snippet class Solution: def isPathCrossing(self, path: str) -> bool: visited = set() visited.add((0, 0)) # Start at origin x, y = 0, 0 for move in path: if move == 'N': y += 1 elif move == 'S': y -= 1 elif move == 'E': x += 1 elif move == 'W': x -= -1 current_pos = (x, y) if current_pos in visited: return True visited.add(current_pos) return False 29. Maximum Score After Splitting a String Given a binary string s , split it into two non-empty substrings left and right (at any valid index). The score of a split is the number of zeros in left plus the number of ones in right . Return the maximum score you can get. Python Snippet class Solution: def maxScore(self, s: str) -> int: max_score = 0 total_ones = s.count('1') zeros_left = 0 ones_right = total_ones for i in range(len(s) - 1): # Split can be from index 0 to len(s)-2 if s[i] == '0': zeros_left += 1 else: ones_right -= 1 max_score = max(max_score, zeros_left + ones_right) return max_score 30. Circular Sentence A sentence is a list of words that are separated by a single space with no leading or trailing spaces. A sentence is circular if the last character of a word is equal to the first character of the next word. Also, the last character of the last word must be equal to the first character of the first word. Return true if the sentence is circular, or false otherwise. Python Snippet class Solution: def isCircularSentence(self, sentence: str) -> bool: words = sentence.split(' ') n = len(words) for i in range(n): current_word = words[i] next_word = words[(i + 1) % n] # Use modulo for circular check if current_word[-1] != next_word[0]: return False return True 31. Maximum Product Difference Between Two Pairs Given an integer array nums of size n , find four distinct indices a, b, c, d such that nums[a] * nums[b] - nums[c] * nums[d] has the maximum value. Return this maximum value. Python Snippet class Solution: def maxProductDifference(self, nums: list[int]) -> int: nums.sort() # To maximize (a*b - c*d), we want a*b to be as large as possible # and c*d to be as small as possible. # This implies a and b are the two largest numbers, # and c and d are the two smallest numbers. # Largest two: nums[-1], nums[-2] # Smallest two: nums[0], nums[1] return (nums[-1] * nums[-2]) - (nums[0] * nums[1]) 32. Destination City You are given the array paths , where paths[i] = [cityA_i, cityB_i] means there exists a direct path from cityA_i to cityB_i . Return the destination city, that is, the city without any path outgoing from it. Python Snippet class Solution: def destCity(self, paths: list[list[str]]) -> str: # Cities with outgoing paths outgoing_cities = set() # All destination cities all_destinations = set() for path in paths: outgoing_cities.add(path[0]) all_destinations.add(path[1]) # The destination city is in all_destinations but not in outgoing_cities for city in all_destinations: if city not in outgoing_cities: return city return "" # Should always find one per problem constraints 33. Largest 3-Same-Digit Number in String You are given a string num representing a large integer. An integer is good if it consists of three identical digits in a row. Return the maximum good integer as a string, or an empty string "" if no such integer exists. Note: The good integer with the largest value is the one which is the largest numerically. Python Snippet class Solution: def largestGoodInteger(self, num: str) -> str: max_good = "" for i in range(len(num) - 2): if num[i] == num[i+1] and num[i+1] == num[i+2]: current_good = num[i:i+3] if not max_good or current_good > max_good: max_good = current_good return max_good 34. Count the Number of Consistent Strings You are given a string allowed consisting of distinct characters and an array of strings words . A string is consistent if all characters in the string appear in the string allowed . Return the number of consistent strings in the array words . Python Snippet class Solution: def countConsistentStrings(self, allowed: str, words: list[str]) -> int: allowed_set = set(allowed) count = 0 for word in words: is_consistent = True for char in word: if char not in allowed_set: is_consistent = False break if is_consistent: count += 1 return count 35. Find Words That Can Be Formed by Characters You are given an array of strings words and a string chars . A string is good if it can be formed by characters from chars (each character can only be used once). Return the sum of lengths of all good strings in words . Python Snippet class Solution: def countCharacters(self, words: list[str], chars: str) -> int: from collections import Counter char_counts = Counter(chars) total_length = 0 for word in words: word_counts = Counter(word) is_good = True for char, count in word_counts.items(): if char not in char_counts or char_counts[char] 36. Pascal's Triangle II Given an integer rowIndex , return the rowIndex th (0-indexed) row of the Pascal's triangle. Python Snippet class Solution: def getRow(self, rowIndex: int) -> list[int]: row = [1] * (rowIndex + 1) for i in range(2, rowIndex + 1): for j in range(i - 1, 0, -1): row[j] = row[j] + row[j-1] return row 37. Divide Array Into Equal Pairs You are given an integer array nums of size 2n . You need to divide nums into n pairs such that: Each element belongs to exactly one pair. The elements in each pair are equal. Return true if you can achieve this, and false otherwise. Python Snippet class Solution: def divideArray(self, nums: list[int]) -> bool: from collections import Counter counts = Counter(nums) for num, freq in counts.items(): if freq % 2 != 0: return False return True 38. Monotonic Array An array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i , nums[i] . An array nums is monotone decreasing if for all i , nums[i] >= nums[j] . Return true if the given array nums is monotonic, or false otherwise. Python Snippet class Solution: def isMonotonic(self, nums: list[int]) -> bool: if len(nums) nums[i+1]: is_increasing = False if nums[i] 39. Check If Array Is Sorted and Rotated Given an array nums , return true if the array was originally sorted in non-decreasing order and then rotated some number of positions (including zero). Python Snippet class Solution: def check(self, nums: list[int]) -> bool: violations = 0 n = len(nums) for i in range(n): if nums[i] > nums[(i + 1) % n]: violations += 1 return violations 40. Sign of the Product of an Array There is a function signFunc(x) that returns: 1 if x is positive, -1 if x is negative, and 0 if x is equal to 0 . You are given an integer array nums . Return signFunc(product) . Python Snippet class Solution: def arraySign(self, nums: list[int]) -> int: negative_count = 0 for num in nums: if num == 0: return 0 if num 41. Find the Index of the First Occurrence in a String Given two strings haystack and needle , return the index of the first occurrence of needle in haystack , or -1 if needle is not part of haystack . Python Snippet class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) # Manual implementation (similar to KMP's idea but brute force) def strStr_manual(self, haystack: str, needle: str) -> int: n = len(haystack) m = len(needle) if m == 0: return 0 if m > n: return -1 for i in range(n - m + 1): if haystack[i:i+m] == needle: return i return -1 42. Redistribute Characters to Make All Strings Equal You are given an array of strings words . In one operation, you can choose two distinct indices i and j , choose a character c present at words[i] , and move it to words[j] . Return true if you can make every string in words equal, and false otherwise. Python Snippet class Solution: def makeEqual(self, words: list[str]) -> bool: from collections import Counter # Concatenate all words into a single string to count overall character frequencies all_chars = "".join(words) char_counts = Counter(all_chars) n = len(words) # For all strings to be equal, each character must be divisible by n for char, count in char_counts.items(): if count % n != 0: return False return True 43. Longest Palindrome Given a string s which consists of lowercase or uppercase Latin letters, return the length of the longest palindrome that can be built with those letters. (Case sensitive, e.g., "Aa" is not a palindrome). Python Snippet class Solution: def longestPalindrome(self, s: str) -> int: from collections import Counter counts = Counter(s) length = 0 has_odd_freq = False for char, freq in counts.items(): if freq % 2 == 0: length += freq else: length += freq - 1 # Use all but one character has_odd_freq = True if has_odd_freq: length += 1 # Add one character to the center of the palindrome return length 44. Largest Substring Between Two Equal Characters Given a string s , return the length of the longest substring between two equal characters, excluding the two characters themselves. If there is no such substring, return -1 . Python Snippet class Solution: def maxLengthBetweenEqualCharacters(self, s: str) -> int: first_occurrence = {} max_length = -1 for i, char in enumerate(s): if char in first_occurrence: # Current index - first occurrence index - 1 (to exclude the two chars) max_length = max(max_length, i - first_occurrence[char] - 1) else: first_occurrence[char] = i return max_length 45. Set Mismatch You have a set of integers s originally containing all the numbers from 1 to n . Unfortunately, due to an error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number. Find the number that occurs twice and the number that is missing. Python Snippet class Solution: def findErrorNums(self, nums: list[int]) -> list[int]: n = len(nums) # Using a set to find the duplicate seen = set() duplicate = -1 for num in nums: if num in seen: duplicate = num break seen.add(num) # Using sum to find the missing number actual_sum = sum(nums) expected_sum = n * (n + 1) // 2 # missing = expected_sum - (actual_sum - duplicate) missing = expected_sum - actual_sum + duplicate return [duplicate, missing] # In-place modification (similar to Find All Numbers Disappeared) def findErrorNums_inplace(self, nums: list[int]) -> list[int]: n = len(nums) duplicate = -1 # Mark visited numbers by negating the value at their index for num in nums: idx = abs(num) - 1 if nums[idx] 0: missing = i + 1 break return [duplicate, missing] 46. First Unique Character in a String Given a string s , find the first non-repeating character in it and return its index. If it does not exist, return -1 . Python Snippet class Solution: def firstUniqChar(self, s: str) -> int: from collections import Counter counts = Counter(s) for i, char in enumerate(s): if counts[char] == 1: return i return -1 47. Intersection of Two Arrays Given two integer arrays nums1 and nums2 , return an array of their intersection. Each element in the result must be unique, and you may return the result in any order. Python Snippet class Solution: def intersection(self, nums1: list[int], nums2: list[int]) -> list[int]: set1 = set(nums1) set2 = set(nums2) return list(set1.intersection(set2)) 48. Find Common Characters Given a string array words , return an array of all characters that show up in all strings within the words list (including duplicates). For example, if a character appears 3 times in all strings, and 2 times in one, then it should be included 2 times in the final answer. Python Snippet class Solution: def commonChars(self, words: list[str]) -> list[str]: from collections import Counter if not words: return [] # Start with the character counts of the first word common_counts = Counter(words[0]) # Iterate through the rest of the words for i in range(1, len(words)): current_word_counts = Counter(words[i]) # Intersect current common_counts with current_word_counts for char in common_counts: common_counts[char] = min(common_counts[char], current_word_counts[char]) result = [] for char, count in common_counts.items(): result.extend([char] * count) # Add char 'count' times return result 49. Number of Students Unable to Eat Lunch The school cafeteria offers circular and square sandwiches. Students are in a queue, and sandwiches are in a stack. Students take sandwiches if it matches their preference; otherwise, they go to the end of the queue. If a student cannot eat, they remain at the front. Return the number of students who are unable to eat. Python Snippet class Solution: def countStudents(self, students: list[int], sandwiches: list[int]) -> int: from collections import Counter student_counts = Counter(students) # Count preferences: 0s and 1s for sandwich in sandwiches: if student_counts[sandwich] > 0: # If there's a student who wants this sandwich student_counts[sandwich] -= 1 else: # No student wants this sandwich, so no more students can eat break # Sum of remaining students return student_counts[0] + student_counts[1] 50. Special Array With X Elements Greater Than or Equal X You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x . Return x if the array is special, otherwise, return -1 . Python Snippet class Solution: def specialArray(self, nums: list[int]) -> int: n = len(nums) # Iterate potential x values from 0 to n for x in range(n + 1): count = 0 for num in nums: if num >= x: count += 1 if count == x: return x return -1