给定一个由N个整数和整数K组成的数组arr [] ,任务是找到最接近K的数组元素。如果存在多个最接近的值,则打印最小的一个。
例子:
Input: arr[]={4, 2, 8, 11, 7}, K = 6
Output: 7
Explanation:
The absolute difference between 4 and 6 is |4 – 6| = 2
The absolute difference between 2 and 6 is |2 – 6| = 4
The absolute difference between 8 and 6 is |8– 6| = 2
The absolute difference between 11 and 6 is |11 – 6| = 5
The absolute difference between 7 and 6 is |7 – 6| = 1
Here, the absolute difference between K(=6) and 7 is minimum. Therefore, the closest value of K(=6) is 7
Input: arr[]={100, 200, 400}, K = 300
Output: 200
方法:想法是遍历给定的数组并打印该数组的元素,该元素与给定的整数K给出最小的绝对差。请按照以下步骤解决问题:
- 初始化一个变量,例如res ,以存储最接近K的数组元素的值。
- 遍历数组并比较abs(K – res)的绝对值和abs(K – arr [i])的绝对值。
- 如果abs(K – res)的值超过abs(K – arr [i]) ,则将res更新为arr [i] 。
- 最后,打印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)