Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

find the minimum sequence of array with sum

+3 votes
asked Aug 14, 2019 by Shaista Samrin (150 points)

1 Answer

+1 vote
answered Feb 17 by Rohini Vallarapu (160 points)
 This is the pseudocode o ur program
  FUNCTION MinSubarrayLengthWithSum(arr, target_sum):
  DECLARE left, right, current_sum, min_length

  SET left = 0
  SET current_sum = 0
  SET min_length = infinity // A very large number

  FOR right FROM 0 TO length(arr) - 1
    // Expand the window by adding the rightmost element
    SET current_sum = current_sum + arr[right]

    // While the current sum meets or exceeds the target, shrink the window from the left
    WHILE current_sum >= target_sum
      // Update the minimum length found so far
      SET current_length = right - left + 1
      IF current_length < min_length
        SET min_length = current_length
      END IF

      // Shrink the window by removing the leftmost element
      SET current_sum = current_sum - arr[left]
      SET left = left + 1
    END WHILE
  END FOR

  // If min_length is still infinity, no valid subarray was found
  IF min_length == infinity
    RETURN 0
  ELSE
    RETURN min_length
  END IF
END FUNCTION
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...