PIXELBANKv9.1.0
Menu

Dot Product of Two Sparse Vectors

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
🔒

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

0/0
Run code to see test results.