📌  相关文章
📜  在 [0, N – 1] 范围内所有可能的 K 值的 (K – arr[i]) 的最小绝对值

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

在 [0, N – 1] 范围内所有可能的 K 值的 (K – arr[i]) 的最小绝对值

给定一个正整数N和一个由M个整数组成的排序数组arr[] ,任务是在[0, N – 1范围内为所有可能的K值找到(K – arr[i])的最小绝对值] .

例子:

方法:给定的问题可以通过从数组中选择刚好大于或小于当前值K的值来解决。请按照以下步骤解决问题:

  • 初始化一个变量,比如ind0 ,以及一个变量,比如prevarr[0] ,它存储先前分配的值。
  • 使用变量K迭代范围[0, N – 1]并执行以下步骤:
    • 初始化一个变量,比如存储(K – arr[i])的最小绝对值的距离
    • 如果i的值小于arr[0] ,则将距离的值更新为(arr[0] – i)
    • 否则,如果i的值至少为 prev(ind + 1)的值小于Mi的值至多为 arr[ind + 1] ,则执行以下步骤:
      • 距离值更新为(i – prev)(arr[ind + 1] – i)的最小值。
      • 如果i的值等于arr[ind + 1] ,则将distance的值更新为0 ,将prev更新为arr[ind + 1] ,并将ind的值增加1
    • 如果i的值大于prev ,则将距离值更新为(i – prev)
    • 完成上述步骤后,将距离值打印为当前 K 值的 ( K – arr[i])的最小绝对值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum absolute
// value of (K - arr[i]) for each possible
// value of K over the range [0, N - 1]|
void minimumDistance(vector arr,
                     int N)
{
    int ind = 0;
    int prev = arr[ind];
    int s = arr.size();
 
    // Traverse the given array arr[]
    for (int i = 0; i < N; i++) {
 
        // Stores the mininimum distance
        // to any array element arr[i]
        int distance = INT_MAX;
 
        // Check if there is no safe
        // position smaller than i
        if (i < arr[0]) {
            distance = arr[0] - i;
        }
 
        // Check if the current position
        // is between two safe positions
        else if (i >= prev && ind + 1 < s
                 && i <= arr[ind + 1]) {
 
            // Take the minimum of two
            // distances
            distance = min(i - prev,
                           arr[ind + 1] - i);
 
            // Check if the current index
            // is a safe position
            if (i == arr[ind + 1]) {
                distance = 0;
                prev = arr[ind + 1];
                ind++;
            }
        }
 
        // Check if there is no safe
        // position greater than i
        else {
            distance = i - prev;
        }
 
        // Print the minimum distance
        cout << distance << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    vector arr = { 0, 4 };
    minimumDistance(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
    // Functinion to find the minimum absolute
    // value of (K - arr[i]) for each possible
    // value of K over the range [0, N - 1]|
    public static void minimumDistance(int arr[],
                         int N)
    {
        int ind = 0;
        int prev = arr[ind];
        int s = arr.length;
     
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
     
            // Stores the mininimum distance
            // to any array element arr[i]
            int distance = Integer.MAX_VALUE;
     
            // Check if there is no safe
            // position smaller than i
            if (i < arr[0]) {
                distance = arr[0] - i;
            }
     
            // Check if the current position
            // is between two safe positions
            else if (i >= prev && ind + 1 < s
                     && i <= arr[ind + 1]) {
     
                // Take the minimum of two
                // distances
                distance = Math.min(i - prev,
                               arr[ind + 1] - i);
     
                // Check if the current index
                // is a safe position
                if (i == arr[ind + 1]) {
                    distance = 0;
                    prev = arr[ind + 1];
                    ind++;
                }
            }
     
            // Check if there is no safe
            // position greater than i
            else {
                distance = i - prev;
            }
             
            // Print the minimum distance
            System.out.print(distance+" ");
        }
    }
 
    // driver code
    public static void main (String[] args) {
        int N = 5;
        int arr[] = { 0, 4 };
        minimumDistance(arr, N);
    }
}
 
// This code is contributed by Manu Pathria


Python3
# Python program for the above approach
 
 
# Function to find the minimum absolute
# value of (K - arr[i]) for each possible
# value of K over the range [0, N - 1]|
def minimumDistance(arr, N):
    ind = 0;
    prev = arr[ind];
    s = len(arr);
 
    # Traverse the given array arr[]
    for i in range(N):
 
        # Stores the mininimum distance
        # to any array element arr[i]
        distance = 10**9;
 
        # Check if there is no safe
        # position smaller than i
        if (i < arr[0]):
            distance = arr[0] - i;
 
        # Check if the current position
        # is between two safe positions
        elif (i >= prev and ind + 1 < s
            and i <= arr[ind + 1]):
 
            # Take the minimum of two
            # distances
            distance = min(i - prev, arr[ind + 1] - i);
 
            # Check if the current index
            # is a safe position
            if (i == arr[ind + 1]):
                distance = 0;
                prev = arr[ind + 1];
                ind += 1;
 
        # Check if there is no safe
        # position greater than i
        else:
            distance = i - prev;
 
        # Print the minimum distance
        print(distance, end=" ");
 
# Driver Code
 
N = 5;
arr = [0, 4];
minimumDistance(arr, N);
 
# This code is contributed by _saurabh_jaiswal.


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Functinion to find the minimum absolute
    // value of (K - arr[i]) for each possible
    // value of K over the range [0, N - 1]|
    public static void minimumDistance(int []arr,
                         int N)
    {
        int ind = 0;
        int prev = arr[ind];
        int s = arr.Length;
     
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
     
            // Stores the mininimum distance
            // to any array element arr[i]
            int distance = Int32.MaxValue;
     
            // Check if there is no safe
            // position smaller than i
            if (i < arr[0]) {
                distance = arr[0] - i;
            }
     
            // Check if the current position
            // is between two safe positions
            else if (i >= prev && ind + 1 < s
                     && i <= arr[ind + 1]) {
     
                // Take the minimum of two
                // distances
                distance = Math.Min(i - prev,
                               arr[ind + 1] - i);
     
                // Check if the current index
                // is a safe position
                if (i == arr[ind + 1]) {
                    distance = 0;
                    prev = arr[ind + 1];
                    ind++;
                }
            }
     
            // Check if there is no safe
            // position greater than i
            else {
                distance = i - prev;
            }
             
            // Print the minimum distance
            Console.Write(distance+" ");
        }
    }
 
    // driver code
    public static void Main (string[] args) {
        int N = 5;
        int []arr = { 0, 4 };
        minimumDistance(arr, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
0 1 2 1 0

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