< Summary

Information
Class: LeetCode.Algorithms.RangeProductQueriesOfPowers.RangeProductQueriesOfPowersPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeProductQueriesOfPowers\RangeProductQueriesOfPowersPrefixSum.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ProductQueries(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RangeProductQueriesOfPowers\RangeProductQueriesOfPowersPrefixSum.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12using System.Numerics;
 13
 14namespace LeetCode.Algorithms.RangeProductQueriesOfPowers;
 15
 16/// <inheritdoc />
 17public class RangeProductQueriesOfPowersPrefixSum : IRangeProductQueriesOfPowers
 18{
 19    private const int Mod = 1_000_000_007;
 20
 21    /// <summary>
 22    ///     Time complexity - O(q), where q is queries.Length
 23    ///     Space complexity - O(1)
 24    /// </summary>
 25    /// <param name="n"></param>
 26    /// <param name="queries"></param>
 27    /// <returns></returns>
 28    public int[] ProductQueries(int n, int[][] queries)
 229    {
 230        var exponents = new List<int>(BitOperations.PopCount((uint)n));
 31
 12832        for (var bit = 0; bit < 31; bit++)
 6233        {
 6234            if (((n >> bit) & 1) == 0)
 5735            {
 5736                continue;
 37            }
 38
 539            exponents.Add(bit);
 540        }
 41
 242        var exponentsPrefixSum = new int[exponents.Count + 1];
 43
 1444        for (var i = 0; i < exponents.Count; i++)
 545        {
 546            exponentsPrefixSum[i + 1] = exponentsPrefixSum[i] + exponents[i];
 547        }
 48
 249        var maxExponent = exponentsPrefixSum[^1];
 50
 251        var powersOfTwo = new int[maxExponent + 1];
 52
 253        powersOfTwo[0] = 1;
 54
 1855        for (var i = 1; i <= maxExponent; i++)
 756        {
 757            powersOfTwo[i] = powersOfTwo[i - 1] * 2 % Mod;
 758        }
 59
 260        var result = new int[queries.Length];
 61
 1262        for (var i = 0; i < queries.Length; i++)
 463        {
 464            var left = queries[i][0];
 465            var right = queries[i][1];
 466            var exponent = exponentsPrefixSum[right + 1] - exponentsPrefixSum[left];
 67
 468            result[i] = powersOfTwo[exponent];
 469        }
 70
 271        return result;
 272    }
 73}