There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.
You may perform the following move any number of times:
ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
Note that there may be multiple seats or students in the same position at the beginning.
Example 1:
Input: seats = [3,1,5], students = [2,7,4] Output: 4 Explanation: The students are moved as follows: - The first student is moved from from position 2 to position 1 using 1 move. - The second student is moved from from position 7 to position 5 using 2 moves. - The third student is moved from from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used.
Example 2:
Input: seats = [4,1,5,9], students = [1,3,2,6] Output: 7 Explanation: The students are moved as follows: - The first student is not moved. - The second student is moved from from position 3 to position 4 using 1 move. - The third student is moved from from position 2 to position 5 using 3 moves. - The fourth student is moved from from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used.
Example 3:
Input: seats = [2,2,6,6], students = [1,3,2,6] Output: 4 Explanation: Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from from position 1 to position 2 using 1 move. - The second student is moved from from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used.
Constraints:
n == seats.length == students.length1 <= n <= 1001 <= seats[i], students[j] <= 100program main
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: seats = (/3, 1, 5/)
integer, dimension(n) :: students = (/2, 7, 4/)
integer :: i, j, moves
do i = 1, n
do j = 1, n
if (seats(i) == students(j)) then
print *, "Error: Student ", j, " is already in seat ", i
stop 1
end if
end do
end do
moves = solve(seats, students)
print '(A, I0)', 'Minimum number of moves: ', moves
contains
function solve(seats, students) result(moves)
implicit none
integer, intent(in) :: seats(:), students(:)
integer :: moves, i, j, k, temp
moves = 0
do i = 1, size(students)
do j = 1, size(seats)
if (students(i) == seats(j)) then
! Move student i to seat j
temp = students(i)
students(i) = seats(j)
seats(j) = temp
moves = moves + 1
exit
end if
end do
end do
do while (any(students == seats))
do i = 1, size(students)
do j = 1, size(seats)
if (students(i) == seats(j)) then
! Move student i to seat j
temp = students(i)
students(i) = seats(j)
seats(j) = temp
moves = moves + 1
exit
end if
end do
end do
end do
do i = 1, size(students)
do j = 1, size(seats)
if (students(i) == seats(j)) then
print *, "Error: Student ", i, " is still in seat ", j
stop 1
end if
end do
end do
end function solve
end program main
temp.f95:34:20:
34 | students(i) = seats(j)
| 1
Error: Dummy argument ‘students’ with INTENT(IN) in variable definition context (assignment) at (1)
temp.f95:35:20:
35 | seats(j) = temp
| 1
Error: Dummy argument ‘seats’ with INTENT(IN) in variable definition context (assignment) at (1)
temp.f95:48:24:
48 | students(i) = seats(j)
| 1
Error: Dummy argument ‘students’ with INTENT(IN) in variable definition context (assignment) at (1)
temp.f95:49:24:
49 | seats(j) = temp
| 1
Error: Dummy argument ‘seats’ with INTENT(IN) in variable definition context (assignment) at (1)
module min_moves_to_seat
implicit none
contains
integer function min_moves(seats, students) result(min_moves)
integer, intent(in) :: seats(:), students(:)
integer :: i, j, moves, student_pos, seat_pos
min_moves = 0
do i = 1, size(students)
student_pos = students(i)
do j = 1, size(seats)
seat_pos = seats(j)
if (student_pos == seat_pos) then
exit
end if
end do
if (j > size(seats)) then
min_moves = min_moves + abs(student_pos - seat_pos)
end if
end do
end function min_moves
end module min_moves_to_seat
program test_min_moves
use min_moves_to_seat
implicit none
integer, parameter :: n = 3
integer :: seats(n), students(n)
integer :: min_moves
seats = [3, 1, 5]
students = [2, 7, 4]
min_moves = min_moves(seats, students)
write (*,*) "Minimum moves:", min_moves
seats = [4, 1, 5, 9]
students = [1, 3, 2, 6]
min_moves = min_moves(seats, students)
write (*,*) "Minimum moves:", min_moves
seats = [2, 2, 6, 6]
students = [1, 3, 2, 6]
min_moves = min_moves(seats, students)
write (*,*) "Minimum moves:", min_moves
end program test_min_moves
temp.f95:4:63:
4 | integer function min_moves(seats, students) result(min_moves)
| 1
Error: RESULT variable at (1) must be different than function name
temp.f95:5:48:
5 | integer, intent(in) :: seats(:), students(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:6:49:
6 | integer :: i, j, moves, student_pos, seat_pos
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:8:14:
8 | min_moves = 0
| 1
Error: Symbol ‘min_moves’ at (1) has already been host associated
temp.f95:9:28:
9 | do i = 1, size(students)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:10:31:
10 | student_pos = students(i)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:11:27:
11 | do j = 1, size(seats)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:12:27:
12 | seat_pos = seats(j)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:13:41:
13 | if (student_pos == seat_pos) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:14:14:
14 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:15:11:
15 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:16:9:
16 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:17:31:
17 | if (j > size(seats)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:18:18:
18 | min_moves = min_moves + abs(student_pos - seat_pos)
| 1
Error: Symbol ‘min_moves’ at (1) has already been host associated
temp.f95:19:9:
19 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:20:7:
20 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:21:5:
21 | end function min_moves
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:25:7:
25 | use min_moves_to_seat
| 1
Fatal Error: Cannot open module file ‘min_moves_to_seat.mod’ for reading at (1): No such file or directory
compilation terminated.
def count_square_triples(n):
count = 0
for a in range(1, n + 1):
for b in range(a, n + 1):
c = int((a * a + b * b)**0.5)
if c * c == a * a + b * b and c <= n:
count += 1
return count
1. Initialize the count of square triples to 0.
#include <cmath>
int countSquareTriples(int n) {
int count = 0;
for (int a = 1; a <= n; a++) {
for (int b = a; b <= n; b++) {
int c = std::sqrt(a * a + b * b);
if (c * c == a * a + b * b && c <= n) {
count++;
}
}
}
return count;
}
1. Initialize the count of square triples to 0.