Sort Array By Parity

🏠 ⬅️ ➡️

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

Example 1:

Input: nums = [3,1,2,4] Output: [2,4,3,1] Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Example 2:

Input: nums = [0] Output: [0]

Constraints:

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 5000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 4
    integer, dimension(n) :: nums
    integer, dimension(n) :: result

    nums = [3, 1, 2, 4]
    write (*, '(*(g0))') 'Example 1:'
    write (*, '(*(g0))') 'Input:', nums
    call move_even_odd(nums, result)
    write (*, '(*(g0))') 'Output:', result
    write (*, *)

    nums = [0]
    write (*, '(*(g0))') 'Example 2:'
    write (*, '(*(g0))') 'Input:', nums
    call move_even_odd(nums, result)
    write (*, '(*(g0))') 'Output:', result
    write (*, *)

contains

    subroutine move_even_odd(nums, result)
        implicit none
        integer, intent(in) :: nums(:)
        integer, intent(out) :: result(:)

        integer :: i, j

        do i = 1, size(nums)
            if (mod(nums(i), 2) == 0) then
                result(j) = nums(i)
                j = j + 1
            end if
        end do

        do i = 1, size(nums)
            if (mod(nums(i), 2) /= 0) then
                result(j) = nums(i)
                j = j + 1
            end if
        end do
    end subroutine move_even_odd
end program main
Compiled
Executed
Correct
program even_odd
      implicit none

      integer, parameter :: int32 = selected_int_kind(9)

      integer :: i, n, nums(5000), temp(5000)

      ! Read the number of elements in the array
      read(*,*) n

      ! Read the elements of the array
      do i = 1, n
        read(*,*) nums(i)
      end do

      ! Initialize the temporary array
      temp = 0

      ! Move the even integers to the beginning of the array
      do i = 1, n
        if (mod(nums(i), 2) == 0) then
          temp(i) = nums(i)
        end if
      end do

      ! Move the odd integers to the end of the array
      do i = 1, n
        if (mod(nums(i), 2) /= 0) then
          temp(i + n/2) = nums(i)
        end if
      end do

      ! Output the sorted array
      do i = 1, n
        write(*,*) temp(i)
      end do

      end program even_odd

This script uses the `selected_int_kind` function to specify the kind of integer to use, which is 32 bits in this case. It then declares two arrays, `nums` and `temp`, to store the input and output values respectively. The `n` variable is used to store the number of elements in the array.

The script reads the number of elements in the array and the elements of the array from stdin, and then initializes the `temp` array with 0s. It then loops through the `nums` array and moves the even integers to the beginning of the `temp` array, and the odd integers to the end of the `temp` array. Finally, it outputs the sorted array to stdout.

Note that this script assumes that the input array is sorted in ascending order. If the input array is not sorted, you will need to sort it before moving the even and odd integers to their respective positions in the output array.
🌐 Data from online sources
def lenLongestFibSubseq(arr):
    index = {x: i for i, x in enumerate(arr)}

    longest = dict()
    ans = 0

    for k, z in enumerate(arr):
        for j, y in enumerate(arr[:k]):
            i = index.get(z - y, None)
            if i is not None and i < j:
                longest[j, k] = longest.get((i, j), 1) + 1
                ans = max(ans, longest[j, k] + 1)

    return ans if ans >= 3 else 0

The algorithm first creates an index map/dictionary to store the element and its corresponding index in the given array arr. Then, it initializes an empty map/dictionary for the longest Fibonacci-like subsequence and initializes an answer variable to store the length of the longest subsequence.

The algorithm then iterates through the array arr using two nested loops. In each iteration, it tries to find whether a Fibonacci-like subsequence can be formed from the given elements. If such a subsequence is found, it updates the length of the longest Fibonacci-like subsequence found so far.

The algorithm checks if the answer is greater than or equal to 3 (minimum length for a Fibonacci-like sequence), then it returns the answer; otherwise, it returns 0, indicating that no Fibonacci-like subsequence was found.

🌐 Data from online sources
#include <unordered_map>
#include <vector>

int lenLongestFibSubseq(std::vector<int>& arr) {
    int n = arr.size();
    std::unordered_map<int, int> index;
    for (int i = 0; i < n; ++i)
        index[arr[i]] = i;

    std::unordered_map<int, int> longest;
    int ans = 0;

    for (int k = 0; k < n; ++k)
        for (int j = 0; j < k; ++j) {
            int i = index.find(arr[k] - arr[j]) == index.end() ? -1 : index[arr[k] - arr[j]];
            if (i >= 0 && i < j) {
                longest[j * n + k] = longest.find(i * n + j) == longest.end() ? 2 : longest[i * n + j] + 1;
                ans = std::max(ans, longest[j * n + k] + 1);
            }
        }

    return ans >= 3 ? ans : 0;
}

The algorithm first creates an index map/dictionary to store the element and its corresponding index in the given array arr. Then, it initializes an empty map/dictionary for the longest Fibonacci-like subsequence and initializes an answer variable to store the length of the longest subsequence.

The algorithm then iterates through the array arr using two nested loops. In each iteration, it tries to find whether a Fibonacci-like subsequence can be formed from the given elements. If such a subsequence is found, it updates the length of the longest Fibonacci-like subsequence found so far.

The algorithm checks if the answer is greater than or equal to 3 (minimum length for a Fibonacci-like sequence), then it returns the answer; otherwise, it returns 0, indicating that no Fibonacci-like subsequence was found.