Encuentre el número de la lista dada para el valor de la función más cercano a A

Dada una función F(n) = P – (0.006 * n) , donde se da P. Dada una lista de enteros y un número,  A  . La tarea es encontrar el número de la lista dada para el cual el valor de la función es más cercano a  A
Ejemplos
 

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

Enfoque: iterar sobre cada valor en la lista dada y encontrar F(n) para cada valor. Ahora, compare la diferencia absoluta de cada valor de F(n) y A y el valor de  n  , para el cual la diferencia absoluta es mínima es la respuesta.
A continuación se muestra la implementación del enfoque anterior: 
 

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

Java

// Java program to find number from
// given list for which value of the
// function is closest to A
import java.util.*;
 
class GFG
{
 
// Function to find number from
// given list for which value of the
// function is closest to A
static 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.MAX_VALUE;
 
    for (int i = 0; i < N; i++)
    {
 
        // Finding F(n)
        float t = (float) (P - a[i] * 0.006);
 
        // Updating the index of the answer if
        // new absolute difference is less than tmp
        if (Math.abs(t-A) < tmp)
        {
            tmp = Math.abs(t - A);
            ans = i;
        }
    }
    return a[ans];
}
 
// Driver code
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));
}
}
 
// This code is contributed by 29AjayKumar

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))

C#

// C# program to find number from
// given list for which value of the
// function is closest to A
using System;
     
class GFG
{
 
// Function to find number from
// given list for which value of the
// function is closest to A
static 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.MaxValue;
 
    for (int i = 0; i < N; i++)
    {
 
        // Finding F(n)
        float t = (float) (P - a[i] * 0.006);
 
        // Updating the index of the answer if
        // new absolute difference is less than tmp
        if (Math.Abs(t-A) < tmp)
        {
            tmp = Math.Abs(t - A);
            ans = i;
        }
    }
    return a[ans];
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 2, P = 12, A = 2005;
    int []a = {1000, 2000};
 
    Console.WriteLine(leastValue(P, A, N, a));
}
}
 
// This code is contributed by Rajput-Ji

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
?>

Javascript

<script>
 
// Javascript 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
    let ans = -1;
   
    // Declaring a variable to store
    // the minimum absolute difference
    let tmp = Number.MAX_VALUE;
   
    for (let i = 0; i < N; i++)
    {
   
        // Finding F(n)
        let t =  (P - a[i] * 0.006);
   
        // Updating the index of the answer if
        // new absolute difference is less than tmp
        if (Math.abs(t-A) < tmp)
        {
            tmp = Math.abs(t - A);
            ans = i;
        }
    }
    return a[ans];
}
     
    // Driver code
 
    let N = 2, P = 12, A = 2005;
    let a = [1000, 2000];
   
    document.write(leastValue(P, A, N, a))
     
</script>
Producción: 

1000

 

Complejidad de tiempo: O(N), donde N representa el tamaño de la array.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por indrajit1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *