📜  给定数组的大于当前数字的先前数字的总和

📅  最后修改于: 2021-09-06 06:04:12             🧑  作者: Mango

给定一个数组A[] ,对于数组中每个元素,任务是找到严格大于当前元素的所有先前元素的总和。

例子:

朴素方法:对于每个元素,其思想是在其左侧找到严格大于当前元素的元素,然后找到所有这些元素的总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Max Element of the Array
const int maxn = 1000000;
 
// Function to find the sum of previous
// numbers that are greater than the
// current number for the given array
void sumGreater(int ar[], int N)
{
 
    // Loop to iterate over all
    // the elements of the array
    for (int i = 0; i < N; i++) {
 
        // Store the answer for
        // the current element
        int cur_sum = 0;
 
        // Iterate from (current index - 1)
        // to 0 and check if ar[j] is greater
        // than the current element and add
        // it to the cur_sum if so
 
        for (int j = i - 1; j >= 0; j--) {
 
            if (ar[j] > ar[i])
                cur_sum += ar[j];
        }
 
        // Print the answer for
        // current element
        cout << cur_sum << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
 
    // Size of the array
    int N = sizeof ar / sizeof ar[0];
 
    // Function call
    sumGreater(ar, N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Max Element of the Array
static int maxn = 1000000;
 
// Function to find the sum of previous
// numbers that are greater than the
// current number for the given array
static void sumGreater(int ar[], int N)
{
     
    // Loop to iterate over all
    // the elements of the array
    for(int i = 0; i < N; i++)
    {
         
        // Store the answer for
        // the current element
        int cur_sum = 0;
 
        // Iterate from (current index - 1)
        // to 0 and check if ar[j] is greater
        // than the current element and add
        // it to the cur_sum if so
        for(int j = i - 1; j >= 0; j--)
        {
            if (ar[j] > ar[i])
                cur_sum += ar[j];
        }
 
        // Print the answer for
        // current element
        System.out.print(cur_sum + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
 
    // Size of the array
    int N = ar.length;
 
    // Function call
    sumGreater(ar, N);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
 
# Max Element of the Array
maxn = 1000000;
 
# Function to find the sum of previous
# numbers that are greater than the
# current number for the given array
def sumGreater(ar, N):
 
    # Loop to iterate over all
    # the elements of the array
    for i in range(N):
 
        # Store the answer for
        # the current element
        cur_sum = 0;
 
        # Iterate from (current index - 1)
        # to 0 and check if ar[j] is greater
        # than the current element and add
        # it to the cur_sum if so
        for j in range(i, -1, -1):
            if (ar[j] > ar[i]):
                cur_sum += ar[j];
         
        # Prthe answer for
        # current element
        print(cur_sum, end = " ");
     
# Driver Code
if __name__ == '__main__':
 
    # Given array arr
    ar = [ 7, 3, 6, 2, 1] ;
 
    # Size of the array
    N = len(ar);
 
    # Function call
    sumGreater(ar, N);
 
# This code is contributed by sapnasingh4991


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Max Element of the Array
//static int maxn = 1000000;
 
// Function to find the sum of previous
// numbers that are greater than the
// current number for the given array
static void sumGreater(int []ar, int N)
{
     
    // Loop to iterate over all
    // the elements of the array
    for(int i = 0; i < N; i++)
    {
         
        // Store the answer for
        // the current element
        int cur_sum = 0;
 
        // Iterate from (current index - 1)
        // to 0 and check if ar[j] is greater
        // than the current element and add
        // it to the cur_sum if so
        for(int j = i - 1; j >= 0; j--)
        {
            if (ar[j] > ar[i])
                cur_sum += ar[j];
        }
 
        // Print the answer for
        // current element
        Console.Write(cur_sum + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []ar = { 7, 3, 6, 2, 1 };
 
    // Size of the array
    int N = ar.Length;
 
    // Function call
    sumGreater(ar, N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Max Element of the Array
const int maxn = 1000000;
 
// Initializing Fenwick Tree
int Bit[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
void sum(int ar[], int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++) {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index) {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index) {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        cout << ans << " ";
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000) {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
    int N = sizeof ar / sizeof ar[0];
 
    // Function call
    sum(ar, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Max Element of the Array
static int maxn = 1000000;
 
// Initializing Fenwick Tree
static int []Bit = new int[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
static void sum(int ar[], int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++)
    {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index > 0)
        {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index > 0)
        {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        System.out.print(ans + " ");
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000)
        {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
    int N = ar.length;
 
    // Function call
    sum(ar, N);
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program for the above approach
 
# Max Element of the Array
maxn = 1000000;
 
# Initializing Fenwick Tree
Bit = [0] * (maxn + 5);
 
# Function to calculate the sum of
# previous numbers that are greater
# than the current number in the array
def sum(ar, N):
   
    # Iterate from 1 to N
    for i in range(N):
        total_sum = 0;
        index = 100000;
 
        # If some greater values has
        # occured before current element
        # then it will be already stored
        # in Fenwick Tree
        while (index > 0):
           
            # Calculating sum of
            # all the elememts
            total_sum += Bit[index];
            index -= index & -index;
 
        cur_sum = 0;
 
        # Sum only smaller or equal
        # elements than current element
        index = ar[i];
 
        while (index > 0):
           
            # If some smaller values has
            # occured before it will be
            # already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
 
        ans = total_sum - cur_sum;
        print(ans, end=" ");
 
        # Update the fenwick tree
        index = ar[i];
        while (index <= 100000):
           
            # Updating The Fenwick Tree
            # for future values
            Bit[index] += ar[i];
            index += (index & -index);
 
# Driver Code
if __name__ == '__main__':
    # Given array arr
    arr = [7, 3, 6, 2, 1];
    N = len(arr);
 
    # Function call
    sum(arr, N);
 
# This code is contributed by sapnasingh4991


C#
// C# program for the above approach
using System;
class GFG{
 
// Max Element of the Array
static int maxn = 1000000;
 
// Initializing Fenwick Tree
static int []Bit = new int[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
static void sum(int []ar, int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++)
    {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index > 0)
        {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index > 0)
        {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        Console.Write(ans + " ");
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000)
        {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given array []arr
    int []ar = { 7, 3, 6, 2, 1 };
    int N = ar.Length;
 
    // Function call
    sum(ar, N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
0 7 7 16 18

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:为了优化上述方法,我们的想法是使用Fenwick Tree 。以下是步骤:

  1. 遍历给定的数组并找到存储在 Fenwick 树中的所有元素的总和(比如total_sum )。
  2. 现在考虑每个元素(比如arr[i] )作为 Fenwick 树的索引。
  3. 现在使用存储在 Tree 中的值找到小于当前元素的所有元素的总和(比如curr_sum )。
  4. total_sum – curr_sum的值将给出严格大于当前元素左侧元素的所有元素的总和。
  5. 更新 Fenwick 树中的当前元素。
  6. 对数组中的所有元素重复上述步骤。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Max Element of the Array
const int maxn = 1000000;
 
// Initializing Fenwick Tree
int Bit[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
void sum(int ar[], int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++) {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index) {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index) {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        cout << ans << " ";
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000) {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
    int N = sizeof ar / sizeof ar[0];
 
    // Function call
    sum(ar, N);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Max Element of the Array
static int maxn = 1000000;
 
// Initializing Fenwick Tree
static int []Bit = new int[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
static void sum(int ar[], int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++)
    {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index > 0)
        {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index > 0)
        {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        System.out.print(ans + " ");
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000)
        {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int ar[] = { 7, 3, 6, 2, 1 };
    int N = ar.length;
 
    // Function call
    sum(ar, N);
}
}
 
// This code is contributed by Rohit_ranjan

蟒蛇3

# Python3 program for the above approach
 
# Max Element of the Array
maxn = 1000000;
 
# Initializing Fenwick Tree
Bit = [0] * (maxn + 5);
 
# Function to calculate the sum of
# previous numbers that are greater
# than the current number in the array
def sum(ar, N):
   
    # Iterate from 1 to N
    for i in range(N):
        total_sum = 0;
        index = 100000;
 
        # If some greater values has
        # occured before current element
        # then it will be already stored
        # in Fenwick Tree
        while (index > 0):
           
            # Calculating sum of
            # all the elememts
            total_sum += Bit[index];
            index -= index & -index;
 
        cur_sum = 0;
 
        # Sum only smaller or equal
        # elements than current element
        index = ar[i];
 
        while (index > 0):
           
            # If some smaller values has
            # occured before it will be
            # already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
 
        ans = total_sum - cur_sum;
        print(ans, end=" ");
 
        # Update the fenwick tree
        index = ar[i];
        while (index <= 100000):
           
            # Updating The Fenwick Tree
            # for future values
            Bit[index] += ar[i];
            index += (index & -index);
 
# Driver Code
if __name__ == '__main__':
    # Given array arr
    arr = [7, 3, 6, 2, 1];
    N = len(arr);
 
    # Function call
    sum(arr, N);
 
# This code is contributed by sapnasingh4991

C#

// C# program for the above approach
using System;
class GFG{
 
// Max Element of the Array
static int maxn = 1000000;
 
// Initializing Fenwick Tree
static int []Bit = new int[maxn + 5];
 
// Function to calculate the sum of
// previous numbers that are greater
// than the current number in the array
static void sum(int []ar, int N)
{
 
    // Iterate from 1 to N
    for (int i = 0; i < N; i++)
    {
        int index;
        int total_sum = 0;
        index = 100000;
 
        // If some greater values has
        // occured before current element
        // then it will be already stored
        // in Fenwick Tree
        while (index > 0)
        {
 
            // Calculating sum of
            // all the elememts
            total_sum += Bit[index];
            index -= index & -index;
        }
        int cur_sum = 0;
 
        // Sum only smaller or equal
        // elements than current element
        index = ar[i];
 
        while (index > 0)
        {
 
            // If some smaller values has
            // occured before it will be
            // already stored in Tree
            cur_sum += Bit[index];
            index -= (index & -index);
        }
 
        int ans = total_sum - cur_sum;
        Console.Write(ans + " ");
 
        // Update the fenwick tree
        index = ar[i];
        while (index <= 100000)
        {
 
            // Updating The Fenwick Tree
            // for future values
            Bit[index] += ar[i];
            index += (index & -index);
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given array []arr
    int []ar = { 7, 3, 6, 2, 1 };
    int N = ar.Length;
 
    // Function call
    sum(ar, N);
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
0 7 7 16 18

时间复杂度: O(N * log(max_element))
辅助空间: O(max_element)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live