# Intersecting Lines - Facebook Top Interview Questions

### Problem Statement :

```You are given a two-dimensional list of integers lines and integers lo and hi.

Each element in lines contains [m, b] which represents the line y = mx + b.

Return the number of lines that intersect with at least one other line between x = lo and x = hi, inclusive.

Constraints

0 ≤ n ≤ 100,000 where n is the length of lines

Example 1

Input

lines = [

[2, 3],

[-3, 5],

[4, 6]

]

lo = 0

hi = 1

Output

2

Explanation

Lines [2, 3] and [-3, 5] intersect at x = 0.4, which is between x = 0 and x = 1.

Example 2

Input

lines = [

[-1, 0],

[-1, 1],

[-1, 2],

[-1, 3]

]

lo = 0

hi = 1

Output

0

Explanation

None of the lines intersect anywhere.```

### Solution :

```                        ```Solution in C++ :

void eval(vector<pair<long long, long long>>& ret, vector<pair<long long, long long>>& lines,
int x) {
ret.clear();
for (int i = 0; i < lines.size(); i++) {
ret.emplace_back(lines[i].first * x + lines[i].second, i);
}
sort(ret.begin(), ret.end());
}

int solve(vector<vector<int>>& olines, int lo, int hi) {
vector<pair<long long, long long>> lines;
for (auto& out : olines) lines.emplace_back(out, out);
vector<pair<long long, long long>> lhs, rhs;
eval(lhs, lines, lo);
eval(rhs, lines, hi);
int ret = 0;
map<int, int> dp;
for (int i = 0; i < lhs.size();) {
int j = i + 1;
while (j < lhs.size() && lhs[i].first == lhs[j].first) j++;
for (int k = i; k < j; k++) {
dp[lhs[k].second] = j - 1;
}
i = j;
}
multiset<int> seen;
int reallhs = 0;
for (int i = 0; i < rhs.size();) {
int j = i + 1;
while (j < rhs.size() && rhs[j].first == rhs[i].first) j++;
for (int k = i; k < j; k++) {
seen.insert(dp[rhs[k].second]);
}
if (reallhs + seen.size() - 1 == *seen.rbegin()) {
if (seen.size() > 1) ret += seen.size();
reallhs += seen.size();
seen.clear();
}
i = j;
}
assert(seen.size() == 0);
return ret;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int solve(int[][] lines, int lo, int hi) {
long[][] p = new long[lines.length];
for (int i = 0; i < lines.length; i++) {
p[i] = lines[i] * lo + lines[i];
p[i] = lines[i] * hi + lines[i];
}
Arrays.sort(p, new Comparator<long[]>() {
public int compare(long[] a, long[] b) {
if (a == b)
return Long.compare(b, a);
return Long.compare(a, b);
}
});
long max = p, min = p[p.length - 1];
HashSet<Integer> set = new HashSet();
for (int i = 1; i < p.length; i++) {
if (p[i] <= max)
max = Math.max(max, p[i]);
}
for (int i = p.length - 2; i > -1; i--) {
if (p[i] >= min)
min = Math.min(min, p[i]);
}
return set.size();
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, lines, lo, hi):
segments = [(m * lo + b, m * hi + b, i) for i, (m, b) in enumerate(lines)]
segments.sort()
res = [0 for _ in lines]

cstarting = Counter([a for a, b, i in segments])
for (x, y, i) in segments:
if cstarting[x] > 1:
res[i] = 1

curmax = -(10 ** 10)
prevx = -(10 ** 10)
for (x, y, i) in segments:
if x == prevx:
res[i] = 1
if y <= curmax:
res[i] = 1
curmax = max(curmax, y)
prevx = x

curmin = 10 ** 10
prevx = 10 ** 10

for (x, y, i) in segments[::-1]:
if x == prevx:
res[i] = 1
if y >= curmin:
res[i] = 1
curmin = min(curmin, y)
prevx = x

return sum(res)```
```

## Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

## Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

## Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =