An array is considered special if every pair of its adjacent elements contains two numbers with different parity.You are given an array of integers nums. Return true if nums is a special array, otherwise, return false. Example 1:Input: nums = [1]Output: trueExplanation:There is only one element. So the answer is true.Example 2:Input: nums = [2,1,4]Output: trueExplanation:There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.Example 3:Input: nums = [4,3,1,6]Output: falseExplanation:nums[1] and nums[2] are both odd. So the answer is false. Constraints:1 <= nums.length <= 1001 <= nums[i] <= 100
Question
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.You are given an array of integers nums. Return true if nums is a special array, otherwise, return false. Example 1:Input: nums = [1]Output: trueExplanation:There is only one element. So the answer is true.Example 2:Input: nums = [2,1,4]Output: trueExplanation:There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.Example 3:Input: nums = [4,3,1,6]Output: falseExplanation:nums[1] and nums[2] are both odd. So the answer is false. Constraints:1 <= nums.length <= 1001 <= nums[i] <= 100
Solution
Here is a step-by-step solution in Python:
- Define a function
isSpecialthat takes an arraynumsas input. - Iterate over the array from the first element to the second last element. For each element, check if it and the next element have different parity. You can do this by checking if the remainder when the current element and the next element are divided by 2 are different. If they are the same, return
False. - If you have checked all pairs and none of them have the same parity, return
True.
Here is the Python code:
def isSpecial(nums):
for i in range(len(nums) - 1):
if nums[i] % 2 == nums[i + 1] % 2:
return False
return True
You can test this function with the provided examples:
print(isSpecial([1])) # Output: True
print(isSpecial([2, 1, 4])) # Output: True
print(isSpecial([4, 3, 1, 6])) # Output: False
This function works by checking each pair of adjacent elements in the array. If any pair has the same parity (both even or both odd), the function immediately returns False. If it checks all pairs and none of them have the same parity, it returns True.
Similar Questions
Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. Example 1:Input: nums = [1,5,11,5]Output: trueExplanation: The array can be partitioned as [1, 5, 5] and [11].Example 2:Input: nums = [1,2,3,5]Output: falseExplanation: The array cannot be partitioned into equal sum subsets. Constraints:1 <= nums.length <= 2001 <= nums[i] <= 100
You are given an array nums, where each number in the array appears either once or twice.Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice. Example 1:Input: nums = [1,2,1,3]Output: 1Explanation:The only number that appears twice in nums is 1.Example 2:Input: nums = [1,2,3]Output: 0Explanation:No number appears twice in nums.Example 3:Input: nums = [1,2,2,1]Output: 3Explanation:Numbers 1 and 2 appeared twice. 1 XOR 2 == 3. Constraints:1 <= nums.length <= 501 <= nums[i] <= 50Each number in nums appears either once or twice.
You are given an array of positive integers nums.Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob's numbers.Return true if Alice can win this game, otherwise, return false. Example 1:Input: nums = [1,2,3,4,10]Output: falseExplanation:Alice cannot win by choosing either single-digit or double-digit numbers.Example 2:Input: nums = [1,2,3,4,5,14]Output: trueExplanation:Alice can win by choosing single-digit numbers which have a sum equal to 15.Example 3:Input: nums = [5,5,5,25]Output: trueExplanation:Alice can win by choosing double-digit numbers which have a sum equal to 25
You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).Return the total number of good pairs. Example 1:Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1Output: 5Explanation:The 5 good pairs are (0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).Example 2:Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3Output: 2Explanation:The 2 good pairs are (3, 0) and (3, 1). Constraints:1 <= n, m <= 1051 <= nums1[i], nums2[j] <= 1061 <= k <= 103C++ 1class Solution {2public:3 long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {4 5 }6};
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example 1:Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].Example 2:Input: nums = [3,2,4], target = 6Output: [1,2]Example 3:Input: nums = [3,3], target = 6Output: [0,1] Constraints:2 <= nums.length <= 104-109 <= nums[i] <= 109-109 <= target <= 109Only one valid answer exists.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.