# Find number from given list for which value of the function is closest to A

Given a function **F(n) = P – (0.006 * n)**, where P is given. Given a list of integers and a number, . The task is to find the number from the given list for which the value of the function is closest to .

**Examples**:

Input: P = 12, A = 5 List = {1000, 2000}Output: 1Explanation: Given, P=12, A=5 For 1000, F(1000) is 12 - 1000×0.006 = 6 For 2000, F(2000) is 12 - 2000×0.006 = 0 As the nearest value to 5 is 6, so the answer is 1000.Input: P = 21, A = -11 List = {81234, 94124, 52141}Output: 3

**Approach:** Iterate over each value in the given list and find F(n) for every value. Now, compare the absolute difference of every value of F(n) and A and the value of , for which the absolute difference is minimum is the answer.

Below is the implementation of the above approach:

## C++

`// C++ program to find number from ` `// given list for which value of the ` `// function is closest to A ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find number from ` `// given list for which value of the ` `// function is closest to A ` `int` `leastValue(` `int` `P, ` `int` `A, ` `int` `N, ` `int` `a[]) ` `{ ` ` ` ` ` `// Stores the final index ` ` ` `int` `ans = -1; ` ` ` ` ` `// Declaring a variable to store ` ` ` `// the minimum absolute difference ` ` ` `float` `tmp = (` `float` `)INFINITY; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` ` ` `// Finding F(n) ` ` ` `float` `t = P - a[i] * 0.006; ` ` ` ` ` `// Updating the index of the answer if ` ` ` `// new absolute difference is less than tmp ` ` ` `if` `(` `abs` `(t-A) < tmp) ` ` ` `{ ` ` ` `tmp = ` `abs` `(t - A); ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` `return` `a[ans]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 2, P = 12, A = 2005; ` ` ` `int` `a[] = {1000, 2000}; ` ` ` ` ` `cout << leastValue(P, A, N, a) << endl; ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find number from ` `# given list for which value of the ` `# function is closest to A ` ` ` `# Function to find number from ` `# given list for which value of the ` `# function is closest to A ` `def` `leastValue(P, A, N, a): ` ` ` `# Stores the final index ` ` ` `ans ` `=` `-` `1` ` ` ` ` `# Declaring a variable to store ` ` ` `# the minimum absolute difference ` ` ` `tmp ` `=` `float` `(` `'inf'` `) ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `# Finding F(n) ` ` ` `t ` `=` `P ` `-` `a[i] ` `*` `0.006` ` ` ` ` `# Updating the index of the answer if ` ` ` `# new absolute difference is less than tmp ` ` ` `if` `abs` `(t ` `-` `A) < tmp: ` ` ` `tmp ` `=` `abs` `(t ` `-` `A) ` ` ` `ans ` `=` `i ` ` ` ` ` `return` `a[ans] ` ` ` `# Driver Code ` `N, P, A ` `=` `2` `, ` `12` `, ` `5` `a ` `=` `[` `1000` `, ` `2000` `] ` ` ` `print` `(leastValue(P, A, N, a)) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number from ` `// given list for which value of the ` `// function is closest to A ` ` ` `// Function to find number from ` `// given list for which value of the ` `// function is closest to A ` `function` `leastValue(` `$P` `, ` `$A` `, ` `$N` `, ` `$a` `) ` `{ ` ` ` `// Stores the final index ` ` ` `$ans` `= -1; ` ` ` ` ` `// Declaring a variable to store ` ` ` `// the minimum absolute difference ` ` ` `$tmp` `= PHP_INT_MAX; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ` `$i` `++) ` ` ` `{ ` ` ` `// Finding F(n) ` ` ` `$t` `= ` `$P` `- ` `$a` `[` `$i` `] * 0.006; ` ` ` ` ` `// Updating the index of the ` ` ` `// answer if new absolute ` ` ` `// difference is less than tmp ` ` ` `if` `(` `abs` `(` `$t` `- ` `$A` `) < ` `$tmp` `) ` ` ` `{ ` ` ` `$tmp` `= ` `abs` `(` `$t` `- ` `$A` `); ` ` ` `$ans` `= ` `$i` `; ` ` ` `} ` ` ` `} ` ` ` `return` `$a` `[` `$ans` `]; ` `} ` ` ` `// Driver Code ` `$N` `= 2; ` `$P` `= 12; ` `$A` `= 5; ` `$a` `= ` `array` `(1000, 2000); ` ` ` `print` `(leastValue(` `$P` `, ` `$A` `, ` `$N` `, ` `$a` `)); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1000

## Recommended Posts:

- Python | Find closest number to k in given list
- Find the number closest to n and divisible by m
- Find a triplet in an array whose sum is closest to a given number
- Find the closest and smaller tidy number
- Find the multiple of x which is closest to a^b
- Find if a number is divisible by every number in a list
- Python | Find the closest Key in dictionary
- Find K Closest Points to the Origin
- Find multiple of x closest to or a ^ b (a raised to power b)
- Python program to find second largest number in a list
- Python program to find smallest number in a list
- Python program to find largest number in a list
- Python Program for Find the closest pair from two sorted arrays
- Minimum absolute difference of a number and its closest prime
- Python program to find number of local variables in a function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.