0

i am using JS Array

var fruits = [['5','7'],['10','20'],['20','30']];

function myFunction() {
    // want to check Here
    fruits.push(['30', '40']); // allow
    fruits.push(['7', '10']); // allow
    fruits.push(['10', '12']); // Should not allow
    fruits.push(['5', '35']); // Should not allow
}

i want to check and allow the Elements in array.

8
  • 2
    At what exact condition should an element be accepted or not? I am confused Commented Dec 1, 2016 at 8:01
  • if i push (['30', '40']); it should allow, if i push (['5', '35']); should not allow. Commented Dec 1, 2016 at 8:01
  • is the fruit array sorted? Commented Dec 1, 2016 at 8:03
  • 3
    No jokes, I can read a question. Why is [30, 40] allowed and not [5, 35]? I understand you want to get min and max of all the numbers in the array of arrays, and the pushed element must have values outside of min and max. Correct? Commented Dec 1, 2016 at 8:03
  • its not Related to Min and Max, for any interval, Let me Update my question Commented Dec 1, 2016 at 8:06

2 Answers 2

4

With unsorted data, you could check every border and push array if no overlapping occurs.

function push(array) {
    return fruits.every(function(a) {
        return array[1] <= a[0] || a[1] <= array[0];
    }) && fruits.push(array) && true;
}

var fruits = [[10, 15], [25, 30]];

console.log(push([5, 35]));  // false
console.log(push([30, 40])); // true
console.log(push([15, 25])); // true
console.log(push([5, 10]));  // true
console.log(fruits);

Sign up to request clarification or add additional context in comments.

5 Comments

Can you check this solution? After var fruits = [[10, 15], [25, 30]]; I am getting true for push([5,35]).
Also a fun problem for golfing: push=a=>fruits.every(b=>(a[1]<=b[0]||b[1]<=a[0]))&&fruits.push(a)&&true
I have a Keys like 0,1,2... how can i work with this
@AbdulWaheed, what do you mean with "Keys like 0,1,2"?
@NinaScholz, Yes, i have Multiple Row, for difference row, values might be Same, i have to Combine the solution with Key.
1

I'd suggest going this route:

class IntervalList {
  constructor() {
    this.data = [];
  }
  add(low, high) {
    if (low > high) {
      [low, high] = [high, low];
    }
    for (let [x,y] of this.data) {
      if (x < low) {
        if (y > low) return false;
      } else if (x > low) {
        if (x < high) return false;
      }
    }
    this.data.push([low, high]);
    return true;
  } 
}

Then you say things like

let c = new IntervalList();
c.add(10, 20);
c.add(20, 30);
c.add(5,7);
c.add(3,5);
c.add(9,7)

Let the data structure maintain itself.

Note that what you want to do is tricky. Some people might erroneously think you that to check if (p,q) overlaps (r,s) you just have to check whether p is between r and s or q is between r and s. THIS IS NOT TRUE. Because if the values were arranged "p r s q" that algorithm would fail. Note how the above algorithm looks weird but it takes care of that case. That is why the problem is interesting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.