Find Subsequence of Length K With the Largest Sum

🏠 ⬅️ ➡️

You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.

Return any such subsequence as an integer array of length k.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

Example 1:

Input: nums = [2,1,3,3], k = 2 Output: [3,3] Explanation: The subsequence has the largest sum of 3 + 3 = 6.

Example 2:

Input: nums = [-1,-2,3,4], k = 3 Output: [-1,3,4] Explanation: The subsequence has the largest sum of -1 + 3 + 4 = 6.

Example 3:

Input: nums = [3,4,3,3], k = 2 Output: [3,4] Explanation: The subsequence has the largest sum of 3 + 4 = 7. Another possible subsequence is [4, 3].

Constraints:

  • 1 <= nums.length <= 1000
  • -105 <= nums[i] <= 105
  • 1 <= k <= nums.length

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

    integer, parameter :: k = 2
    integer, dimension(4) :: nums = [2, 1, 3, 3]
    integer, dimension(k) :: result

    call solve(nums, k, result)

    write (*, '(A, I0, A, I0)') 'Result: ', result(1), ',', result(2)

contains

    subroutine solve(nums, k, result)
        implicit none
        integer, intent(in) :: nums(:), k
        integer, intent(out) :: result(:)
        integer :: i, j, max_sum
        logical :: found

        do i = 1, size(nums) - k + 1
            max_sum = -huge(max_sum)
            found = .false.
            do j = i, i + k - 1
                if (nums(j) > max_sum) then
                    max_sum = nums(j)
                    found = .true.
                end if
            end do
            if (found) then
                result = nums(i:i+k-1)
                return
            end if
        end do
    end subroutine solve

end program main
Compiled
Executed
Correct
module largest_subsequence
  implicit none
contains
  function largest_subsequence(nums, k) result(subsequence)
    integer, intent(in) :: nums(:), k
    integer :: subsequence(k)
    ! Local variables
    integer :: i, j, max_sum, current_sum
    ! Initialize variables
    max_sum = -1000000000
    current_sum = 0
    ! Loop through the array
    do i = 1, size(nums) - k + 1
      ! Calculate the sum of the current subsequence
      current_sum = sum(nums(i:i+k-1))
      ! Check if the sum is greater than the maximum sum
      if (current_sum > max_sum) then
        ! If it is, update the maximum sum and the subsequence
        max_sum = current_sum
        subsequence = nums(i:i+k-1)
      end if
    end do
  end function largest_subsequence
end module

program largest_subsequence_test
  use largest_subsequence
  implicit none
  integer, parameter :: nums(4) = [2,1,3,3]
  integer, parameter :: k = 2
  integer :: subsequence(k)
  subsequence = largest_subsequence(nums, k)
  write (*,*) subsequence
end program
🌐 Data from online sources
def count_patterns_in_word(patterns, word):
    count = 0
    for pattern in patterns:
        if pattern in word:
            count += 1
    return count

The algorithm iterates through each string in the given patterns array. In each iteration, it checks if the current pattern is a substring of the given word. If it is, it increases the count.

For C++, we use the find method of the std::string class and compare the result to std::string::npos to check if the pattern is present.

For Java, we use the contains method of the String class to check if the pattern is present.

For Python, we use the in operator to check if the pattern is present in the word.

For JavaScript, we use the includes method of the String class to check if the pattern is present.

At the end of the loop, the function returns the count of the patterns that are substrings of the given word.

🌐 Data from online sources
#include <string>
#include <vector>

int countPatternsInWord(const std::vector<std::string> &patterns, const std::string &word) {
    int count = 0;
    for (const std::string &pattern : patterns) {
        if (word.find(pattern) != std::string::npos) {
            count++;
        }
    }
    return count;
}

The algorithm iterates through each string in the given patterns array. In each iteration, it checks if the current pattern is a substring of the given word. If it is, it increases the count.

For C++, we use the find method of the std::string class and compare the result to std::string::npos to check if the pattern is present.

For Java, we use the contains method of the String class to check if the pattern is present.

For Python, we use the in operator to check if the pattern is present in the word.

For JavaScript, we use the includes method of the String class to check if the pattern is present.

At the end of the loop, the function returns the count of the patterns that are substrings of the given word.