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

📅  最后修改于: 2021-05-07 00:08:45             🧑  作者: 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


输出:
200









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