Knowee
Questions
Features
Study Tools

Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

Question

Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using a stack data structure. Here is a step-by-step guide on how to solve it:

  1. Initialize a stack. This stack will store the indices of the elements in the array. The bottom of the stack represents the current minimum.

  2. Initialize two arrays, left and right, of the same length as the input array. These arrays will store the number of contiguous subarrays that can be formed to the left and right of each element in the array.

  3. Iterate over the array from left to right. For each element, while the stack is not empty and the top of the stack is greater than the current element, pop the stack. The top of the stack now represents the first element to the left that is smaller than the current element. If the stack is empty, then the current element is the smallest so far. The number of contiguous subarrays that can be formed to the left of the current element is its index minus the index of the top of the stack.

  4. Repeat the previous step but iterate from right to left to fill the right array.

  5. Now that we have the left and right arrays, we can calculate the sum of the minimum of each subarray. Iterate over the array and for each element, add its value times the product of its corresponding values in the left and right arrays to the sum.

  6. Finally, return the sum modulo 109 + 7 to get the result.

Here is a Python code snippet that implements the above steps:

def sumSubarrayMins(arr):
    MOD = 10**9 + 7
    N = len(arr)

    stack = []
    prev = [0]*N
    for i in range(N):
        while stack and arr[i] <= arr[stack[-1]]:
            stack.pop()
        prev[i] = stack[-1] if stack else -1
        stack.append(i)

    stack = []
    next_ = [0]*N
    for k in range(N-1, -1, -1):
        while stack and arr[k] < arr[stack[-1]]:
            stack.pop()
        next_[k] = stack[-1] if stack else N
        stack.append(k)

    return sum((i - prev[i]) * (next_[i] - i) * arr[i] for i in range(N)) % MOD

This function takes as input a list of integers and returns the sum of the minimum of each subarray modulo 109 + 7.

This problem has been solved

Similar Questions

Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum. If the array is empty, return 0.Sample Input:9-2 -1 -3 4 -1 2 1 -5 4Sample Output: 6Explanation:The subarray is [4, -1, 2, 1] and the sum is 6

Given an array Arr of size N containing positive integers. Find the maximum sum of a any possible subsequence such that no two numbers in the subsequence should be adjacent in Arr.

Given an array A of N integers and an integer S. Write an efficient program to find the minimum X such that X divides all the elements of A, the total sum of the quotients doesn’t exceed S. Consider integer division while dividing elements of the array.Input format:First line contains two spaced integers N and S.Second line contains N space separated integers of A.Output format:Print single integer representing X.SAMPLE INPUT 6 2710 8 8 11 14 19SAMPLE OUTPUT 3ExplanationIf N=6, S=27 and A=[10,8,8,11,14,19]If all elements are divided by 1, i.e, then the total sum of quotients of elements in A is 70, which is greater than S. So X shouldn’t be 1.If X=2, then the total sum of quotients of elements in A becomes 34, which is also greater than S.If X=3, the total sum becomes 20, which is less than S. Therefore the output is 3 because 3 is the minimum number where quotient’s sum goes less than S for the first time.Constraints:1<=N<=10^51<=A[i],S<=10^9

01.Find the minimum element and its index in an array 3.5 marks Problem Statement You are given a function, void MinInArray(int arr[], int length); The function accepts an integer array 'arr' of size 'length' as its argument. Implement the function to find the minimum element of the array and print the minimum element and its index to the standard output (STDOUT). The minimum element and its index should be printed in separate lines. Note: Array index starts with 0 Minimum element and its index should be separated by a line in the output Assume there is only 1 minimum element in the array Print exactly what is asked, do not print any additional greeting messages Example: Input: 23 45 82 27 66 12 78 13 71 86 Output: 12 5 Explanation: 12 is the minimum element of array at index 5. The custom input format for the above case: 10 23 45 82 27 66 12 78 13 71 86 (The first line represents the size of the array, the second line represents the elements of the array) Sample input 1 9 11 144 6 7 112 95 Sample Output 1 0 The custom input format for the above case: 8 1 9 11 144 6 7 112 95 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time. give me the code

You are given an array of integers nums of length n.The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.You need to divide nums into 3 disjoint contiguous subarrays.Return the minimum possible sum of the cost of these subarrays.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.