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

## View More Similar Problems

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

View Solution →## 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

View Solution →## 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) =

View Solution →## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →