Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Maximize K Corner Sum
00:00
5 left

Maximize K Corner Sum

MediumPython

Problem

Maximize sum of K corner elements in Array.

Given an integer array nums and integer k, select exactly k elements, where each selected element must come from either the left or right end of the array. Return the maximum possible sum.

Function signature: def max_sum_k_corner_elements(nums, k): Input is a list of integers and an integer. Output is an integer.

Example: nums = [1, 2, 3, 4, 5, 6, 1], k = 3 returns 12, by selecting 5, 6, 1 from the right corner.

Constraints: 1 <= k <= len(nums) <= 1000; values may be negative.

Constraints

  • 1 <= k <= len(nums) <= 1000
  • -10^9 <= nums[i] <= 10^9
  • Exactly k elements must be selected
  • Selected elements must come from the two ends of the array

Function Signature

def max_sum_k_corner_elements(nums, k):
Interviewer

Your question is Maximize K Corner Sum. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.