You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code
of length of n
and a key k
.
To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
k > 0
, replace the ith
number with the sum of the next k
numbers.k < 0
, replace the ith
number with the sum of the previous k
numbers.k == 0
, replace the ith
number with 0
.As code
is circular, the next element of code[n-1]
is code[0]
, and the previous element of code[0]
is code[n-1]
.
Given the circular array code
and an integer key k
, return the decrypted code to defuse the bomb!
Example 1:
Input: code = [5,7,1,4], k = 3 Output: [12,10,16,13] Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
Example 2:
Input: code = [1,2,3,4], k = 0 Output: [0,0,0,0] Explanation: When k is zero, the numbers are replaced by 0.
Example 3:
Input: code = [2,4,9,3], k = -2 Output: [12,5,6,13] Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
Constraints:
n == code.length
1 <= n <= 100
1 <= code[i] <= 100
-(n - 1) <= k <= n - 1
program main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: code = (/ 5, 7, 1, 4 /)
integer :: k = 3
integer, dimension(n) :: decrypted_code
decrypted_code = defuse_bomb(code, k)
write (*, '(A, I0, A, I0, A)') 'Decrypted code: ', decrypted_code
contains
function defuse_bomb(code, k) result(decrypted_code)
implicit none
integer, dimension(:), intent(in) :: code
integer, intent(in) :: k
integer, dimension(size(code)) :: decrypted_code
integer :: i
do i = 1, size(code)
if (k > 0) then
decrypted_code(i) = sum(code((i + 1):(i + k)))
else if (k < 0) then
decrypted_code(i) = sum(code((i - k):(i - 1)))
else
decrypted_code(i) = 0
end if
end do
end function defuse_bomb
end program main
Decrypted code: 12 7
module defuse_bomb
implicit none
contains
function decrypt(code, k) result(decrypted_code)
integer, intent(in) :: code(:), k
integer :: decrypted_code(size(code))
! Declare variables
integer :: i, next_index, previous_index
integer :: next_value, previous_value
! Loop through the array and decrypt each value
do i = 1, size(code)
! Calculate the next index
next_index = i + k
if (next_index > size(code)) then
next_index = next_index - size(code)
end if
! Calculate the previous index
previous_index = i - k
if (previous_index < 1) then
previous_index = previous_index + size(code)
end if
! Calculate the next value
next_value = code(next_index)
! Calculate the previous value
previous_value = code(previous_index)
! Calculate the decrypted value
if (k > 0) then
decrypted_code(i) = next_value + previous_value
else if (k < 0) then
decrypted_code(i) = next_value - previous_value
else
decrypted_code(i) = 0
end if
end do
end function decrypt
end module
program main
use defuse_bomb
implicit none
! Test case 1: [5, 7, 1, 4], k = 3
integer, parameter :: code1(4) = [5, 7, 1, 4]
integer, parameter :: k1 = 3
integer :: decrypted_code1(4)
decrypted_code1 = decrypt(code1, k1)
write (*,*) "Decrypted code 1:", decrypted_code1
! Test case 2: [1, 2, 3, 4], k = 0
integer, parameter :: code2(4) = [1, 2, 3, 4]
integer, parameter :: k2 = 0
integer :: decrypted_code2(4)
decrypted_code2 = decrypt(code2, k2)
write (*,*) "Decrypted code 2:", decrypted_code2
! Test case 3: [2, 4, 9, 3], k = -2
integer, parameter :: code3(4) = [2, 4, 9, 3]
integer, parameter :: k3 = -2
integer :: decrypted_code3(4)
decrypted_code3 = decrypt(code3, k3)
write (*,*) "Decrypted code 3:", decrypted_code3
end program
temp.f95:56:49: 56 | integer, parameter :: code2(4) = [1, 2, 3, 4] | 1 Error: Unexpected data declaration statement at (1) temp.f95:57:32: 57 | integer, parameter :: k2 = 0 | 1 Error: Unexpected data declaration statement at (1) temp.f95:58:33: 58 | integer :: decrypted_code2(4) | 1 Error: Unexpected data declaration statement at (1) temp.f95:63:49: 63 | integer, parameter :: code3(4) = [2, 4, 9, 3] | 1 Error: Unexpected data declaration statement at (1) temp.f95:64:33: 64 | integer, parameter :: k3 = -2 | 1 Error: Unexpected data declaration statement at (1) temp.f95:65:33: 65 | integer :: decrypted_code3(4) | 1 Error: Unexpected data declaration statement at (1) temp.f95:59:35: 59 | decrypted_code2 = decrypt(code2, k2) | 1 Error: Symbol ‘code2’ at (1) has no IMPLICIT type; did you mean ‘code1’? temp.f95:66:35: 66 | decrypted_code3 = decrypt(code3, k3) | 1 Error: Symbol ‘code3’ at (1) has no IMPLICIT type; did you mean ‘code1’? temp.f95:59:19: 59 | decrypted_code2 = decrypt(code2, k2) | 1 Error: Symbol ‘decrypted_code2’ at (1) has no IMPLICIT type; did you mean ‘decrypted_code1’? temp.f95:66:19: 66 | decrypted_code3 = decrypt(code3, k3) | 1 Error: Symbol ‘decrypted_code3’ at (1) has no IMPLICIT type; did you mean ‘decrypted_code1’? temp.f95:59:39: 59 | decrypted_code2 = decrypt(code2, k2) | 1 Error: Symbol ‘k2’ at (1) has no IMPLICIT type; did you mean ‘k1’? temp.f95:66:39: 66 | decrypted_code3 = decrypt(code3, k3) | 1 Error: Symbol ‘k3’ at (1) has no IMPLICIT type; did you mean ‘k1’?
def minOperations(target: str) -> int:
operations = 0
current_bit = '0'
for bit in target:
if bit != current_bit:
operations += 1
current_bit = bit
return operations
The algorithm iterates through the target binary string, keeping track of the current bit. Whenever it encounters a bit in the target string that is different from the current one, it increments the operation counter and updates the current bit. By following this approach, we ensure that we only need to flip bits when necessary, resulting in the minimum number of operations.
int minOperations(string target) {
int operations = 0;
char current_bit = '0';
for (char bit : target) {
if (bit != current_bit) {
operations++;
current_bit = bit;
}
}
return operations;
}
The algorithm iterates through the target binary string, keeping track of the current bit. Whenever it encounters a bit in the target string that is different from the current one, it increments the operation counter and updates the current bit. By following this approach, we ensure that we only need to flip bits when necessary, resulting in the minimum number of operations.