v9.1.1
Menu

Dot Product of Two Sparse Vectors

easyPROHash Maps & DesignMeta DSA

Given two sparse vectors represented as arrays, compute their dot product efficiently.

A sparse vector has mostly zero elements. Store only non-zero elements for efficiency.

Example

Input
1,0,0,2,3
0,3,0,4,0
Output
8
Reasoning
  • The input represents two sparse vectors: [1,0,0,2,3][1, 0, 0, 2, 3] and [0,3,0,4,0][0, 3, 0, 4, 0]
  • To compute the dot product, we multiply corresponding elements: 1⋅0+0⋅3+0⋅0+2⋅4+3⋅01 \cdot 0 + 0 \cdot 3 + 0 \cdot 0 + 2 \cdot 4 + 3 \cdot 0
  • This simplifies to: 0+0+0+8+0=80 + 0 + 0 + 8 + 0 = 8
  • The final output is the result of this computation: 88

Constraints

  • 1 <= len(nums1) == len(nums2) <= 10^5
  • 0 <= nums[i] <= 100

Learn this in

More from Meta DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.