Given a positive integer n
, find and return the longest distance between any two adjacent 1
's in the binary representation of n
. If there are no two adjacent 1
's, return 0
.
Two 1
's are adjacent if there are only 0
's separating them (possibly no 0
's). The distance between two 1
's is the absolute difference between their bit positions. For example, the two 1
's in "1001 "
have a distance of 3.
Example 1:
Input: n = 22 Output: 2 Explanation: 22 in binary is "10110 ". The first adjacent pair of 1's is "10110 " with a distance of 2. The second adjacent pair of 1's is "10110 " with a distance of 1. The answer is the largest of these two distances, which is 2. Note that "10110 " is not a valid pair since there is a 1 separating the two 1's underlined.
Example 2:
Input: n = 8 Output: 0 Explanation: 8 in binary is "1000 ". There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
Example 3:
Input: n = 5 Output: 2 Explanation: 5 in binary is "101 ".
Constraints:
1 <= n <= 109
program longest_ones_distance
implicit none
integer :: n, i, j, dist, max_dist
logical :: found
character(len=64) :: bin_str
do while (.true.)
read(*,*) n
if (n == -1) exit
write(bin_str, '(Z0)') n
max_dist = 0
found = .false.
do i = 1, len(trim(bin_str))-1
if (bin_str(i:i+1) == '11') then
found = .true.
dist = abs(i - (i + 1))
if (dist > max_dist) then
max_dist = dist
end if
end if
end do
if (found) then
print *, max_dist
else
print *, 0
end if
end do
end program longest_ones_distance
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7977ba86f960 in ??? #1 0x7977ba8704d9 in ??? #2 0x7977baac417b in ??? #3 0x7977baabd684 in ??? #4 0x7977baabe2aa in ??? #5 0x57fdeb89123a in MAIN__ #6 0x57fdeb8914ba in main
! Declare variables
integer :: n, i, j, longest_distance, current_distance
logical :: found_adjacent_ones
! Read input
read(*,*) n
! Initialize variables
longest_distance = 0
found_adjacent_ones = .false.
! Convert n to binary
do i = 1, 32
if (mod(n, 2) == 1) then
if (found_adjacent_ones) then
current_distance = i - j
if (current_distance > longest_distance) then
longest_distance = current_distance
end if
else
found_adjacent_ones = .true.
j = i
end if
end if
n = n / 2
end do
! Output result
write(*,*) longest_distance
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7a02611db960 in ??? #1 0x7a02611dc4d9 in ??? #2 0x7a026143017b in ??? #3 0x7a0261429684 in ??? #4 0x7a026142a2aa in ??? #5 0x5b7f2469f209 in MAIN__ #6 0x5b7f2469f331 in main
def push_dominoes(dominoes: str) -> str:
prev, cur = None, dominoes
while prev != cur:
prev = cur
cur_lst = list(prev)
for i in range(len(cur)):
if prev[i] == 'L' and i > 0 and prev[i - 1] == '.':
cur_lst[i - 1] = 'L'
elif prev[i] == 'R' and i < len(cur) - 1 and prev[i + 1] == '.':
cur_lst[i + 1] = 'R'
cur = ''.join(cur_lst)
return cur
prev
and cur
variables to store the state of dominoes. Set cur
initially to the input string.prev != cur
.prev
string:cur
.#include <string>
using namespace std;
string pushDominoes(string dominoes) {
int n = dominoes.size();
string prev, cur = dominoes;
do {
prev = cur;
for (int i = 0; i < n; ++i) {
if (prev[i] == 'L' && i > 0 && prev[i - 1] == '.')
cur[i - 1] = 'L';
else if (prev[i] == 'R' && i < n - 1 && prev[i + 1] == '.')
cur[i + 1] = 'R';
}
} while (prev != cur);
return cur;
}
prev
and cur
variables to store the state of dominoes. Set cur
initially to the input string.prev != cur
.prev
string:cur
.