Given an array of integers nums
, half of the integers in nums
are odd, and the other half are even.
Sort the array so that whenever nums[i]
is odd, i
is odd, and whenever nums[i]
is even, i
is even.
Return any answer array that satisfies this condition.
Example 1:
Input: nums = [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Example 2:
Input: nums = [2,3] Output: [2,3]
Constraints:
2 <= nums.length <= 2 * 104
nums.length
is even.nums
are even.0 <= nums[i] <= 1000
Follow Up: Could you solve it in-place?
program main
implicit none
integer :: i, n, nums(4)
! Examples
n = 4
nums = (/ 4, 2, 5, 7 /)
call sort_array(n, nums)
write (*, '(A, I0, A, 4I0)') 'Example 1: ', n, ' -> ', nums
n = 2
nums = (/ 2, 3 /)
call sort_array(n, nums)
write (*, '(A, I0, A, 2I0)') 'Example 2: ', n, ' -> ', nums
contains
subroutine sort_array(n, nums)
implicit none
integer, intent(inout) :: n
integer, intent(inout) :: nums(:)
integer :: i, j, temp
do i = 1, n - 1
do j = i + 1, n
if (mod(nums(i), 2) == 0 .neqv. mod(j, 2) == 0) then
temp = nums(i)
nums(i) = nums(j)
nums(j) = temp
end if
end do
end do
end subroutine sort_array
end program main
temp.f95:13:4: 13 | nums = (/ 2, 3 /) | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 2)
module sort_array
implicit none
private
public :: sort_array
contains
subroutine sort_array(nums)
implicit none
integer, intent(inout) :: nums(:)
integer :: i, j, temp
! Loop through the array and swap odd and even elements
do i = 1, size(nums) - 1
if (mod(nums(i), 2) == 0) then
do j = i + 1, size(nums)
if (mod(nums(j), 2) /= 0) then
temp = nums(i)
nums(i) = nums(j)
nums(j) = temp
exit
end if
end do
end if
end do
end subroutine sort_array
end module sort_array
program test_sort_array
use sort_array
implicit none
integer, parameter :: nums(4) = [4, 2, 5, 7]
integer :: i
call sort_array(nums)
do i = 1, size(nums)
write (*,*) nums(i)
end do
end program test_sort_array
temp.f95:4:24: 4 | public :: sort_array | 1 Error: PUBLIC attribute applied to MODULE sort_array at (1) temp.f95:6:25: 6 | subroutine sort_array(nums) | 1 Error: MODULE attribute of โsort_arrayโ conflicts with PROCEDURE attribute at (1) temp.f95:7:21: 7 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:8:41: 8 | integer, intent(inout) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:29: 9 | integer :: i, j, temp | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:32: 12 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:42: 13 | if (mod(nums(i), 2) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:14:40: 14 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:50: 15 | if (mod(nums(j), 2) /= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:38: 16 | temp = nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:41: 17 | nums(i) = nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:38: 18 | nums(j) = temp | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:28: 19 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:20:23: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:19: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:15: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end subroutine sort_array | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use sort_array | 1 Fatal Error: Cannot open module file โsort_array.modโ for reading at (1): No such file or directory compilation terminated.
from collections import defaultdict
def possible_bipartition(n, dislikes):
def dfs(person, group):
if person in groups:
return groups[person] == group
groups[person] = group
for other in dislike_list[person]:
if not dfs(other, -group):
return False
return True
groups = {}
dislike_list = defaultdict(list)
for p1, p2 in dislikes:
dislike_list[p1].append(p2)
dislike_list[p2].append(p1)
for person in range(1, n + 1):
if person not in groups and not dfs(person, 1):
return False
return True
The problem can be modeled as a graph where each person is a node and dislikes are edges between nodes. We need to color the graph using two colors (or groups) such that if there is an edge between the nodes, they must have different colors. This is possible if and only if the graph is bipartite.
We can use Depth First Search (DFS) to check if the graph is bipartite. Start with an uncolored node, color it with group 1, and move to its neighbors. Each neighbor gets the opposite color (group) of the current node. If we find a node that has already been colored and it has the same color as the current node, then the graph is not bipartite, and we return false. If all nodes are successfully colored, the graph is bipartite, and we return true. The time complexity is O(n + e), where n is the number of nodes (people) and e is the number of edges (dislikes).
#include <vector>
using namespace std;
bool dfs(int person, int group, vector<int> &groups, vector<vector<int>> &dislikeList) {
if (groups[person] != 0) {
return groups[person] == group;
}
groups[person] = group;
for (int other : dislikeList[person]) {
if (!dfs(other, -group, groups, dislikeList)) {
return false;
}
}
return true;
}
bool possible_bipartition(int n, vector<vector<int>> &dislikes) {
vector<int> groups(n + 1, 0);
vector<vector<int>> dislikeList(n + 1);
for (auto &dislikePair : dislikes) {
dislikeList[dislikePair[0]].push_back(dislikePair[1]);
dislikeList[dislikePair[1]].push_back(dislikePair[0]);
}
for (int person = 1; person <= n; person++) {
if (groups[person] == 0 && !dfs(person, 1, groups, dislikeList)) {
return false;
}
}
return true;
}
The problem can be modeled as a graph where each person is a node and dislikes are edges between nodes. We need to color the graph using two colors (or groups) such that if there is an edge between the nodes, they must have different colors. This is possible if and only if the graph is bipartite.
We can use Depth First Search (DFS) to check if the graph is bipartite. Start with an uncolored node, color it with group 1, and move to its neighbors. Each neighbor gets the opposite color (group) of the current node. If we find a node that has already been colored and it has the same color as the current node, then the graph is not bipartite, and we return false. If all nodes are successfully colored, the graph is bipartite, and we return true. The time complexity is O(n + e), where n is the number of nodes (people) and e is the number of edges (dislikes).