Find number from given list for which value of the function is closest to A
Last Updated :
27 May, 2022
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 : 1
Explanation :
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++
#include <bits/stdc++.h>
using namespace std;
int leastValue( int P, int A, int N, int a[])
{
int ans = -1;
float tmp = ( float )INFINITY;
for ( int i = 0; i < N; i++)
{
float t = P - a[i] * 0.006;
if ( abs (t-A) < tmp)
{
tmp = abs (t - A);
ans = i;
}
}
return a[ans];
}
int main()
{
int N = 2, P = 12, A = 2005;
int a[] = {1000, 2000};
cout << leastValue(P, A, N, a) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int leastValue( int P, int A,
int N, int a[])
{
int ans = - 1 ;
float tmp = Float.MAX_VALUE;
for ( int i = 0 ; i < N; i++)
{
float t = ( float ) (P - a[i] * 0.006 );
if (Math.abs(t-A) < tmp)
{
tmp = Math.abs(t - A);
ans = i;
}
}
return a[ans];
}
public static void main(String[] args)
{
int N = 2 , P = 12 , A = 2005 ;
int a[] = { 1000 , 2000 };
System.out.println(leastValue(P, A, N, a));
}
}
|
Python3
def leastValue(P, A, N, a):
ans = - 1
tmp = float ( 'inf' )
for i in range (N):
t = P - a[i] * 0.006
if abs (t - A) < tmp:
tmp = abs (t - A)
ans = i
return a[ans]
N, P, A = 2 , 12 , 5
a = [ 1000 , 2000 ]
print (leastValue(P, A, N, a))
|
C#
using System;
class GFG
{
static int leastValue( int P, int A,
int N, int []a)
{
int ans = -1;
float tmp = float .MaxValue;
for ( int i = 0; i < N; i++)
{
float t = ( float ) (P - a[i] * 0.006);
if (Math.Abs(t-A) < tmp)
{
tmp = Math.Abs(t - A);
ans = i;
}
}
return a[ans];
}
public static void Main(String[] args)
{
int N = 2, P = 12, A = 2005;
int []a = {1000, 2000};
Console.WriteLine(leastValue(P, A, N, a));
}
}
|
PHP
<?php
function leastValue( $P , $A , $N , $a )
{
$ans = -1;
$tmp = PHP_INT_MAX;
for ( $i = 0; $i < $N ; $i ++)
{
$t = $P - $a [ $i ] * 0.006;
if ( abs ( $t - $A ) < $tmp )
{
$tmp = abs ( $t - $A );
$ans = $i ;
}
}
return $a [ $ans ];
}
$N = 2;
$P = 12;
$A = 5;
$a = array (1000, 2000);
print (leastValue( $P , $A , $N , $a ));
?>
|
Javascript
<script>
function leastValue(P, A, N, a)
{
let ans = -1;
let tmp = Number.MAX_VALUE;
for (let i = 0; i < N; i++)
{
let t = (P - a[i] * 0.006);
if (Math.abs(t-A) < tmp)
{
tmp = Math.abs(t - A);
ans = i;
}
}
return a[ans];
}
let N = 2, P = 12, A = 2005;
let a = [1000, 2000];
document.write(leastValue(P, A, N, a))
</script>
|
Time Complexity: O(N), where N represents the size of the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...