给定一个数组arr[]表示数组元素的范围,任务是使用这些范围计算每个可能数组的不同中位数。
例子:
Input: arr[] = {{1, 2}, {2, 3}}
Output: 3
Explanation:
=> If x1 = 1 and x2 = 2, Median is 1.5
=> If x1 = 1 and x2 = 3, Median is 2
=> If x1 = 2 and x2 = 2, Median is 2
=> If x1 = 2 and x2 = 3, Median is 2.5
Hence the number of distinct medians are {1.5, 2, 2.5} i.e, 3
Input: arr[] = {{100, 100}, {10, 10000}, {1, 109}}
Output: 9991
朴素的方法:一个简单的解决方案是尝试数组的所有可能值并找到所有这些元素的中值并计算数组的不同中值。
时间复杂度:
,其中 K 是范围的可能值。
有效的方法:这个想法是找到数组元素的起始范围和结束范围的中位数,然后这个中位数的差异将表示每个可能数组的不同中位数。下面是该方法的说明:
- 将所有起始范围值存储在一个数组中。
- 对起始范围的数组进行排序并找到数组的中位数。
- 对于数组的奇数长度 – 中位数 = A[n/2]
- 对于偶数长度的数组 – 中位数 =
- 将所有结束范围值存储在一个数组中。
- 对结束范围的数组进行排序并找到数组的中位数
- 对于奇数长度的数组,中位数 = B[n/2]
- 对于偶数 N ,中位数 =
- 当数组的长度为奇数时,则所有整数值在起始范围中位数到结束范围中位数的范围内相差1。
- 因此,不同中位数的计数将是起始范围的中位数和结束范围的中位数的差值。
- 当数组的长度为偶数时,则所有的整数值相差0.5,在起始范围中位数到结束范围中位数的范围内。
- 范围 [l, r] 中相差 0.5 的所有值与范围 [2 * l, 2 * r] 中相差 1 的所有值相同
- 因此,不同中位数的计数是
下面是上述方法的实现:
C++
// C++ implementation to Count the
// number of distinct medians of an array
// where each array elements
// are given by a range
#include
using namespace std;
#define int long long int
// Function to Count the
// number of distinct medians of an array
// where each array elements
// are given by a range
void solve(int n,
const vector >& vec)
{
vector a, b;
// Loop to store the starting
// and end range in the array
for (auto pr : vec) {
a.push_back(pr.first);
b.push_back(pr.second);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int left, right, ans;
// Condition to check if the
// length of the array is odd
if ((n & 1)) {
left = a[n / 2];
right = b[n / 2];
ans = right - left + 1;
}
else {
left = (a[n / 2] + a[n / 2 - 1]);
right = (b[n / 2] + b[n / 2 - 1]);
ans = right - left + 1;
}
cout << ans << endl;
}
// Driver Code
signed main()
{
int N = 3;
vector > vec =
{ { 100, 100 }, { 10, 10000 },
{ 1, 1000000000 } };
// Function Call
solve(N, vec);
return 0;
}
Java
// Java implementation to count the
// number of distinct medians of an
// array where each array elements
// are given by a range
import java.util.*;
import java.awt.*;
class GFG{
// Function to count the number
// of distinct medians of an array
// where each array elements are
// given by a range
static void solve(int n, ArrayList vec)
{
ArrayList a = new ArrayList<>();
ArrayList b = new ArrayList<>();
// Loop to store the starting
// and end range in the array
for(Point pr : vec)
{
a.add(pr.x);
b.add(pr.y);
}
Collections.sort(a);
Collections.sort(b);
int left, right, ans;
// Condition to check if the
// length of the array is odd
if ((n & 1) != 0)
{
left = a.get(n / 2);
right = b.get(n / 2);
ans = right - left + 1;
}
else
{
left = (a.get(n / 2) +
a.get(n / 2 - 1));
right = (b.get(n / 2) +
b.get(n / 2 - 1));
ans = right - left + 1;
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
ArrayList vec = new ArrayList<>();
vec.add(new Point(100, 100));
vec.add(new Point(10, 10000));
vec.add(new Point(1, 1000000000));
// Function call
solve(N, vec);
}
}
// This code is contributed by jrishabh99
Python3
# Python3 implementation to count the
# number of distinct medians of an array
# where each array elements
# are given by a range
# Function to count the number of
# distinct medians of an array
# where each array elements
# are given by a range
def solve(n, vec):
a = []
b = []
# Loop to store the starting
# and end range in the array
for pr in vec :
a.append(pr[0])
b.append(pr[1])
a.sort()
b.sort()
# Condition to check if the
# length of the array is odd
if ((n & 1)):
left = a[n // 2]
right = b[n // 2]
ans = right - left + 1
else:
left = (a[n // 2] + a[n // 2 - 1])
right = (b[n // 2] + b[n // 2 - 1])
ans = right - left + 1
print(ans)
# Driver Code
if __name__ == "__main__":
N = 3
vec = [ (100, 100), (10, 10000),
(1, 1000000000) ]
# Function Call
solve(N, vec)
# This code is contributed by chitranayal
C#
// C# implementation to count the
// number of distinct medians of
// an array where each array elements
// are given by a range
using System;
using System.Collections;
using System.Collections.Generic;
public class Point
{
public int x, y;
public Point(int xx, int yy)
{
x = xx;
y = yy;
}
}
class GFG{
// Function to count the number
// of distinct medians of an array
// where each array elements are
// given by a range
static void solve(int n, ArrayList vec)
{
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
// Loop to store the starting
// and end range in the array
foreach(Point pr in vec)
{
a.Add(pr.x);
b.Add(pr.y);
}
a.Sort();
b.Sort();
int left, right, ans;
// Condition to check if the
// length of the array is odd
if ((n & 1) != 0)
{
left = (int)a[n / 2];
right = (int)b[n / 2];
ans = right - left + 1;
}
else
{
left = ((int)a[n / 2] +
(int)a[n / 2 - 1]);
right = ((int)b[n / 2] +
(int)b[n / 2 - 1]);
ans = right - left + 1;
}
Console.WriteLine(ans);
}
// Driver code
static public void Main()
{
int N = 3;
ArrayList vec = new ArrayList();
vec.Add(new Point(100, 100));
vec.Add(new Point(10, 10000));
vec.Add(new Point(1, 1000000000));
// Function call
solve(N, vec);
}
}
// This code is contributed by offbeat
Javascript
输出:
9991
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live