Replace Elements with Greatest Element on Right Side

🏠 ⬅️ ➡️

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

Example 1:

Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). - index 3 --> the greatest element to the right of index 3 is index 4 (6). - index 4 --> the greatest element to the right of index 4 is index 5 (1). - index 5 --> there are no elements to the right of index 5, so we put -1.

Example 2:

Input: arr = [400] Output: [-1] Explanation: There are no elements to the right of index 0.

Constraints:

  • 1 <= arr.length <= 104
  • 1 <= arr[i] <= 105

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 6
    integer, dimension(n) :: arr
    integer :: i

    ! Example 1
    arr = [17, 18, 5, 4, 6, 1]
    call solve(arr)
    write (*, '(A)') 'Example 1:'
    do i = 1, size(arr)
        write (*, '(I2)', advance='no') arr(i)
    end do
    write (*, *) ''

    ! Example 2
    arr = [400]
    call solve(arr)
    write (*, '(A)') 'Example 2:'
    do i = 1, size(arr)
        write (*, '(I2)', advance='no') arr(i)
    end do
    write (*, *) ''

contains

    subroutine solve(arr)
        implicit none
        integer, intent(inout) :: arr(:)
        integer :: i, j

        do i = 1, size(arr) - 1
            j = maxloc(arr(i+1:size(arr)), dim=1) + i - 1
            arr(i) = arr(j)
        end do
        arr(size(arr)) = -1
    end subroutine solve

end program main
Compiled
Executed
Correct
module greatest_right_side

implicit none

contains

function greatest_right_side(arr) result(res)

integer, intent(in) :: arr(:)
integer :: res(size(arr))
integer :: i

do i = 1, size(arr) - 1
    res(i) = max(arr(i), arr(i+1:))
end do

res(size(arr)) = -1

end function greatest_right_side

end module greatest_right_side

program test_greatest_right_side

use greatest_right_side

implicit none

integer, parameter :: arr1(6) = [17, 18, 5, 4, 6, 1]
integer, parameter :: arr2(1) = [400]
integer :: res1(6)
integer :: res2(1)

res1 = greatest_right_side(arr1)
res2 = greatest_right_side(arr2)

write (*,*) res1
write (*,*) res2

end program test_greatest_right_side
🌐 Data from online sources
def kConcatenationMaxSum(arr, k):
    M = 10**9 + 7
    s = sum(arr)
    max_sum = max_ending_here = 0
    for i in range(len(arr) * min(2, k)):
        max_ending_here = max(arr[i % len(arr)], max_ending_here + arr[i % len(arr)])
        max_sum = max(max_sum, max_ending_here)
    return 0 if k == 1 else (((max_sum - max_ending_here) % M) * (k - 2) % M + max_ending_here) % M
  1. Calculate the sum of the array elements.
  2. Find the maximum subarray sum using Kadane's algorithm in a single array.
  3. If k is 1, return the maximum subarray sum from the single array.
  4. Concatenate the array twice and find the maximum subarray sum, considering that the subarray can go over the boundaries.
  5. Calculate the final maximum sum by considering the maximum subarray found in the two concatenated arrays, plus the sum of arrays multiplied by (k - 2), if the sum is positive.
  6. Return the maximum sum modulo 1,000,000,007.
🌐 Data from online sources
int kConcatenationMaxSum(vector<int>& arr, int k) {
    int M = 1e9 + 7;
    long long sum = 0, maxSum = 0, curMax = 0;
    for (int i = 0; i < arr.size(); i++) {
        sum += arr[i];
        curMax = max(arr[i], curMax + arr[i]);
        maxSum = max(maxSum, curMax);
    }
    if (k == 1) return maxSum % M;
    long long twoConcatenation = 0, twoConcatenationMax = 0;
    for (int i = 0; i < 2 * arr.size(); i++) {
        twoConcatenation = max((long long)arr[i % arr.size()], twoConcatenation + arr[i % arr.size()]);
        twoConcatenationMax = max(twoConcatenationMax, twoConcatenation);
    }
    return max({maxSum, twoConcatenationMax + (k - 2) * max(0LL, sum)}) % M;
}
  1. Calculate the sum of the array elements.
  2. Find the maximum subarray sum using Kadane's algorithm in a single array.
  3. If k is 1, return the maximum subarray sum from the single array.
  4. Concatenate the array twice and find the maximum subarray sum, considering that the subarray can go over the boundaries.
  5. Calculate the final maximum sum by considering the maximum subarray found in the two concatenated arrays, plus the sum of arrays multiplied by (k - 2), if the sum is positive.
  6. Return the maximum sum modulo 1,000,000,007.