Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

You and your books

Easy Acceptance 50% Points 20

You are given n stacks of books, where each stack arr[i] represents the number of books in that stack (with each book having a height of 1 unit). In a single move, you can choose any number of consecutive stacks such that the height of each selected stack is less than or equal to k. After selecting a sequence of stacks, you can collect any number of books from those chosen stacks.

Your task is to determine the maximum number of books you can collect in this manner. You need to complete the function max_Books() which takes the integer arr, n, and k returns the maximum number of books you can collect.

Examples
Example 1

Example 1

Input: 8 2, 3 2 2 3 1 1 1 3

Output: 4

Explanation: We can collect maximum books from consecutive stacks numbered 2 and 3 having height less than equal to K.

Constraints
  • 1 <= n <= 10^5
  • 1 <= k <= 10^9
  • 0 <= arr[i] <= 10^9
Hints
  • Expected Time Complexity: O(n)
  • Expected Space Complexity: O(1)
Topics
Array Dynamic Programming
Companies
Visa Cisco
Added 02 Jan 2025 50% acceptance
Ln 1, Col 1Sign in to save your code
8 2
3 2 2 3 1 1 1 3
4