对于所有数组元素,找到所有较小元素之和的乘积和所有较大元素之和
给定一个长度为N的整数数组arr[] ,任务是为数组中的每个数字找到所有大于该数字的数字之和与小于该数字的所有数字之和的乘积。
例子:
Input: arr[] = {8, 4, 9, 3}, N = 4
Output:- 63, 51, 0, 0
Explanation:
For first number 8: Sum of elements smaller than this is (4 + 3) = 7
and sum of elements greater than this is 9. hence output is 7*9 = 63.
For 4: Summation of elements smaller than this is 3 and
summation of elements greater than this is (9 + 8) = 17 hence output is 17*3 = 51
For 9: Summation of elements smaller than this is (8 + 4 + 3 ) = 15 and
summation of elements greater than this is 0. Hence output is 15*0 = 0
For 3: Summation of elements smaller than this is 0 and
summation of elements greater than this is (8 + 4 + 9) = 21. Hence output is 0*21 = 0
Input: arr[] = {1, 4, 7, 3, 6}, N = 5
Output: 0, 52, 0, 17, 56
方法:这个想法是找到每个数组元素的所有较小元素和所有较大元素的总和,然后找到它们的乘积。
请按照以下步骤解决此问题:
- 遍历数组的所有元素。
- 对于数组的每个元素,在两个不同的变量中不断添加小于当前元素的元素和大于当前元素的元素。
- 将这两个变量相乘(即存储的元素大于和小于当前元素)
- 存储每个元素的答案。
- 返回存储答案的数组。
下面是上述方法的实现:
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)