Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the closest smaller value of an array

I think what I want is pretty simple but I can't really find the correct solution.

I have this kind of array in Javascript :

[0, 38, 136, 202, 261, 399]

And I get a generated value from 0 to 600 on a button click. What I need is to find the nearest lower value in this array.

For example, if the generated value is 198, I want to get 136 as the result. If the generated value is 300, I want 261... If it's 589, I want 399 etc etc.

Until now, I have tried with this code :

var theArray = [ 1, 3, 8, 10, 13 ];
var goal = 7;
var closest = null;

$.each(theArray, function(){
    if (closest == null || Math.abs(this - goal) < Math.abs(closest - goal)) {
        closest = this;
    }
});

alert(closest);

But it only returns the closest value... Now I need the to get only the closest smaller value for the given number... How can I improve my algorithm to fit my needs?

Thanks!

like image 439
fraxool Avatar asked Nov 26 '17 16:11

fraxool


People also ask

How do I find the nearest smaller element in an array?

A Simple Solution is to use two nested loops. The outer loop starts from the second element, the inner loop goes to all elements on the left side of the element picked by the outer loop and stops as soon as it finds a smaller element.

How do you find the closest value to a number in an array?

Therefore, to find out the closest number we just return the index of the found minimum in the given array indexArr. indexOf(min) .

How do I find the closest number in an array in C?

So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5. We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.

How do you find the nearest number in a range?

Find the closest or nearest number with array formulaSelect a blank cell, and enter below formula, and press the Ctrl + Shift + Enter keys together. Note: In this array formula of {=INDEX(B3:B22,MATCH(MIN(ABS(B3:B22-E2)),ABS(B3:B22-E2),0))}, B3:B22 is the range that you want to find the specific value.

How to find the closest smaller element in an array?

Given an array of integers, find the closest smaller element for every element. If there is no smaller element then print -1 Recommended: Please try your approach on {IDE} first, before moving on to the solution. A simple solution is to run two nested loops. We pick an outer element one by one.

How do you find the closest number in an array?

Find closest number in array. Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: A simple solution is to traverse through the given array and keep track of absolute difference of current element with every element.

Can I find the closest smaller value to a number?

It is much more difficult to find the closest smaller value when your list of numbers is random and not ordered, but it is possible. Let's explore how to do this. If you want to follow along with this tutorial, download the example spreadsheet. In our example below, we have a list of material lengths in column A in the range A2:A13.

How to find the minimum absolution difference of an array?

A simple solution is to traverse through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolution difference. An efficient solution is to use Binary Search .


2 Answers

Reverse the array and use find

let arr = [0, 38, 136, 202, 261, 399];
let val = 300;
let number = arr.reverse().find(e => e <= val);
console.log(number);
like image 198
baao Avatar answered Oct 30 '22 12:10

baao


If you array is sorted, and small enough, a really simple mode to do what you want it's simplly iterate over the array until number > number-in-array then return the number on the previous position.

function getClosestValue(myArray, myValue){
    //optional
    var i = 0;

    while(myArray[++i] < myValue);

    return myArray[--i];
}

Regards.

like image 28
David Solé González Avatar answered Oct 30 '22 12:10

David Solé González