Skip to content

Commit 3fa951a

Browse files
authored
Merge pull request #493 from sir-gon/feature/friend_circle_queries
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Friend Circle…
2 parents 50f97d7 + 2885085 commit 3fa951a

File tree

5 files changed

+453
-0
lines changed

5 files changed

+453
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# [Miscellaneous: Friend Circle Queries](https://www.hackerrank.com/challenges/friend-circle-queries)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#Miscellaneous`
5+
6+
The population of HackerWorld is $ 10^9 $.
7+
Initially, none of the people are friends with each other.
8+
In order to start a friendship, two persons `a` and `b` have to shake hands,
9+
where $ 1 \leq a, b \leq 10^9 $.
10+
The friendship relation is transitive,
11+
that is if `a` and `b` shake hands with each other,
12+
`a` and friends of `b` become friends with `b` and friends of `b`.
13+
14+
You will be given `q` queries. After each query,
15+
you need to report the size of the largest
16+
friend circle (the largest group of friends) formed after considering that query.
17+
18+
For example, your list of queries is:
19+
20+
```text
21+
1 2
22+
3 4
23+
2 3
24+
```
25+
26+
First, `1` and `2` shake hands, forming a circle of `2`.
27+
Next, `3` and `4` do the same. Now there are two groups of `2` friends.
28+
When `2` and `3` become friends in the next query,
29+
both groups of friends are added together to make a circle of `4` friends.
30+
We would print
31+
32+
```text
33+
2
34+
2
35+
4
36+
```
37+
38+
## Function Description
39+
40+
Complete the function maxCircle in the editor below.
41+
It must return an array of integers representing
42+
the size of the maximum circle of friends after each query.
43+
44+
maxCircle has the following parameter(s):
45+
46+
- `queries`: an array of integer arrays,
47+
each with two elements indicating a new friendship
48+
49+
## Input Format
50+
51+
The first line contains an integer, `q`, the number of queries to process.
52+
Each of the next `q` lines consists of two space-separated integers
53+
denoting the 2-D array `queries`.
54+
55+
## Constraints
56+
57+
- $ 1 \leq q \leq 10^5 $
58+
- $ 1
59+
\leq
60+
queries[i][0],queries[i][1]
61+
\leq 10^9
62+
$ for
63+
$ 0 \leq i < q $
64+
- $ queries[i][0] \not = queries[i][1] $
65+
66+
## Output Format
67+
68+
Return an integer array of size `q`,
69+
whose value at index is the size of
70+
largest group present after processing the $ i^{th}$ query.
71+
72+
## Sample Input 0
73+
74+
```text
75+
2
76+
1 2
77+
1 3
78+
```
79+
80+
## Sample Output 0
81+
82+
```text
83+
2
84+
3
85+
```
86+
87+
## Explanation 0
88+
89+
In the first query, `1` and `2` shake hands.
90+
So, the size of largest group of friends is `2` (as no other friendships exist).
91+
After the second query, `1`, `2` and `3` all become friends,
92+
as `1` shakes hand with `3`, `2` also become friends with `3`
93+
as he was already a friend of `1`.
94+
95+
## Sample Input 1
96+
97+
```text
98+
4
99+
1000000000 23
100+
11 3778
101+
7 47
102+
11 1000000000
103+
```
104+
105+
## Sample Output 1
106+
107+
```text
108+
2
109+
2
110+
2
111+
4
112+
```
113+
114+
## Explanation 1
115+
116+
After first query, person `1000000000` and person `23` become friends.
117+
So, the largest group size is `2`.
118+
119+
After the second query, person `11` and person `3778` become friends.
120+
So, the largest group size is still .
121+
122+
After the third query, person `7` and person `47` become friends.
123+
Answer is still `2`.
124+
125+
After the last query, person `11` and person `1000000000` become friends,
126+
which means `23`, `11`, `1000000000` and `3778` all become friends.
127+
Hence, the answer now increased to `4`.
128+
129+
## Sample Input 2
130+
131+
```text
132+
6
133+
1 2
134+
3 4
135+
1 3
136+
5 7
137+
5 6
138+
7 4
139+
```
140+
141+
## Sample Output 2
142+
143+
```text
144+
2
145+
2
146+
4
147+
4
148+
4
149+
7
150+
```
151+
152+
## Explanation 2
153+
154+
Friend circles after each iteration:
155+
156+
```text
157+
1 [1,2]
158+
2 [1,2],[3,4]
159+
3 [1,2,3,4]
160+
4 [1,2,3,4],[5,7]
161+
5 [1,2,3,4],[5,7,6]
162+
6 [1,2,3,4,5,6,7]
163+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# [Miscellaneous: Friend Circle Queries](https://www.hackerrank.com/challenges/friend-circle-queries)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#Miscellaneous`
5+
6+
The population of HackerWorld is $ 10^9 $.
7+
Initially, none of the people are friends with each other.
8+
In order to start a friendship, two persons `a` and `b` have to shake hands,
9+
where $ 1 \leq a, b \leq 10^9 $.
10+
The friendship relation is transitive,
11+
that is if `a` and `b` shake hands with each other,
12+
`a` and friends of `b` become friends with `b` and friends of `b`.
13+
14+
You will be given `q` queries. After each query,
15+
you need to report the size of the largest
16+
friend circle (the largest group of friends) formed after considering that query.
17+
18+
For example, your list of queries is:
19+
20+
```text
21+
1 2
22+
3 4
23+
2 3
24+
```
25+
26+
First, `1` and `2` shake hands, forming a circle of `2`.
27+
Next, `3` and `4` do the same. Now there are two groups of `2` friends.
28+
When `2` and `3` become friends in the next query,
29+
both groups of friends are added together to make a circle of `4` friends.
30+
We would print
31+
32+
```text
33+
2
34+
2
35+
4
36+
```
37+
38+
## Function Description
39+
40+
Complete the function maxCircle in the editor below.
41+
It must return an array of integers representing
42+
the size of the maximum circle of friends after each query.
43+
44+
maxCircle has the following parameter(s):
45+
46+
- `queries`: an array of integer arrays,
47+
each with two elements indicating a new friendship
48+
49+
## Input Format
50+
51+
The first line contains an integer, `q`, the number of queries to process.
52+
Each of the next `q` lines consists of two space-separated integers
53+
denoting the 2-D array `queries`.
54+
55+
## Constraints
56+
57+
- $ 1 \leq q \leq 10^5 $
58+
- $ 1
59+
\leq
60+
queries[i][0],queries[i][1]
61+
\leq 10^9
62+
$ for
63+
$ 0 \leq i < q $
64+
- $ queries[i][0] \not = queries[i][1] $
65+
66+
## Output Format
67+
68+
Return an integer array of size `q`,
69+
whose value at index is the size of
70+
largest group present after processing the $ i^{th}$ query.
71+
72+
## Sample Input 0
73+
74+
```text
75+
2
76+
1 2
77+
1 3
78+
```
79+
80+
## Sample Output 0
81+
82+
```text
83+
2
84+
3
85+
```
86+
87+
## Explanation 0
88+
89+
In the first query, `1` and `2` shake hands.
90+
So, the size of largest group of friends is `2` (as no other friendships exist).
91+
After the second query, `1`, `2` and `3` all become friends,
92+
as `1` shakes hand with `3`, `2` also become friends with `3`
93+
as he was already a friend of `1`.
94+
95+
## Sample Input 1
96+
97+
```text
98+
4
99+
1000000000 23
100+
11 3778
101+
7 47
102+
11 1000000000
103+
```
104+
105+
## Sample Output 1
106+
107+
```text
108+
2
109+
2
110+
2
111+
4
112+
```
113+
114+
## Explanation 1
115+
116+
After first query, person `1000000000` and person `23` become friends.
117+
So, the largest group size is `2`.
118+
119+
After the second query, person `11` and person `3778` become friends.
120+
So, the largest group size is still .
121+
122+
After the third query, person `7` and person `47` become friends.
123+
Answer is still `2`.
124+
125+
After the last query, person `11` and person `1000000000` become friends,
126+
which means `23`, `11`, `1000000000` and `3778` all become friends.
127+
Hence, the answer now increased to `4`.
128+
129+
## Sample Input 2
130+
131+
```text
132+
6
133+
1 2
134+
3 4
135+
1 3
136+
5 7
137+
5 6
138+
7 4
139+
```
140+
141+
## Sample Output 2
142+
143+
```text
144+
2
145+
2
146+
4
147+
4
148+
4
149+
7
150+
```
151+
152+
## Explanation 2
153+
154+
Friend circles after each iteration:
155+
156+
```text
157+
1 [1,2]
158+
2 [1,2],[3,4]
159+
3 [1,2,3,4]
160+
4 [1,2,3,4],[5,7]
161+
5 [1,2,3,4],[5,7,6]
162+
6 [1,2,3,4,5,6,7]
163+
```

0 commit comments

Comments
 (0)