Given an array arr[] of integers of length N, the task is to find the product of the sum of all the numbers larger than that number with the sum of all the numbers less than that number for each number in the array.
Examples:
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
Approach: The idea is to find the sum of all the smaller elements and all the greater elements for each array element and then find the product for those.
Follow the steps below to solve this problem:
- Iterate through all the elements of an array.
- For each element of an array keep adding elements smaller than the current and elements greater than the current element in two different variables.
- Multiply these two variables ( i.e which are storing elements greater and less than the current element )
- Store the answer for each element.
- Return the array storing the answers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > constructArray( int arr[], int n)
{
int i, s = 0, j = 0, sum = 0;
vector< int > v;
for (i = 0; i < n; i++) {
s = 0, sum = 0;
for (j = 0; j < n; j++) {
if (arr[j] != arr[i]) {
if (arr[i] > arr[j])
s += arr[j];
else
sum += arr[j];
}
}
v.push_back(s * sum);
}
return v;
}
int main()
{
int N = 4;
int arr[] = { 8, 4, 9, 3 };
vector< int > ans = constructArray(arr, N);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
void constructArray( int arr[], int n)
{
int i, s = 0, j = 0, sum = 0;
int idx=0;
int v[n];
for (i = 0; i < n; i++) {
s = 0, sum = 0;
for (j = 0; j < n; j++) {
if (arr[j] != arr[i]) {
if (arr[i] > arr[j])
s += arr[j];
else
sum += arr[j];
}
}
v[idx++]=(s * sum);
}
for ( int i=0;i<idx;i++){
printf ( "%d , " ,v[i]);
}
}
int main()
{
int N = 4;
int arr[] = { 8, 4, 9, 3 };
constructArray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static ArrayList<Integer>
constructArray( int arr[], int n)
{
int i = 0 ;
int s = 0 ;
int j = 0 ;
int sum = 0 ;
ArrayList<Integer> v = new ArrayList<Integer>();
for (i = 0 ; i < n; i++) {
s = 0 ;
sum = 0 ;
for (j = 0 ; j < n; j++) {
if (arr[j] != arr[i]) {
if (arr[i] > arr[j])
s += arr[j];
else
sum += arr[j];
}
}
v.add(s * sum);
}
return v;
}
public static void main(String[] args)
{
int N = 4 ;
int arr[] = { 8 , 4 , 9 , 3 };
ArrayList<Integer> ans = constructArray(arr, N);
for (Integer x : ans)
System.out.print(x + " " );
}
}
|
Python3
def constructArray(arr, n):
s = 0
sums = 0
v = []
for i in range (n):
s = 0
sums = 0
for j in range (n):
if arr[j] ! = arr[i]:
if arr[i] > arr[j]:
s + = arr[j]
else :
sums + = arr[j]
v.append(s * sums)
return v
N = 4
arr = [ 8 , 4 , 9 , 3 ]
ans = constructArray(arr, N)
print ( " " .join( list ( map ( str , ans))))
|
C#
using System;
using System.Collections;
public class GFG{
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]) {
if (arr[i] > arr[j])
s += arr[j];
else
sum += arr[j];
}
}
v.Add(s * sum);
}
return v;
}
static public void Main (){
int N = 4;
int [] arr = { 8, 4, 9, 3 };
ArrayList ans = constructArray(arr, N);
foreach ( var x in ans)
Console.Write(x + " " );
}
}
|
Javascript
<script>
function constructArray(arr, n) {
let i, s = 0, j = 0, sum = 0;
let v = [];
for (i = 0; i < n; i++) {
s = 0, sum = 0;
for (j = 0; j < n; j++) {
if (arr[j] != arr[i]) {
if (arr[i] > arr[j])
s += arr[j];
else
sum += arr[j];
}
}
v.push(s * sum);
}
return v;
}
let N = 4;
let arr = [8, 4, 9, 3];
let ans = constructArray(arr, N);
for (let x of ans)
document.write(x + " " )
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach:
Above solution works in O(N2) time, we can write a solution in O(n*Logn) time and with O(N) space complexity.
The idea is to sort the array and finding prefix_sum of sorted array.Now prefix sum will help us in finding the sum of all smaller elements and sum of all larger elements of the current element.
Follow the steps below for understanding…
- sort the given array and store resultant array into another array
- find prefix_sum array of sorted array
- Now find index of each element of given array into sorted array using binary search.
- With the help of prefix array,we can directly calculate sum of all smaller element and sum of all larger element of the current element.
- to calculate the resultant value corresponding to each element, do smaller*larger.
finally return the array storing the answers
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binary_search(vector< int > &arr, int low, int high, int x)
{
if (high >= low)
{
int mid = ((high + low) / 2);
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return binary_search(arr, low, mid - 1, x);
else
return binary_search(arr, mid + 1, high, x);
}
}
vector< int > constructArray(vector< int > &arr, int n)
{
vector< int > arr2;
arr2 = arr;
sort(arr2.begin(),arr2.end());
int prefix_sum[n] = {0};
prefix_sum[0] = arr2[0];
for ( int i = 1; i < n; i++)
prefix_sum[i] = prefix_sum[i - 1] + arr2[i];
int element_index, smaller, larger;
for ( int i = 0; i < n; i++)
{
element_index = binary_search(arr2, 0, n - 1, arr[i]);
smaller = prefix_sum[element_index] - arr[i];
larger = prefix_sum[n - 1] - prefix_sum[element_index];
arr[i] = smaller * larger;
}
return arr;
}
int main()
{
int N = 4;
vector< int > arr;
arr.push_back(8);
arr.push_back(4);
arr.push_back(9);
arr.push_back(3);
constructArray(arr, N);
for ( int i = 0; i < N; i++)
{
cout << arr[i] << " " ;
}
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int binary_search(ArrayList<Integer> arr, int low, int high, int x){
if (high >= low){
int mid = (high + low);
if (arr.get(mid) == x){
return mid;
} else if (arr.get(mid) > x){
return binary_search(arr, low, mid - 1 , x);
} else {
return binary_search(arr, mid + 1 , high, x);
}
}
return - 1 ;
}
static ArrayList<Integer> constructArray(ArrayList<Integer> arr, int n){
ArrayList<Integer> arr2 = new ArrayList<Integer>(arr);
Collections.sort(arr2);
int [] prefix_sum = new int [n];
prefix_sum[ 0 ] = arr2.get( 0 );
for ( int i = 1 ; i < n ; i++){
prefix_sum[i] = prefix_sum[i- 1 ] + arr2.get(i);
}
for ( int i = 0 ; i < n ; i++){
int element_index = binary_search(arr2, 0 , n - 1 , arr.get(i));
int smaller = prefix_sum[element_index]-arr.get(i);
int larger = prefix_sum[n- 1 ]-prefix_sum[element_index];
arr.set(i, smaller * larger);
}
return arr;
}
public static void main(String args[])
{
int N = 4 ;
ArrayList<Integer> arr = new ArrayList<Integer>(
List.of(
8 , 4 , 9 , 3
)
);
ArrayList<Integer> ans = constructArray(arr, N);
for ( int i = 0 ; i < ans.size() ; i++){
System.out.print(ans.get(i) + " " );
}
System.out.println( "" );
}
}
|
Python3
def binary_search(arr, low, high, x):
if high > = low:
mid = (high + low) / / 2
if arr[mid] = = x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1 , x)
else :
return binary_search(arr, mid + 1 , high, x)
def constructArray(arr, n):
arr2 = sorted (arr)
prefix_sum = [ 0 ] * n
prefix_sum[ 0 ] = arr2[ 0 ]
for i in range ( 1 , n):
prefix_sum[i] = prefix_sum[i - 1 ] + arr2[i]
for i in range (n):
element_index = binary_search(arr2, 0 , n - 1 , arr[i])
smaller = prefix_sum[element_index] - arr[i]
larger = prefix_sum[n - 1 ] - prefix_sum[element_index]
arr[i] = smaller * larger
return arr
N = 4
arr = [ 8 , 4 , 9 , 3 ]
ans = constructArray(arr, N)
print ( " " .join( list ( map ( str , ans))))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int binary_search(List< int > arr, int low, int high, int x){
if (high >= low){
int mid = (high + low);
if (arr[mid] == x){
return mid;
} else if (arr[mid] > x){
return binary_search(arr, low, mid - 1, x);
} else {
return binary_search(arr, mid + 1, high, x);
}
}
return -1;
}
static List< int > constructArray(List< int > arr, int n){
List< int > arr2 = new List< int >(arr);
arr2.Sort();
int [] prefix_sum = new int [n];
prefix_sum[0] = arr2[0];
for ( int i = 1 ; i < n ; i++){
prefix_sum[i] = prefix_sum[i-1] + arr2[i];
}
for ( int i = 0 ; i < n ; i++){
int element_index = binary_search(arr2, 0, n - 1, arr[i]);
int smaller = prefix_sum[element_index]-arr[i];
int larger = prefix_sum[n-1]-prefix_sum[element_index];
arr[i] = smaller * larger;
}
return arr;
}
public static void Main( string [] args){
int N = 4;
List< int > arr = new List< int >{8, 4, 9, 3};
List< int > ans = constructArray(arr, N);
for ( int i = 0 ; i < ans.Count ; i++){
Console.Write(ans[i] + " " );
}
Console.WriteLine( "" );
}
}
|
Javascript
function binary_search(arr, low, high, x)
{
if (high >= low)
{
let mid = Math.floor((high + low) / 2);
if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
return binary_search(arr, low, mid - 1, x);
else
return binary_search(arr, mid + 1, high, x);
}
}
function constructArray(arr, n)
{
let arr2 = [...arr];
arr2.sort();
prefix_sum = new Array(n).fill(0);
prefix_sum[0] = arr2[0];
for ( var i = 1; i < n; i++)
prefix_sum[i] = prefix_sum[i-1]+arr2[i];
let element_index, smaller, larger;
for ( var i = 0; i < n; i++)
{
element_index = binary_search(arr2, 0, n-1, arr[i]);
smaller = prefix_sum[element_index]-arr[i];
larger = prefix_sum[n-1]-prefix_sum[element_index]
arr[i] = smaller*larger;
}
return arr;
}
let N = 4;
let arr = [8, 4, 9, 3];
let ans = constructArray(arr, N);
console.log(ans);
|
Time Complexity: Steps involved into time requirement:
- Sorting the array takes :O(n*log(n)) time
- Finding prefix sum takes O(n) time
- Calling binary_search function each time inside loop running n time : O(n*logn)
Overall Time Complexity: O(n*logn)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Nov, 2022
Like Article
Save Article