Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Maximize dot product

Medium Acceptance 13.27% Points 30

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.

Constraints
  • 1 = m =n = 10^3
  • 1 = a[i], b[i] = 10^3
Hints
  • Expected Time Complexity: O(n*m)
  • Expected Auxiliary Space: O(n*m)
Topics
Dynamic Programming
Companies
Samsung Adobe IBM
Added 02 Jan 2025 13.27% acceptance
Ln 1, Col 1Sign in to save your code
5 3
2 3 1 7 8
3 6 7
107