Sum of floor division of all pairs from given array
Given an array arr[] of size N, the task is to find the sum of the floor value of (arr[i] / arr[j]) for all pairs of indices (i, j).
Examples:
Input: arr[] = { 1, 2, 3 }
Output: 9
Explanation:
Sum = (arr[i] / arr[j]) (a[0] / a[0]) + (a[0] / a[1]) + (a[0] / a[2]) + (a[1] / a[1]) + (a[1] / a[2]) + (a[2] / a[2]) + (a[1] / a[0]) + (a[2] / a[0]) + (a[2] / a[1]) = 1 + 0 + 0 + 1 + 0 + 1 + 2 + 3 + 1 = 9
Therefore, the required output is 9.
Input: arr[] = { 4, 2, 5, 6 }
Output: 14
Naive Approach: The simplest approach to solve this problem is to generate all possible pairs of the array and for each pair, increment the result by the floor value of (arr[i] / arr[j]). Finally, print the result obtained.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the following observation:
If a sequence is X, X + 1, …, 2 * X – 1, 2 * X, …., 3 * X – 1
(X) / X + (X + 1) / X + … + (2 * X – 1) / X + (2 * X) / X + … + (3 * X – 1) / X
= 1 + 1 + … + 1 + 2 + … + 2
For the first X consecutive numbers, the floor value of (X + i) / X = 1
For the next X consecutive numbers, the floor value of (2 * X + i) / X = 2
and so on…
Follow the steps below to solve the problem:
- Initialize an array, say freq[], to store the frequency of array elements.
- Initialize an array, say preFreq[], to store the prefix sum of count[] array.
- preFreq[j] – preFreq[i] stores the count of array elements whose values lies in the range [i, j].
- Find the largest element in the array say, Max.
- Iterate over the range [1, Max]. For every ith value, count the array elements whose value lies in the range [i, j], using the preFreq[] array, where j is a multiple of i and increment the result by frequency[i] * (preFreq[j – 1] – preFreq[j – i – 1]) * (j / i – 1).
- Finally, print the result obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
void getFloorSum( int arr[], int n)
{
int freq[N] = { 0 };
int preFreq[N] = { 0 };
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
for ( int i = 1; i < N; i++) {
preFreq[i]
= preFreq[i - 1] + freq[i];
}
int ans = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i; j <= N; j += i) {
int X = (preFreq[j - 1]
- preFreq[j - i - 1]);
ans += X * (j / i - 1) * freq[i];
}
}
cout << ans;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
getFloorSum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = ( int ) 3e5;
static void getFloorSum( int arr[], int n)
{
int freq[] = new int [N];
int preFreq[] = new int [N];
for ( int i = 0 ; i < n; i++)
{
freq[arr[i]]++;
}
for ( int i = 1 ; i < N; i++)
{
preFreq[i]
= preFreq[i - 1 ] + freq[i];
}
int ans = 0 ;
for ( int i = 1 ; i < N; i++)
{
for ( int j = i; j < N; j += i)
{
int X = (preFreq[j - 1 ]
- preFreq[(Math.abs(j - i - 1 ))]);
ans += X * (j / i - 1 ) * freq[i];
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
getFloorSum(arr, n);
}
}
|
Python3
N = 10 * * 5
def getFloorSum(arr, n):
freq = [ 0 for i in range (N + 1 )]
preFreq = [ 0 for i in range (N + 1 )]
for i in range (n):
freq[arr[i]] + = 1
for i in range ( 1 , N):
preFreq[i] = preFreq[i - 1 ] + freq[i]
ans = 0
for i in range ( 1 , N + 1 ):
for j in range (i, N + 1 , i):
X = (preFreq[j - 1 ] - preFreq[j - i - 1 ])
ans + = X * (j / / i - 1 ) * freq[i]
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
n = len (arr)
getFloorSum(arr, n)
|
C#
using System;
class GFG{
static int N = ( int )3e5;
static void getFloorSum( int []arr, int n)
{
int []freq = new int [N];
int []preFreq = new int [N];
for ( int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
for ( int i = 1; i < N; i++)
{
preFreq[i] = preFreq[i - 1] + freq[i];
}
int ans = 0;
for ( int i = 1; i < N; i++)
{
for ( int j = i; j < N; j += i)
{
int X = (preFreq[j - 1] -
preFreq[(Math.Abs(j - i - 1))]);
ans += X * (j / i - 1) * freq[i];
}
}
Console.Write(ans);
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
getFloorSum(arr, n);
}
}
|
Javascript
<script>
var N = 1000;
function getFloorSum(arr, n)
{
var freq = Array(N).fill(0);
var preFreq = Array(N).fill(0);
for ( var i = 0; i < n; i++) {
freq[arr[i]]++;
}
for ( var i = 1; i < N; i++) {
preFreq[i]
= preFreq[i - 1] + freq[i];
}
var ans = 0;
for ( var i = 1; i <N; i++) {
for ( var j = i; j <N; j += i) {
var X = (preFreq[j - 1]
- preFreq[(Math.abs(j - i - 1))]);
ans += X * (parseInt(j / i) - 1) * freq[i];
}
}
document.write( ans);
}
var arr = [1, 2, 3 ];
var n = arr.length;
getFloorSum(arr, n);
</script>
|
Time Complexity: O(N + M * log(log(M)), where M is the largest array element
Auxiliary Space: O(M)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...