Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Maximize dot product

Medium Acceptance 13.27% Points 30.00

Given two arrays a and b of positive integers of size n and m where n >= m, the task is to maximize the dot product by inserting zeros in the second array but you cannot disturb the order of elements.

Dot product of array a and b of size n is a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1].

Examples
Example 1

Example 1

Input: n = 5, a[] = {2, 3, 1, 7, 8} m = 3, b[] = {3, 6, 7}

Output: 107

Explanation: We get maximum dot product after inserting 0 at first and third positions in second array. Therefore b becomes {0, 3, 0, 6, 7}. Maximum dot product = 2*0 + 3*3 + 1*0 + 7*6 + 8*7 = 107.

Hints
Hint 1
Expected Time Complexity: O(n*m)
Hint 2
Expected Auxiliary Space: O(n*m)
Constraints
  • 1 = m =n = 10^3
  • 1 = a[i], b[i] = 10^3
Companies
Samsung Adobe IBM
Topics
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