Google Online Assessment (OA) - Largest Subarray

An array A is greater than an array B if the first non-matching item in both arrays has a greater value in A than in B. For example,

  • A = [3, 4, 9, 6, 8]
  • B = [3, 4, 8, 6, 7]

A is bigger than B because the first non-matching element is larger in A (A[2] > B[2]).

A contiguous subarray is a subarray that has consecutive indexes.

Given an array arr consisting of n integers and an integer k, return the largest contiguous subarray of length k from all the possible contiguous subarrays of length k.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= arr[i] <= 1000

Examples

Example 1:

Input:

arr = [1, 4, 3, 2, 5]

k = 4

Output: [4, 3, 2, 5]
Explanation:

There are 2 possible subarrays of size 4: [1, 4, 3, 2] and [4, 3, 2, 5], and the largest subarray is [4, 3, 2, 5].

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade