You are given an integer n
and an integer start
.
Define an array nums
where nums[i] = start + 2 * i
(0-indexed) and n == nums.length
.
Return the bitwise XOR of all elements of nums
.
Example 1:
Input: n = 5, start = 0 Output: 8 Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8. Where "^ " corresponds to bitwise XOR operator.
Example 2:
Input: n = 4, start = 3 Output: 8 Explanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
Constraints:
1 <= n <= 1000
0 <= start <= 1000
n == nums.length
program main
implicit none
integer :: n, start, i
integer, dimension(n) :: nums
do i = 1, n
nums(i) = start + 2 * (i - 1)
end do
print '(I0)', sum(nums)
end program main
temp.f95:5:33: 5 | integer, dimension(n) :: nums | 1 Error: Explicit shaped array with nonconstant bounds at (1) temp.f95:8:8: 8 | nums(i) = start + 2 * (i - 1) | 1 Error: Function βnumsβ at (1) has no IMPLICIT type temp.f95:8:12: 8 | nums(i) = start + 2 * (i - 1) | 1 Error: Symbol βnumsβ at (1) has no IMPLICIT type
program xor_operation
implicit none
integer :: n, start, i, nums(n)
read(*,*) n
read(*,*) start
do i = 1, n
nums(i) = start + 2 * i
end do
do i = 1, n - 1
nums(i) = nums(i) ^ nums(i + 1)
end do
write(*,*) nums(1)
end program xor_operation
temp.f95:4:37: 4 | integer :: n, start, i, nums(n) | 1 Error: Explicit shaped array with nonconstant bounds at (1) temp.f95:14:13: 14 | nums(i) = nums(i) ^ nums(i + 1) | 1 Error: βnumsβ at (1) is not a variable temp.f95:9:10: 9 | do i = 1, n | 1 Error: Symbol βiβ at (1) has no IMPLICIT type temp.f95:6:17: 6 | read(*,*) n | 1 Error: Symbol βnβ at (1) has no IMPLICIT type temp.f95:7:21: 7 | read(*,*) start | 1 Error: Symbol βstartβ at (1) has no IMPLICIT type temp.f95:17:16: 17 | write(*,*) nums(1) | 1 Error: Function βnumsβ at (1) has no IMPLICIT type
def findTheDistanceValue(arr1, arr2, d):
count = 0
for i in arr1:
valid = True
for j in arr2:
if abs(i - j) <= d:
valid = False
break
if valid:
count += 1
return count
The algorithm starts by initializing a variable count
to store the number of elements obeying the given constraints. For every element arr1[i]
, iterate through every element arr2[j]
. Check if |arr1[i] - arr2[j]| <= d
. If this condition is true, the current element arr1[i]
is not valid, so set valid
to false and break out of the inner loop. If the entire inner loop is exhausted and all elements arr2[j]
did not meet this condition, the current arr1[i]
is valid and increments the count. Return the count at the end.
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
int count = 0;
for (int i = 0; i < arr1.size(); i++) {
bool valid = true;
for (int j = 0; j < arr2.size(); j++) {
if (abs(arr1[i] - arr2[j]) <= d) {
valid = false;
break;
}
}
if (valid) count++;
}
return count;
}
The algorithm starts by initializing a variable count
to store the number of elements obeying the given constraints. For every element arr1[i]
, iterate through every element arr2[j]
. Check if |arr1[i] - arr2[j]| <= d
. If this condition is true, the current element arr1[i]
is not valid, so set valid
to false and break out of the inner loop. If the entire inner loop is exhausted and all elements arr2[j]
did not meet this condition, the current arr1[i]
is valid and increments the count. Return the count at the end.