Emerson | National Instruments | Qn 1 of 2

57
Sep 13, 2026

Spiderman In Trouble 2 — Total Web Tension

I came across this problem in a coding assessment and I'm looking for the most optimal solution/approach.

Problem

Spider-Man visits N buildings consecutively. The resistance level of each building is given in an array a.

For every building i, calculate the sum of the resistance levels of all previously visited buildings j such that:

  • j < i
  • a[j] < a[i]

The total web tension is the sum of these values for all buildings.

Return the answer modulo 10^9 + 7.

Example

Input:
5
1 5 3 6 4

Output:
15

Explanation:

1 → 0
5 → 1
3 → 1
6 → 1 + 5 + 3 = 9
4 → 1 + 3 = 4

Total = 0 + 1 + 1 + 9 + 4 = 15

Constraints

  • 1 <= N <= 10^5
  • 1 <= a[i] <= 10^9

if anyone has encountered this exact problem before, I'd appreciate a link/source or a solution pls

Comments (1)