📌  相关文章
📜  在任一方向上距 arr[i] D 距离的元素的最小和最大计数

📅  最后修改于: 2022-05-13 01:56:07.253000             🧑  作者: Mango

在任一方向上距 arr[i] D 距离的元素的最小和最大计数

给定一个排序数组arr[]和一个正整数D ,任务是找到位于D与数组元素arr[i]在任一方向上的距离上的数组元素的最小和最大数量,即在范围内[arr[i] – D, arr[i]][arr[i], arr[i] + D]

例子:

方法:可以使用贪心技术解决给定的问题,方法是在每个点的左侧和右侧使用二分搜索来检查距离D范围内可以包含多少点。请按照以下步骤解决问题:

  • 初始化两个变量,比如minmax以存储包含在距离D范围内的最小和最大元素。
  • 迭代数组arr[]并对每个元素执行以下操作:
    1. 初始化变量dist以计算包含在距离D范围内的点数。
    2. arr[i]的左侧执行二分查找,并使用以下步骤在[arr[i] – D, arr[i]]范围内找到一个数字数组元素:
      • 初始化left = 0, right = i – 1并在每次迭代时:
        • 找到mid = (left + right) / 2的值。
        • 如果arr[mid] < arr[i] – D,则将left的值更新为mid + 1 。否则,将dist的值更新为mid并将right的值更新为mid – 1
    3. 根据dist的值更新minmax的值。
    4. arr[i]的左侧执行二分查找,并使用以下步骤查找范围[arr[i], arr[i] + D]内的数组元素的数量:
      • 初始化left = i + 1, right = N – i并且在每次迭代时:
        • 找到mid = (left + right) / 2的值。
        • 如果arr[mid] > arr[i] + D ,则将right的值更新为mid – 1 。否则,将dist的值更新为mid并将left的值更新为mid + 1
    5. 根据dist的值更新minmax的值。
  • 完成上述步骤后,将minmax的值打印为所覆盖的最小和最大点数。

下面是上述方法的实现:

C++
// c++ program for the above approach
#include 
using namespace std;
 
// Function to perform the Binary
// Search to the left of arr[i]
// over the given range
int leftSearch(int arr[], int val, int i)
{
   
    // Base Case
    if (i == 0)
        return 1;
 
    int left = 0, right = i - 1;
    int ind = -1;
 
    // Binary Search for index to left
    while (left <= right) {
 
        int mid = (left + right) / 2;
        if (arr[mid] < val) {
            left = mid + 1;
        }
        else {
            right = mid - 1;
 
            // update index
            ind = mid;
        }
    }
 
    // Return the number of elements
    // by subtracting indices
    return ind != -1 ? i - ind + 1 : 1;
}
 
// Function to perform the Binary
// Search to the right of arr[i]
// over the given range
int rightSearch(int arr[], int val, int i, int N)
{
   
    // Base Case
    if (i == (N - 1))
        return 1;
 
    int left = i + 1;
    int right = N - 1;
    int ind = -1;
 
    // Binary Search for index to right
    while (left <= right) {
 
        int mid = (left + right) / 2;
        if (arr[mid] > val) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
 
            // Update the index
            ind = mid;
        }
    }
 
    // Return the number of elements
    // by subtracting indices
    return ind != -1 ? ind - i + 1 : 1;
}
vector minMaxRange(int arr[], int D, int N)
{
 
    // Stores the minimum and maximum
    // number of points that lies
    // over the distance of D
    int mx = 1, mn = N;
 
    // Iterate the array
    for (int i = 0; i < N; i++) {
 
        // Count of elements included
        // to left of point at index i
        int dist = leftSearch(arr, arr[i] - D, i);
 
        // Update the minimum number
        // of points
        mn = min(mn, dist);
 
        // Update the maximum number
        // of points
        mx = max(mx, dist);
 
        // Count of elements included
        // to right of point at index i
        dist = rightSearch(arr, arr[i] + D, i, N);
 
        // Update the minimum number
        // of points
        mn = min(mn, dist);
 
        // Update the maximum number
        // of points
        mx = max(mx, dist);
    }
 
    // Return the array
    vector v;
    v.push_back(mn);
    v.push_back(mx);
    return v;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 5, 9, 14 };
    int N = 5;
    int D = 4;
    vector minMax = minMaxRange(arr, D, N);
    cout << minMax[0] << " " << minMax[1] << endl;
    return 0;
}
 
// This code is contributed by dwivediyash


Java
// Java program for the above approach
 
import java.io.*;
import java.lang.Math;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum and
    // maximum number of points included
    // in a range of distance D
    public static int[] minMaxRange(
        int[] arr, int D, int N)
    {
 
        // Stores the minimum and maximum
        // number of points that lies
        // over the distance of D
        int max = 1, min = N;
 
        // Iterate the array
        for (int i = 0; i < N; i++) {
 
            // Count of elements included
            // to left of point at index i
            int dist = leftSearch(
                arr, arr[i] - D, i);
 
            // Update the minimum number
            // of points
            min = Math.min(min, dist);
 
            // Update the maximum number
            // of points
            max = Math.max(max, dist);
 
            // Count of elements included
            // to right of point at index i
            dist = rightSearch(
                arr, arr[i] + D, i);
 
            // Update the minimum number
            // of points
            min = Math.min(min, dist);
 
            // Update the maximum number
            // of points
            max = Math.max(max, dist);
        }
 
        // Return the array
        return new int[] { min, max };
    }
 
    // Function to perform the Binary
    // Search to the left of arr[i]
    // over the given range
    public static int leftSearch(
        int[] arr, int val, int i)
    {
        // Base Case
        if (i == 0)
            return 1;
 
        int left = 0, right = i - 1;
        int ind = -1;
 
        // Binary Search for index to left
        while (left <= right) {
 
            int mid = (left + right) / 2;
            if (arr[mid] < val) {
                left = mid + 1;
            }
            else {
                right = mid - 1;
 
                // update index
                ind = mid;
            }
        }
 
        // Return the number of elements
        // by subtracting indices
        return ind != -1 ? i - ind + 1 : 1;
    }
 
    // Function to perform the Binary
    // Search to the right of arr[i]
    // over the given range
    public static int rightSearch(
        int[] arr, int val, int i)
    {
        // Base Case
        if (i == arr.length - 1)
            return 1;
 
        int left = i + 1;
        int right = arr.length - 1;
        int ind = -1;
 
        // Binary Search for index to right
        while (left <= right) {
 
            int mid = (left + right) / 2;
            if (arr[mid] > val) {
                right = mid - 1;
            }
            else {
                left = mid + 1;
 
                // Update the index
                ind = mid;
            }
        }
 
        // Return the number of elements
        // by subtracting indices
        return ind != -1 ? ind - i + 1 : 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 3, 5, 9, 14 };
        int N = arr.length;
        int D = 4;
        int[] minMax = minMaxRange(arr, D, N);
 
        // Function Call
        System.out.print(
            minMax[0] + " " + minMax[1]);
    }
}


Python3
# Python Program to implement
# the above approach
 
# Function to find the minimum and
# maximum number of points included
# in a range of distance D
def minMaxRange(arr, D, N):
 
    # Stores the minimum and maximum
    # number of points that lies
    # over the distance of D
    Max = 1
    Min = N
 
    # Iterate the array
    for i in range(N):
 
        # Count of elements included
        # to left of point at index i
        dist = leftSearch(arr, arr[i] - D, i)
 
        # Update the minimum number
        # of points
        Min = min(Min, dist)
 
        # Update the maximum number
        # of points
        Max = max(Max, dist)
 
        # Count of elements included
        # to right of point at index i
        dist = rightSearch(arr, arr[i] + D, i)
 
        # Update the minimum number
        # of points
        Min = min(Min, dist)
 
        # Update the maximum number
        # of points
        Max = max(Max, dist)
 
    # Return the array
    return [Min, Max]
 
 
# Function to perform the Binary
# Search to the left of arr[i]
# over the given range
def leftSearch(arr, val, i):
    # Base Case
    if (i == 0):
        return 1
 
    left = 0
    right = i - 1
    ind = -1
 
    # Binary Search for index to left
    while (left <= right):
 
        mid = (left + right) // 2
        if (arr[mid] < val):
            left = mid + 1
 
        else:
            right = mid - 1
 
            # update index
            ind = mid
 
    # Return the number of elements
    # by subtracting indices
    return i - ind + 1 if ind != -1 else 1
 
# Function to perform the Binary
# Search to the right of arr[i]
# over the given range
def rightSearch(arr, val, i):
   
    # Base Case
    if (i == len(arr) - 1):
        return 1
 
    left = i + 1
    right = len(arr) - 1
    ind = -1
 
    # Binary Search for index to right
    while (left <= right):
 
        mid = (left + right) // 2
 
        if (arr[mid] > val):
            right = mid - 1
        else:
            left = mid + 1
 
            # Update the index
            ind = mid
 
    # Return the number of elements
    # by subtracting indices
    return ind - i + 1 if ind != -1 else 1
 
# Driver Code
arr = [1, 3, 5, 9, 14]
N = len(arr)
D = 4
minMax = minMaxRange(arr, D, N)
 
# Function Call
print(f"{minMax[0]}  {minMax[1]}")
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
 
class GFG
{
 
    // Function to find the minimum and
    // maximum number of points included
    // in a range of distance D
    public static int[] minMaxRange(int[] arr, int D, int N)
    {
 
        // Stores the minimum and maximum
        // number of points that lies
        // over the distance of D
        int max = 1, min = N;
 
        // Iterate the array
        for (int i = 0; i < N; i++)
        {
 
            // Count of elements included
            // to left of point at index i
            int dist = leftSearch(
                arr, arr[i] - D, i);
 
            // Update the minimum number
            // of points
            min = Math.Min(min, dist);
 
            // Update the maximum number
            // of points
            max = Math.Max(max, dist);
 
            // Count of elements included
            // to right of point at index i
            dist = rightSearch(
                arr, arr[i] + D, i);
 
            // Update the minimum number
            // of points
            min = Math.Min(min, dist);
 
            // Update the maximum number
            // of points
            max = Math.Max(max, dist);
        }
 
        // Return the array
        return new int[] { min, max };
    }
 
    // Function to perform the Binary
    // Search to the left of arr[i]
    // over the given range
    public static int leftSearch(
        int[] arr, int val, int i)
    {
        // Base Case
        if (i == 0)
            return 1;
 
        int left = 0, right = i - 1;
        int ind = -1;
 
        // Binary Search for index to left
        while (left <= right)
        {
 
            int mid = (left + right) / 2;
            if (arr[mid] < val)
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
 
                // update index
                ind = mid;
            }
        }
 
        // Return the number of elements
        // by subtracting indices
        return ind != -1 ? i - ind + 1 : 1;
    }
 
    // Function to perform the Binary
    // Search to the right of arr[i]
    // over the given range
    public static int rightSearch(
        int[] arr, int val, int i)
    {
        // Base Case
        if (i == arr.Length - 1)
            return 1;
 
        int left = i + 1;
        int right = arr.Length - 1;
        int ind = -1;
 
        // Binary Search for index to right
        while (left <= right)
        {
 
            int mid = (left + right) / 2;
            if (arr[mid] > val)
            {
                right = mid - 1;
            }
            else
            {
                left = mid + 1;
 
                // Update the index
                ind = mid;
            }
        }
 
        // Return the number of elements
        // by subtracting indices
        return ind != -1 ? ind - i + 1 : 1;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 3, 5, 9, 14 };
        int N = arr.Length;
        int D = 4;
        int[] minMax = minMaxRange(arr, D, N);
 
        // Function Call
        Console.Write(minMax[0] + " " + minMax[1]);
    }
}
 
// This code is contributed by gfgking.


Javascript


输出:
1 3

时间复杂度: O(N*log N)
辅助空间: O(1)