Alice and Bob have a different total number of candies. You are given two integer arrays aliceSizes
and bobSizes
where aliceSizes[i]
is the number of candies of the ith
box of candy that Alice has and bobSizes[j]
is the number of candies of the jth
box of candy that Bob has.
Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.
Return a_n integer array_ answer
where answer[0]
is the number of candies in the box that Alice must exchange, and answer[1]
is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists.
Example 1:
Input: aliceSizes = [1,1], bobSizes = [2,2] Output: [1,2]
Example 2:
Input: aliceSizes = [1,2], bobSizes = [2,3] Output: [1,2]
Example 3:
Input: aliceSizes = [2], bobSizes = [1,3] Output: [2,3]
Constraints:
1 <= aliceSizes.length, bobSizes.length <= 104
1 <= aliceSizes[i], bobSizes[j] <= 105
program main
implicit none
integer, parameter :: n = 2
integer, dimension(n) :: aliceSizes = [1, 1]
integer, dimension(n) :: bobSizes = [2, 2]
integer, dimension(2) :: answer
call solve(aliceSizes, bobSizes, answer)
write (*, '(A, I0, A, I0)') 'Answer: ', answer(1), ',', answer(2)
contains
subroutine solve(aliceSizes, bobSizes, answer)
implicit none
integer, intent(in) :: aliceSizes(:), bobSizes(:)
integer, intent(out) :: answer(:)
integer :: aliceTotal, bobTotal, diff
aliceTotal = sum(aliceSizes)
bobTotal = sum(bobSizes)
diff = abs(aliceTotal - bobTotal)
if (diff == 0) then
answer = [0, 0]
return
end if
if (aliceTotal > bobTotal) then
answer = [diff, 0]
else
answer = [0, diff]
end if
end subroutine solve
end program main
Answer: 0,2
module exchange_candies
implicit none
contains
function exchange_candies(alice_sizes, bob_sizes) result(answer)
integer, intent(in) :: alice_sizes(:), bob_sizes(:)
integer :: answer(2)
! Local variables
integer :: alice_total, bob_total, diff, exchange
! Calculate the total number of candies for Alice and Bob
alice_total = sum(alice_sizes)
bob_total = sum(bob_sizes)
! Calculate the difference between the total number of candies for Alice and Bob
diff = abs(alice_total - bob_total)
! Check if the difference is odd
if (mod(diff, 2) == 1) then
! If the difference is odd, one person must exchange an odd number of candies
exchange = diff / 2 + 1
else
! If the difference is even, one person must exchange an even number of candies
exchange = diff / 2
endif
! Calculate the number of candies that must be exchanged by Alice and Bob
answer(1) = exchange
answer(2) = exchange
! Check if the total number of candies for Alice is greater than the total number of candies for Bob
if (alice_total > bob_total) then
! If the total number of candies for Alice is greater than the total number of candies for Bob, Alice must exchange more candies
answer(1) = answer(1) + 1
else
! If the total number of candies for Bob is greater than the total number of candies for Alice, Bob must exchange more candies
answer(2) = answer(2) + 1
endif
end function exchange_candies
end module exchange_candies
program test_exchange_candies
use exchange_candies
implicit none
! Test case 1: Alice and Bob have the same total number of candies
integer, parameter :: alice_sizes1(1) = [1]
integer, parameter :: bob_sizes1(1) = [1]
integer :: answer1(2)
! Test case 2: Alice and Bob have different total numbers of candies
integer, parameter :: alice_sizes2(2) = [1, 2]
integer, parameter :: bob_sizes2(2) = [2, 3]
integer :: answer2(2)
! Test case 3: Alice has more candies than Bob
integer, parameter :: alice_sizes3(1) = [2]
integer, parameter :: bob_sizes3(2) = [1, 3]
integer :: answer3(2)
! Test case 4: Bob has more candies than Alice
integer, parameter :: alice_sizes4(2) = [1, 2]
integer, parameter :: bob_sizes4(1) = [2]
integer :: answer4(2)
! Test case 5: Alice and Bob have the same total number of candies
integer, parameter :: alice_sizes5(1) = [1]
integer, parameter :: bob_sizes5(1) = [1]
integer :: answer5(2)
! Test case 6: Alice and Bob have different total numbers of candies
integer, parameter :: alice_sizes6(2) = [1, 2]
integer, parameter :: bob_sizes6(2) = [2, 3]
integer :: answer6(2)
! Test case 7: Alice has more candies than Bob
integer, parameter :: alice_sizes7(1) = [2]
integer, parameter :: bob_sizes7(2) = [1, 3]
integer :: answer7(2)
! Test case 8: Bob has more candies than Alice
integer, parameter :: alice_sizes8(2) = [1, 2]
integer, parameter :: bob_sizes8(1) = [2]
integer :: answer8(2)
! Test case 9: Alice and Bob have the same total number of candies
integer, parameter :: alice_sizes9(1) = [1]
integer, parameter :: bob_sizes9(1) = [1]
integer ::
temp.f95:7:25: 7 | function exchange_candies(alice_sizes, bob_sizes) result(answer) | 1 Error: MODULE attribute of ‘exchange_candies’ conflicts with PROCEDURE attribute at (1) temp.f95:9:51: 9 | integer, intent(in) :: alice_sizes(:), bob_sizes(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | integer :: answer(2) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:49: 13 | integer :: alice_total, bob_total, diff, exchange | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:16:30: 16 | alice_total = sum(alice_sizes) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:26: 17 | bob_total = sum(bob_sizes) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:35: 20 | diff = abs(alice_total - bob_total) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:27: 23 | if (mod(diff, 2) == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:27: 25 | exchange = diff / 2 + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:4: 26 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:28:23: 28 | exchange = diff / 2 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:3: 29 | endif | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:20: 32 | answer(1) = exchange | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:20: 33 | answer(2) = exchange | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:36:33: 36 | if (alice_total > bob_total) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:38:29: 38 | answer(1) = answer(1) + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:39:4: 39 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:41:29: 41 | answer(2) = answer(2) + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:42:3: 42 | endif | 1 Error: Expecting END MODULE statement at (1) temp.f95:44:3: 44 | end function exchange_candies | 1 Error: Expecting END MODULE statement at (1) temp.f95:50:5: 50 | use exchange_candies | 1 Fatal Error: Cannot open module file ‘exchange_candies.mod’ for reading at (1): No such file or directory compilation terminated.
def mirrorReflection(p, q):
from math import gcd
m = p // gcd(p, q)
if m % 2 == 0:
return 2
if (q // gcd(p, q)) % 2 == 0:
return 0
return 1
To solve this problem, we first determine the Lowest Common Multiple (LCM) of p and q. However, instead of finding LCM directly, it's simpler to first find the Greatest Common Divisor (GCD) and divide p and q by the GCD.
Next, we check if p divided by the GCD is an even number. If it is, the ray first meets receptor 2. If q divided by the GCD is even, the ray first meets receptor 0. Otherwise, the ray first meets receptor 1.
The algorithm remains the same across all languages, with only minor syntax differences between them. The key is to find the GCD and then further process p and q to identify which receptor the ray meets first.
int mirrorReflection(int p, int q) {
int m = p, n = q;
int gcd;
while (n) {
gcd = m % n;
m = n;
n = gcd;
}
if ((p / m) % 2 == 0) return 2;
if ((q / m) % 2 == 0) return 0;
return 1;
}
To solve this problem, we first determine the Lowest Common Multiple (LCM) of p and q. However, instead of finding LCM directly, it's simpler to first find the Greatest Common Divisor (GCD) and divide p and q by the GCD.
Next, we check if p divided by the GCD is an even number. If it is, the ray first meets receptor 2. If q divided by the GCD is even, the ray first meets receptor 0. Otherwise, the ray first meets receptor 1.
The algorithm remains the same across all languages, with only minor syntax differences between them. The key is to find the GCD and then further process p and q to identify which receptor the ray meets first.