Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

You and your books

Easy Acceptance 50.00% Points 20.00

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.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= n <= 10^5
  • 1 <= k <= 10^9
  • 0 <= arr[i] <= 10^9
Companies
Visa Cisco
Topics
Array Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions