📌  相关文章
📜  使用给定范围的元素计算数组的不同中位数

📅  最后修改于: 2021-06-25 11:12:19             🧑  作者: Mango

给定一个数组arr [] ,该数组表示数组元素的范围,任务是使用这些范围来计数每个可能数组的不同中位数。

例子:

天真的方法:一个简单的解决方案是尝试数组的所有可能值,找到所有这些元素的中值,然后计算数组的不同中值。
时间复杂度:

O(k^{N})

,其中K是范围的可能值。

高效方法:想法是找到数组元素的起始范围和终止范围的中位数,然后该中位数的差将表示每个可能数组的不同中位数。下面是该方法的说明:

  • 将所有起始范围值存储在数组中。
    • 对起始范围的数组进行排序,然后找到数组的中位数。
    • 对于数组的奇数长度–中位数= A [n / 2]
    • 对于均匀的数组长度–中位数=

\frac{A[n/2] + A[n/2 - 1]}{2}

  • 将所有结束范围值存储在数组中。
    • 对结束范围的数组进行排序,然后找到数组的中位数
    • 对于数组的奇数长度,中位数= B [n / 2]
    • 对于偶数N ,中位数=

\frac{B[n/2] + B[n/2 - 1]}{2}

  • 当数组的长度为奇数时,则在开始范围中位数到结束范围中位数的范围内,所有积分值都将相差1。
    • 因此,不同中位数的计数将是开始范围的中位数和结束范围的中位数之差。
  • 当数组的长度为偶数时,则在开始范围中位数到结束范围中位数的范围内,所有积分值都相差0.5。
    • [l,r]范围内所有值相差0.5的值与[2 * l,2 * r]范围内相差1的所有值相同
    • 因此,不同中位数的计数为

2 * right median - 2 * left median + 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


输出:
9991