You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums.
You then do the following steps:
original is found in nums, multiply it by two (i.e., set original = 2 * original).Return the final value of original.
Example 1:
Input: nums = [5,3,6,1,12], original = 3 Output: 24 Explanation: - 3 is found in nums. 3 is multiplied by 2 to obtain 6. - 6 is found in nums. 6 is multiplied by 2 to obtain 12. - 12 is found in nums. 12 is multiplied by 2 to obtain 24. - 24 is not found in nums. Thus, 24 is returned.
Example 2:
Input: nums = [2,7,9], original = 4 Output: 4 Explanation: - 4 is not found in nums. Thus, 4 is returned.
Constraints:
1 <= nums.length <= 10001 <= nums[i], original <= 1000program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer :: original
integer :: i
! Example 1
nums = [5, 3, 6, 1, 12]
original = 3
call solve(nums, original)
write (*, '(A, I0)') 'Example 1: ', original
! Example 2
nums = [2, 7, 9]
original = 4
call solve(nums, original)
write (*, '(A, I0)') 'Example 2: ', original
contains
subroutine solve(nums, original)
implicit none
integer, dimension(:), intent(in) :: nums
integer, intent(inout) :: original
do while (.true.)
if (any(nums == original)) then
original = 2 * original
else
exit
end if
end do
end subroutine solve
end program main
temp.f95:16:4:
16 | nums = [2, 7, 9]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (5 and 3)
module solve_problem
implicit none
private
public :: solve
contains
integer function solve(nums, original) result(final)
implicit none
integer, intent(in) :: nums(:), original
integer :: i
final = original
do i = 1, size(nums)
if (any(nums == final)) then
final = 2 * final
else
exit
end if
end do
end function solve
end module solve_problem
program test
use solve_problem
implicit none
integer, parameter :: nums(3) = [5, 3, 6]
integer :: original = 3
integer :: final
final = solve(nums, original)
write (*,*) "Final value of original:", final
end program test
Final value of original: 12
def min_moves_to_convert(s):
count = 0
i = 0
while i < len(s) - 2:
if s[i] == "X" and s[i + 1] == "X" and s[i + 2] == "X":
count += 1
i += 3
else:
i += 1
return count
The algorithm iterates over the given string s. For each character, if it finds a sequence of 3 consecutive 'X', it increases the 'count' and moves the iterator 3 positions forward. If it doesn't find the sequence, it moves the iterator 1 position forward. The algorithm ultimately returns the 'count', which is the minimum number of moves required to convert all characters in the string to 'O'.
int min_moves_to_convert(const std::string& s) {
int count = 0;
for (size_t i = 0; i < s.length() - 2; i++) {
if (s[i] == 'X' && s[i + 1] == 'X' && s[i + 2] == 'X') {
count++;
i += 2; // move by 3
}
}
return count;
}
The algorithm iterates over the given string s. For each character, if it finds a sequence of 3 consecutive 'X', it increases the 'count' and moves the iterator 3 positions forward. If it doesn't find the sequence, it moves the iterator 1 position forward. The algorithm ultimately returns the 'count', which is the minimum number of moves required to convert all characters in the string to 'O'.