FastPrepFind Max Minimum Value Using K Elements 🐴

Find Max Minimum Value Using K Elements 🐴

ServiceNow logoServiceNow● EasyOA

Updated

Problem statement

You are given two arrays, shop1 and shop2, both of size N, and an integer K. You have to find the maximum possible minimum value using K elements from both arrays.

Choose exactly K different indices i1, i2, ..., iK. The same indices are used in both arrays. The value of the choice is the smaller of the two sums:

Min(shop1[i1] + ... + shop1[iK], shop2[i1] + ... + shop2[iK])

Return the largest value over all possible choices.

For example, if shop1 = [6, 3, 6, 5, 1], shop2 = [1, 4, 5, 9, 2] and K = 3, choosing indices 0, 2, 3 gives Min(6 + 6 + 5, 1 + 5 + 9) = Min(17, 15) = 15, and no other choice does better.

Complete the function findMaxMinValueUsingKElements in the editor.

findMaxMinValueUsingKElements has the following parameters:

  • int[] shop1: an array of integers representing the first shop
  • int[] shop2: an array of integers representing the second shop
  • int k: the number of indices to choose

The function returns an int: the maximum possible value of the smaller sum.

Function

findMaxMinValueUsingKElements(shop1: int[], shop2: int[], k: int) → int

Examples

Example 1

shop1 = [6, 3, 6, 5, 1]shop2 = [1, 4, 5, 9, 2]k = 3return = 15

Min(6+6+5, 1+5+9) = 15.

Example 2

shop1 = [10, 2, 4]shop2 = [1, 9, 6]k = 1return = 4

Min(4, 6) = 4.

Constraints

  • 1 ≤ N ≤ 50
  • shop1.length == shop2.length == N
  • 1 ≤ k ≤ N
  • 1 ≤ shop1[i], shop2[i] ≤ 50

More ServiceNow interview questions

Similar questions at other companies

All ServiceNow interview questionsSee ServiceNow hiring insights
Saved
class Solution {
    public int findMaxMinValueUsingKElements(int[] shop1, int[] shop2, int k) {
        // write your code here
    }

}
Checking account…