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

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
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
❌ Compiled
❌ Executed
❌ Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
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.