📌  相关文章
📜  对于所有数组元素,找到所有较小元素之和的乘积和所有较大元素之和

📅  最后修改于: 2022-05-13 01:56:06.672000             🧑  作者: Mango

对于所有数组元素,找到所有较小元素之和的乘积和所有较大元素之和

给定一个长度为N的整数数组arr[] ,任务是为数组中的每个数字找到所有大于该数字的数字之和与小于该数字的所有数字之和的乘积。

例子

方法:这个想法是找到每个数组元素的所有较小元素和所有较大元素的总和,然后找到它们的乘积。

请按照以下步骤解决此问题:

  • 遍历数组的所有元素。
    • 对于数组的每个元素,在两个不同的变量中不断添加小于当前元素的元素和大于当前元素的元素。
    • 将这两个变量相乘(即存储的元素大于和小于当前元素)
    • 存储每个元素的答案。
  • 返回存储答案的数组。

下面是上述方法的实现:

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find the answer
vector constructArray(int arr[], int n)
{
    int i, s = 0, j = 0, sum = 0;
    vector v;
    for (i = 0; i < n; i++) {
        s = 0, sum = 0;
        for (j = 0; j < n; j++) {
            if (arr[j] != arr[i]) {
 
                // Additon of elements
                // greater than ith indexed
                // elemet
                if (arr[i] > arr[j])
                    s += arr[j];
 
                // Additon of elements
                // less than ith indexed
                // element
                else
                    sum += arr[j];
            }
        }
 
        // Storing the product of elements
        // greater than ith indexed elements
        // with elements less thean ith
        // indexed element.
        v.push_back(s * sum);
    }
    return v;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { 8, 4, 9, 3 };
 
    // Function call
    vector ans = constructArray(arr, N);
    for (int x : ans)
        cout << x << " ";
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG
{
   
  // Function to find the answer
  public static ArrayList
    constructArray(int arr[], int n)
  {
    int i = 0;
    int s = 0;
    int j = 0;
    int sum = 0;
    ArrayList v = new ArrayList();
    for (i = 0; i < n; i++) {
      s = 0;
      sum = 0;
      for (j = 0; j < n; j++) {
        if (arr[j] != arr[i]) {
 
          // Additon of elements
          // greater than ith indexed
          // element
          if (arr[i] > arr[j])
            s += arr[j];
 
          // Additon of elements
          // less than ith indexed
          // element
          else
            sum += arr[j];
        }
      }
 
      // Storing the product of elements
      // greater than ith indexed elements
      // with elements less thean ith
      // indexed element.
      v.add(s * sum);
    }
    return v;
  }
 
  public static void main(String[] args)
  {
    int N = 4;
    int arr[] = { 8, 4, 9, 3 };
 
    // Function call
    ArrayList ans = constructArray(arr, N);
    for (Integer x : ans)
      System.out.print(x + " ");
  }
}
 
// This code is contributed by Rohit Pradhan


C#
// C# code to implement the approach
using System;
using System.Collections;
 
public class GFG{
 
  // Function to find the answer
  public static ArrayList
    constructArray(int[] arr, int n)
  {
    int i = 0;
    int s = 0;
    int j = 0;
    int sum = 0;
    ArrayList v = new ArrayList();
    for (i = 0; i < n; i++) {
      s = 0;
      sum = 0;
      for (j = 0; j < n; j++) {
        if (arr[j] != arr[i]) {
 
          // Additon of elements
          // greater than ith indexed
          // element
          if (arr[i] > arr[j])
            s += arr[j];
 
          // Additon of elements
          // less than ith indexed
          // element
          else
            sum += arr[j];
        }
      }
 
      // Storing the product of elements
      // greater than ith indexed elements
      // with elements less thean ith
      // indexed element.
      v.Add(s * sum);
    }
    return v;
  }
  static public void Main (){
 
    int N = 4;
    int[] arr = { 8, 4, 9, 3 };
 
    // Function call
    ArrayList ans = constructArray(arr, N);
    foreach (var x in ans)
      Console.Write(x + " ");
  }
}
 
// This code is contributed by hrithikgarg03188.


输出
63 51 0 0 

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