Given an array arr
of integers, check if there exist two indices i
and j
such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1:
Input: arr = [10,2,5,3] Output: true Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
Example 2:
Input: arr = [3,1,7,11] Output: false Explanation: There is no i and j that satisfy the conditions.
Constraints:
2 <= arr.length <= 500
-103 <= arr[i] <= 103
program main
implicit none
integer :: arr(4)
logical :: result
! Example 1
arr = [10, 2, 5, 3]
write(*,*) 'Example 1:'
result = check_array(arr)
write(*,*) 'Result: ', result
write(*,*) ''
! Example 2
arr = [3, 1, 7, 11]
write(*,*) 'Example 2:'
result = check_array(arr)
write(*,*) 'Result: ', result
write(*,*) ''
contains
function check_array(arr) result(result_)
implicit none
integer, intent(in) :: arr(:)
logical :: result_
integer :: i, j
do i = 1, size(arr) - 1
do j = i + 1, size(arr)
if (arr(i) == 2 * arr(j)) then
result_ = .true.
return
end if
end do
end do
result_ = .false.
end function check_array
end program main
Example 1: Result: T Example 2: Result: F
```
program two_times
implicit none
integer, parameter :: int64 = selected_int_kind(13)
integer :: i, j
integer(int64), dimension(:), allocatable :: arr
logical :: result
read (*, *) arr
do i = 1, size(arr) - 1
do j = i + 1, size(arr)
if (arr(i) == 2 * arr(j)) then
result = .true.
exit
end if
end do
if (result) exit
end do
write (*, *) result
end program two_times
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:24:2: 24 | ``` | 1 Error: Invalid character in name at (1)
def maxValueInStack(nums, k):
n = len(nums)
if k >= n:
return -1
max_val = float('-inf')
for i in range(k+1):
max_val = max(max_val, nums[i])
return max_val
We are looking for the maximum nums[i]
such that 0 <= i <= k
. Iterate through the first k+1
elements in the array and keep track of the maximum value encountered. If k >= n
, then it's not possible to have a non-empty pile after k moves, so return -1
in that case. Otherwise, return the maximum value found.
int maxValueInStack(vector<int>& nums, int k) {
int n = nums.size();
if (k >= n) return -1;
int max_val = INT_MIN;
for (int i = 0; i <= k; ++i) {
max_val = max(max_val, nums[i]);
}
return max_val;
}
We are looking for the maximum nums[i]
such that 0 <= i <= k
. Iterate through the first k+1
elements in the array and keep track of the maximum value encountered. If k >= n
, then it's not possible to have a non-empty pile after k moves, so return -1
in that case. Otherwise, return the maximum value found.