📜  未排序数组中最接近 K 的值

📅  最后修改于: 2021-09-04 11:38:05             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是找到最接近K的数组元素。如果存在多个最接近的值,则打印最小的一个。

例子:

方法:这个想法是遍历给定的数组并打印数组的一个元素,该元素给出与给定整数K的最小绝对差异。请按照以下步骤解决问题:

  1. 初始化一个变量,比如res ,以存储最接近K 的值的数组元素。
  2. 遍历数组,比较abs(K – res)的绝对值和abs(K – arr[i])的绝对值。
  3. 如果abs(K – res) 的值超过abs(K – arr[i]) ,则将res更新为arr[i]
  4. 最后,打印res

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get
// the closest value
int clostVal(int arr[],
             int N,
             int K)
{
    // Stores the closest
    // value to K
    int res = arr[0];
 
    // Traverse the array
    for (int i = 1; i < N;
         i++) {
 
        // If absolute difference
        // of K and res exceeds
        // absolute difference of K
        // and current element
        if (abs(K - res) > abs(K - arr[i])) {
            res = arr[i];
        }
    }
 
    // Return the closest
    // array element
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 100, 200, 400 };
    int K = 300;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << clostVal(arr, N, K);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
  
// Function to get
// the closest value
static int clostVal(int arr[], int N,
                    int K)
{
     
    // Stores the closest
    // value to K
    int res = arr[0];
  
    // Traverse the array
    for(int i = 1; i < N; i++)
    {
         
        // If absolute difference
        // of K and res exceeds
        // absolute difference of K
        // and current element
        if (Math.abs(K - res) >
            Math.abs(K - arr[i]))
        {
            res = arr[i];
        }
    }
  
    // Return the closest
    // array element
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 100, 200, 400 };
    int K = 300;
    int N = arr.length;
     
    System.out.print(clostVal(arr, N, K));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to get
# the closest value
def clostVal(arr, N, K):
     
    # Stores the closest
    # value to K
    res = arr[0]
 
    # Traverse the array
    for i in range(1, N, 1):
 
        # If absolute difference
        # of K and res exceeds
        # absolute difference of K
        # and current element
        if (abs(K - res) >
            abs(K - arr[i])):
            res = arr[i]
 
    # Return the closest
    # array element
    return res
 
# Driver Code
arr = [ 100, 200, 400 ]
K = 300
N = len(arr)
 
print(clostVal(arr, N, K))
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program to implement
// the above approach 
using System;
 
class GFG{
  
// Function to get
// the closest value
static int clostVal(int[] arr, int N,
                    int K)
{
     
    // Stores the closest
    // value to K
    int res = arr[0];
  
    // Traverse the array
    for(int i = 1; i < N; i++)
    {
         
        // If absolute difference
        // of K and res exceeds
        // absolute difference of K
        // and current element
        if (Math.Abs(K - res) >
            Math.Abs(K - arr[i]))
        {
            res = arr[i];
        }
    }
  
    // Return the closest
    // array element
    return res;
}
  
// Driver Code
public static void Main ()
{
    int[] arr = { 100, 200, 400 };
    int K = 300;
    int N = arr.Length;
     
    Console.WriteLine(clostVal(arr, N, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
200

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live