You are given an integer array arr
. Sort the integers in the array in ascending order by the number of 1
's in their binary representation and in case of two or more integers have the same number of 1
's you have to sort them in ascending order.
Return the array after sorting it.
Example 1:
Input: arr = [0,1,2,3,4,5,6,7,8] Output: [0,1,2,4,8,3,5,6,7] Explantion: [0] is the only integer with 0 bits. [1,2,4,8] all have 1 bit. [3,5,6] have 2 bits. [7] has 3 bits. The sorted array by bits is [0,1,2,4,8,3,5,6,7]
Example 2:
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1] Output: [1,2,4,8,16,32,64,128,256,512,1024] Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
Constraints:
1 <= arr.length <= 500
0 <= arr[i] <= 104
program main
implicit none
integer, parameter :: n = 9
integer :: arr(n) = reshape([0, 1, 2, 3, 4, 5, 6, 7, 8], shape(arr))
integer :: i, j, tmp
call sort_by_bits(arr)
do i = 1, size(arr)
write(*, '(I0)', advance='no') arr(i)
end do
write(*,*)
contains
subroutine sort_by_bits(arr)
implicit none
integer, intent(inout) :: arr(:)
integer :: i, j, tmp
do i = 1, size(arr) - 1
do j = i + 1, size(arr)
if (count_bits(arr(i)) < count_bits(arr(j))) then
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp
else if (count_bits(arr(i)) == count_bits(arr(j)) .and. arr(i) > arr(j)) then
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp
end if
end do
end do
end subroutine sort_by_bits
function count_bits(x) result(res)
implicit none
integer, value :: x
integer :: res
res = 0
do while (x /= 0)
res = res + 1
x = x - 2 * (mod(x, 2))
end do
end function count_bits
end program main
infinite loop
module sort_by_bits
implicit none
private
public :: sort_by_bits
contains
function sort_by_bits(arr) result(sorted_arr)
integer, intent(in) :: arr(:)
integer :: sorted_arr(size(arr))
integer :: i, j, temp
logical :: swapped
do
swapped = .false.
do i = 1, size(arr) - 1
if (count_bits(arr(i)) > count_bits(arr(i + 1))) then
temp = arr(i)
arr(i) = arr(i + 1)
arr(i + 1) = temp
swapped = .true.
end if
end do
if (.not. swapped) exit
end do
sorted_arr = arr
end function sort_by_bits
function count_bits(n) result(count)
integer, intent(in) :: n
integer :: count
integer :: i
count = 0
do i = n, 1, -1
if (mod(n, 2) == 1) then
count = count + 1
end if
n = n / 2
end do
end function count_bits
end module sort_by_bits
program test_sort_by_bits
use sort_by_bits
implicit none
integer, parameter :: arr1(9) = [0, 1, 2, 3, 4, 5, 6, 7, 8]
integer, parameter :: arr2(11) = [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
integer :: arr(9)
arr = arr1
write (*, '(A)') 'Example 1:'
write (*, '(A)') 'Input: ' // to_string(arr)
write (*, '(A)') 'Output: ' // to_string(sort_by_bits(arr))
write (*, *)
arr = arr2
write (*, '(A)') 'Example 2:'
write (*, '(A)') 'Input: ' // to_string(arr)
write (*, '(A)') 'Output: ' // to_string(sort_by_bits(arr))
write (*, *)
contains
function to_string(arr) result(str)
integer, intent(in) :: arr(:)
character(len=:), allocatable :: str
integer :: i
allocate(character(len=size(arr)*10) :: str)
do i = 1, size(arr)
write (str(10*(i-1)+1:10*i), '(I10)') arr(i)
end do
end function to_string
end program test_sort_by_bits
temp.f95:4:26: 4 | public :: sort_by_bits | 1 Error: PUBLIC attribute applied to MODULE sort_by_bits at (1) temp.f95:6:25: 6 | function sort_by_bits(arr) result(sorted_arr) | 1 Error: MODULE attribute of โsort_by_bitsโ conflicts with PROCEDURE attribute at (1) temp.f95:7:37: 7 | integer, intent(in) :: arr(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:35: 8 | integer :: sorted_arr(size(arr)) | 1 Error: Symbol โarrโ is used before it is typed at (1) temp.f95:9:29: 9 | integer :: i, j, temp | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:26: 10 | logical :: swapped | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:10: 12 | do | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:29: 13 | swapped = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:35: 14 | do i = 1, size(arr) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:69: 15 | if (count_bits(arr(i)) > count_bits(arr(i + 1))) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:33: 16 | temp = arr(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:39: 17 | arr(i) = arr(i + 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:37: 18 | arr(i + 1) = temp | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:36: 19 | swapped = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:19: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:15: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:35: 22 | if (.not. swapped) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:24: 25 | sorted_arr = arr | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:7: 26 | end function sort_by_bits | 1 Error: Expecting END MODULE statement at (1) temp.f95:38:12: 38 | n = n / 2 | 1 Error: Dummy argument โnโ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:44:9: 44 | use sort_by_bits | 1 Fatal Error: Cannot open module file โsort_by_bits.modโ for reading at (1): No such file or directory compilation terminated.
def min_moves_to_palindrome(s: str) -> int:
moves = 0
s = list(s)
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
temp_right = right
while s[left] != s[temp_right]:
temp_right -= 1
s.insert(right, s.pop(temp_right))
moves += right - temp_right
left += 1
right -= 1
return moves
moves
to store the total number of moves, and left
and right
pointers for both ends of string s.left
pointer is less than right
.
a. If the characters at the left and right pointers are not equal, find the position of the left pointer character in the remaining right section of the string.
b. Swap the characters found in the right section with the character at 'right' position.
c. Increment the number of moves by the distance between the right
and the swap position.left
pointer and decrement the right
pointer after each iteration.#include <string>
using namespace std;
int min_moves_to_palindrome(string s) {
int moves = 0;
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) {
int temp_right = right;
while (s[left] != s[temp_right]) {
temp_right--;
}
s.erase(s.begin() + temp_right);
s.insert(s.begin() + right, s[left]);
moves += right - temp_right;
}
left++;
right--;
}
return moves;
}
moves
to store the total number of moves, and left
and right
pointers for both ends of string s.left
pointer is less than right
.
a. If the characters at the left and right pointers are not equal, find the position of the left pointer character in the remaining right section of the string.
b. Swap the characters found in the right section with the character at 'right' position.
c. Increment the number of moves by the distance between the right
and the swap position.left
pointer and decrement the right
pointer after each iteration.