Given an integer num
, return the number of steps to reduce it to zero.
In one step, if the current number is even, you have to divide it by 2
, otherwise, you have to subtract 1
from it.
Example 1:
Input: num = 14 Output: 6 Explanation: Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7 is odd; subtract 1 and obtain 6. Step 3) 6 is even; divide by 2 and obtain 3. Step 4) 3 is odd; subtract 1 and obtain 2. Step 5) 2 is even; divide by 2 and obtain 1. Step 6) 1 is odd; subtract 1 and obtain 0.
Example 2:
Input: num = 8 Output: 4 Explanation: Step 1) 8 is even; divide by 2 and obtain 4. Step 2) 4 is even; divide by 2 and obtain 2. Step 3) 2 is even; divide by 2 and obtain 1. Step 4) 1 is odd; subtract 1 and obtain 0.
Example 3:
Input: num = 123 Output: 12
Constraints:
0 <= num <= 106
program main
implicit none
integer :: num, steps
! Example 1
num = 14
write (*, '(A, I0, A, I0)') 'Example 1: ', num, ' -> ', solve(num)
! Example 2
num = 8
write (*, '(A, I0, A, I0)') 'Example 2: ', num, ' -> ', solve(num)
! Example 3
num = 123
write (*, '(A, I0, A, I0)') 'Example 3: ', num, ' -> ', solve(num)
contains
function solve(num) result(steps)
implicit none
integer, intent(in) :: num
integer :: steps
do while (num /= 0)
if (mod(num, 2) == 0) then
num = num / 2
else
num = num - 1
end if
steps = steps + 1
end do
end function solve
end program main
temp.f95:27:16: 27 | num = num / 2 | 1 Error: Dummy argument ‘num’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:29:16: 29 | num = num - 1 | 1 Error: Dummy argument ‘num’ with INTENT(IN) in variable definition context (assignment) at (1)
! Declare variables
integer :: num, steps
! Read input
read(*,*) num
! Initialize steps
steps = 0
! While num is not equal to 0
do while (num /= 0)
! If num is even
if (mod(num, 2) == 0) then
! Divide num by 2
num = num / 2
! Otherwise
else
! Subtract 1 from num
num = num - 1
end if
! Increment steps
steps = steps + 1
end do
! Print output
write(*,*) steps
end
At line 5 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7de9b7251960 in ??? #1 0x7de9b72524d9 in ??? #2 0x7de9b74a617b in ??? #3 0x7de9b749f684 in ??? #4 0x7de9b74a02aa in ??? #5 0x5b43a37c6209 in MAIN__ #6 0x5b43a37c62f7 in main
def queensAttacktheKing(queens, king):
result = []
for x in range(-1, 2):
for y in range(-1, 2):
if x == 0 and y == 0:
continue
posX, posY = king[0] + x, king[1] + y
while 0 <= posX < 8 and 0 <= posY < 8:
for queen in queens:
if posX == queen[0] and posY == queen[1]:
result.append([queen[0], queen[1]])
break
posX += x
posY += y
return result
The algorithm consists of the following steps: 1. For each direction, including diagonals, create a loop that starts from the position of the King. 2. Iterate through the loop, going one cell at a time in the chosen direction. 3. For each iteration, check if there is a queen on that cell by comparing it to the coordinates of the queens in the input array. 4. If a queen is found in that cell, add the coordinates of that queen to the result list and break the inner loop. 5. Continue to the next direction.
This algorithm ensures that every possible direction is checked from the King's position, and if there's a queen in that direction, it will be added to the resulting list. This way, we get all the queens that can attack the King.
#include <vector>
std::vector<std::vector<int>> queensAttacktheKing(std::vector<std::vector<int>>& queens, std::vector<int>& king) {
std::vector<std::vector<int>> result;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (x == 0 && y == 0) continue;
int posX = king[0] + x;
int posY = king[1] + y;
while (posX >= 0 && posY >= 0 && posX < 8 && posY < 8) {
for (std::vector<int>& queen : queens) {
if (posX == queen[0] && posY == queen[1]) {
result.push_back({queen[0], queen[1]});
break;
}
}
posX += x;
posY += y;
}
}
}
return result;
}
The algorithm consists of the following steps: 1. For each direction, including diagonals, create a loop that starts from the position of the King. 2. Iterate through the loop, going one cell at a time in the chosen direction. 3. For each iteration, check if there is a queen on that cell by comparing it to the coordinates of the queens in the input array. 4. If a queen is found in that cell, add the coordinates of that queen to the result list and break the inner loop. 5. Continue to the next direction.
This algorithm ensures that every possible direction is checked from the King's position, and if there's a queen in that direction, it will be added to the resulting list. This way, we get all the queens that can attack the King.