Find Total Power Amazon OA 2023 Solution

Want to solve Find Total Power Amazon OA 2023?, if yes then this article is for you.

We have research and collected a bundle of questions which was asked in Amazon OA in the year of 2022 and 2023. Today, we are going to see another problem from amazon oa called Find Total Power which was asked in 2022, this questions might be still active in some of the Amazon OA so save all the solutions and lets see how can we solve this problem.

Find Total Power Amazon OA 2023 Solution

As the ruler of a kingdom, you have an army of wizards at your command.

You are given a 0-indexed integer array strength, where strength[i] denotes the strength of the ith wizard. For a contiguous group of wizards (i.e. the wizards’ strengths form a subarray of strength), the total strength is defined as the product of the following two values:

  • The strength of the weakest wizard in the group.
  • The total of all the individual strengths of the wizards in the group.

Return the sum of the total strengths of all contiguous groups of wizards. Since the answer may be very large, return it modulo 109 + 7.

subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: strength = [1,3,1,2]
Output: 44

Explanation:

The following are all the contiguous groups of wizards:

  • [1] from [1,3,1,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1
  • [3] from [1,3,1,2] has a total strength of min([3]) * sum([3]) = 3 * 3 = 9
  • [1] from [1,3,1,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1
  • [2] from [1,3,1,2] has a total strength of min([2]) * sum([2]) = 2 * 2 = 4
  • [1,3] from [1,3,1,2] has a total strength of min([1,3]) * sum([1,3]) = 1 * 4 = 4
  • [3,1] from [1,3,1,2] has a total strength of min([3,1]) * sum([3,1]) = 1 * 4 = 4
  • [1,2] from [1,3,1,2] has a total strength of min([1,2]) * sum([1,2]) = 1 * 3 = 3
  • [1,3,1] from [1,3,1,2] has a total strength of min([1,3,1]) * sum([1,3,1]) = 1 * 5 = 5
  • [3,1,2] from [1,3,1,2] has a total strength of min([3,1,2]) * sum([3,1,2]) = 1 * 6 = 6
  • [1,3,1,2] from [1,3,1,2] has a total strength of min([1,3,1,2]) * sum([1,3,1,2]) = 1 * 7 = 7
    The sum of all the total strengths is 1 + 9 + 1 + 4 + 4 + 4 + 3 + 5 + 6 + 7 = 44.

Example 2:

Input: strength = [5,4,6]
Output: 213

Explanation:

The following are all the contiguous groups of wizards:

  • [5] from [5,4,6] has a total strength of min([5]) * sum([5]) = 5 * 5 = 25
  • [4] from [5,4,6] has a total strength of min([4]) * sum([4]) = 4 * 4 = 16
  • [6] from [5,4,6] has a total strength of min([6]) * sum([6]) = 6 * 6 = 36
  • [5,4] from [5,4,6] has a total strength of min([5,4]) * sum([5,4]) = 4 * 9 = 36
  • [4,6] from [5,4,6] has a total strength of min([4,6]) * sum([4,6]) = 4 * 10 = 40
  • [5,4,6] from [5,4,6] has a total strength of min([5,4,6]) * sum([5,4,6]) = 4 * 15 = 60
    The sum of all the total strengths is 25 + 16 + 36 + 36 + 40 + 60 = 213.

Constraints:

  • 1 <= strength.length <= 105
  • 1 <= strength[i] <= 109

Related: Google Online Assessment 2022 Questions List

Find Total Power Amazon OA 2022
Find Total Power Amazon OA 2023

SOLUTION

Program: Find Total Power Amazon OA Solution in Python

class Solution:
    def totalStrength(self, A: List[int]) -> int:
        N = len(A)
        Q = int(1e9)+7

        # monostack
        # ple[i] = previous lesser element, than the one at i
        st, ple = [], [-1]*N
        for i in range(N-1,-1,-1):
            while st and A[st[-1]] > A[i]:
                ple[st.pop()] = i
            st += [i]

        # preprocessing
        ps = [0] * (N+1)   # suffix sum
        ds = [0] * (N+1)   # suffix sum of suffix sum
        dpM = [0] * (N+1)  # dpM[i] = sum( suffixMin of A[0..i] )
        for z in range(N):
            i, j = ~z-1, ~z
            ps[i] = A[j] + ps[j]
            ds[i] = ps[i] + ds[j]
            k = ple[z]
            dpM[z] = A[z] * (z-k)
            if k != -1: dpM[z] += dpM[k]

        def query(L, R):  # -> sum( sum(A[k..R]) for k in [L..R] )
            if R < L: return 0
            diff = ds[L] - ds[R+1]
            chop = ps[R+1]
            size = R-L+1
            return diff - chop * size

        ans = 0
        dpC = [0] * (N+1)
        for i in range(N):
            j = ple[i]
            x = A[i] * query(j+1, i) % Q
            if j == -1: y = 0
            else:
                s = ps[j+1] - ps[i+1]
                y = (dpC[j] + dpM[j] * s) % Q
            dpC[i] = (x + y) % Q
            ans = (ans + dpC[i]) % Q

        return ans

Program: Find Total Power Amazon OA Solution in Java

    public int totalStrength(int[] A) {
        int res = 0, ac = 0, mod = (int)1e9 + 7, n = A.length;
        Stack<Integer> stack = new Stack<>();
        int[] acc = new int[n + 2];
        for (int r = 0; r <= n; ++r) {
            int a = r < n ? A[r] : 0;
            ac = (ac + a) % mod;
            acc[r + 1] = (ac + acc[r]) % mod;
            while (!stack.isEmpty() && A[stack.peek()] > a) {
                int i = stack.pop();
                int l = stack.isEmpty() ? -1 : stack.peek();
                long lacc = l < 0 ? acc[i] : acc[i] - acc[l], racc = acc[r] - acc[i];
                int ln = i - l, rn = r - i;
                res = (int)(res + (racc * ln - lacc * rn) % mod * A[i] % mod) % mod;
            }
            stack.push(r);
        }
        return (res + mod) % mod;
    }

Program: Find Total Power Amazon OA Solution in C++

    int totalStrength(vector<int>& A) {
        int res = 0, ac = 0, mod = 1e9 + 7, n = A.size();
        vector<int> stack = {}, acc(n + 2);
        for (int r = 0; r <= n; ++r) {
            int a = r < n ? A[r] : 0;
            ac = (ac + a) % mod;
            acc[r + 1] = (ac + acc[r]) % mod;
            while (!stack.empty() && A[stack.back()] > a) {
                int i = stack.back(); stack.pop_back();
                int l = stack.empty() ? -1 : stack.back();
                long lacc = l < 0 ? acc[i] : acc[i] - acc[l], racc = acc[r] - acc[i];
                int ln = i - l, rn = r - i;
                res = (res + (racc * ln - lacc * rn) % mod * A[i] % mod) % mod;
            }
            stack.push_back(r);
        }
        return (res + mod) % mod;
    }

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Grid Connections Amazon OA 2023
  7. Shipment Imbalance Amazon OA 2023
  8. Max Profit Amazon OA 2023
  9. Find Lowest Price Amazon OA 2023
  10. Decode String Frequency Amazon OA 2023
  11. Simple Cipher Amazon OA 2023
  12. Valid Discount Coupons Amazon OA 2023 Solution
  13. Count Maximum Teams Amazon OA 2023
  14. Minimum Coin Flips Amazon OA 2023
  15. Max Average Stock Price Amazon OA 2023 Solution
  16. Robot Bounded In Circle Amazon OA 2023
  17. Shopping Options Amazon OA 2023 Solution
  18. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  19. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  20. Slowest Key Amazon OA 2023 Solution
  21. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  22. Split String Into Unique Primes Amazon OA 2023 Solution
  23. Storage Optimization Amazon OA 2023 Solution
  24. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  25. Autoscale Policy Utilization Check Amazon OA 2023
  26. Optimal Utilization Solution Amazon OA 2023
  27. Merge Two Sorted Lists Solution Amazon OA 2023
  28. Two Sum Unique Pairs Solution Amazon OA 2023
  29. Amazon Music Pairs Amazon OA 2023 Solution
  30. Class Grouping Amazon OA 2023 Solution
  31. Find Max products Amazon OA 2023 Solution
  32. Get encrypted number Amazon OA 2023 Solution
  33. Find Total Imbalance Amazon OA 2023 Solution