Skip to content

Commit 0a3ccce

Browse files
committed
--added: Arrays/SolvingForQueriesWithCups.java
1 parent 00b7632 commit 0a3ccce

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Solving For Queries With Cups
2+
3+
**Problem from HackerRank**
4+
5+
There are `n` cups in a row (assume 1-based indexing for the cups). You have `m` balls and you place the `i`<sup>th</sup> ball in the cup _balls<sub>i</sub>_ such that there's atmost ball in each of the cups, therefore the elements of balls are pairwise distinct.
6+
7+
Now, you perform `s` swaps, each swap contains two integers `a` and `b`, you are required to swap the positions of cups at index `a` and index `b`.
8+
9+
Finally, you are asked `q` queries, each query contains two space separated integers `l` and `r`, you need to answer the total number of balls that are present in the cups having index from `l` to `r` (both inclusive).
10+
11+
For example, `n=4`, `m=2`, `balls={2,4}` . Now you perform `s=3` swaps, where you swap the following cups:
12+
13+
- 2nd and 4th
14+
- 2nd and 1st
15+
- 2nd and 3rd
16+
17+
Finally, the balls would be at the indices `{1,4}` . So, for example, if we turned over all the cups from `l=2` to `r=4`, the number of balls found would be `1`.
18+
19+
**Input Format**
20+
21+
The first line of input contains `4` space separated integers `n`, `m`, `s` and `q` denoting the number of cups, number of balls, number of swaps performed and the number of queries respectively.
22+
23+
The second line of input contains `m` space separated integers ball<sub>1</sub>, ball<sub>2</sub>, ball<sub>3</sub> ... ball<sub>n</sub> where ball<sub>i</sub> denotes the position of ball `i`.
24+
25+
The next `s` lines containts two space separated integer `a` and `b` - the indices of cups needed to be swapped in this query.
26+
27+
The next `q` lines containts two space separated integer `l` and `r`, the description of each query.
28+
29+
**Constraints**
30+
31+
- 2 <= `n` <= 10<sup>9</sup>
32+
- 1 <= `m` <= 10<sup>5</sup>
33+
- 1 <= `s, q` <= 6.10<sup>4</sup>
34+
- 1 <= balls<sub>i</sub>, `a`, `b` <= `n`, `a` != `b`
35+
- 1 <= `l` <= `r` <= `n`
36+
37+
**Output Format**
38+
39+
Print `q` space separated integers, where the `i`<sup>th</sup> integer denote the answer for the `i`<sup>th</sup> query.
40+
41+
## Examples:
42+
43+
### Example 1
44+
45+
**Sample Input** :
46+
47+
```java
48+
3 1 3 2
49+
2
50+
1 2
51+
1 3
52+
3 1
53+
1 2
54+
1 3
55+
```
56+
57+
**Sample Output** :
58+
59+
```java
60+
1 1
61+
```
62+
63+
**Explanation**: After the given swaps, the ball will be at position **1**, hence, for both the queries, the answer is **1**.
64+
65+
### Example 2
66+
67+
**Sample Input** :
68+
69+
```java
70+
3 2 2 2
71+
1 3
72+
1 3
73+
3 2
74+
1 2
75+
3 3
76+
```
77+
78+
**Sample Output** :
79+
80+
```java
81+
2 0
82+
```
83+
84+
**Explanation**: After the given swaps, the balls would be at positions `{1,2}`. Hence, for the first query the answer would be **2** (There are **2** balls in the range `[1,2]`) and for the second query, the answer would be **0** (There are no balls in the cup **3**).
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import java.util.*;
2+
3+
public class SolvingForQueriesWithCups {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
int m = sc.nextInt();
10+
int s = sc.nextInt();
11+
int q = sc.nextInt();
12+
13+
List<Integer> balls = new ArrayList<>();
14+
15+
for (int i = 0; i < m; i++) {
16+
balls.add(sc.nextInt());
17+
}
18+
19+
List<List<Integer>> swaps = new ArrayList<>();
20+
21+
for (int i = 0; i < s; i++) {
22+
List<Integer> swapsRowItems = new ArrayList<>();
23+
24+
for (int j = 0; j < 2; j++) {
25+
swapsRowItems.add(sc.nextInt());
26+
}
27+
swaps.add(swapsRowItems);
28+
}
29+
30+
List<List<Integer>> query = new ArrayList<>();
31+
32+
for (int i = 0; i < q; i++) {
33+
List<Integer> queryRowItems = new ArrayList<>();
34+
35+
for (int j = 0; j < 2; j++) {
36+
queryRowItems.add(sc.nextInt());
37+
}
38+
39+
query.add(queryRowItems);
40+
}
41+
42+
List<Integer> result = countCups(n, balls, swaps, query);
43+
44+
for (int i = 0; i < result.size(); i++) {
45+
System.out.print(String.valueOf(result.get(i)));
46+
47+
if (i != result.size() - 1) {
48+
System.out.print(" ");
49+
}
50+
}
51+
52+
System.out.println();
53+
}
54+
55+
public static List<Integer> countCups(int n, List<Integer> balls, List<List<Integer>> swaps, List<List<Integer>> queries) {
56+
57+
List<Integer> ansList = new ArrayList<>();
58+
Set<Integer> set = new HashSet<>(balls);
59+
TreeMap<Integer, Integer> map = new TreeMap<>();
60+
61+
for(int i=0; i<swaps.size(); i++) {
62+
int x = swaps.get(i).get(0);
63+
int y = swaps.get(i).get(1);
64+
65+
boolean xC = set.contains(x);
66+
boolean yC = set.contains(y);
67+
68+
if(xC && yC || !xC && !yC) {
69+
continue;
70+
}
71+
72+
if(xC) {
73+
set.remove(x);
74+
set.add(y);
75+
} else {
76+
set.remove(y);
77+
set.add(x);
78+
}
79+
}
80+
81+
for(int x : set) {
82+
map.put(x, 0);
83+
}
84+
85+
map.put(0,0);
86+
int count = 0;
87+
88+
for(int x : map.keySet()) {
89+
count += 1;
90+
map.put(x, count);
91+
}
92+
93+
for(int i=0; i<queries.size(); i++) {
94+
int x = queries.get(i).get(0)-1;
95+
int y = queries.get(i).get(1);
96+
97+
Integer cLeft = 0;
98+
Integer cRight = 0;
99+
100+
cLeft = map.get(x);
101+
cRight = map.get(y);
102+
103+
if(cLeft == null) {
104+
cLeft = map.get(map.floorKey(x));
105+
}
106+
107+
if(cRight == null) {
108+
cRight = map.get(map.floorKey(y));
109+
}
110+
111+
ansList.add(cRight-cLeft);
112+
}
113+
114+
return ansList;
115+
}
116+
117+
}
118+

0 commit comments

Comments
 (0)