- Related Questions & Answers
- 10 Interesting Python Cool Tricks
- howdoi in Python
- How to print without newline in Python?
- How to download Google Images using Python
- How to clear screen in python?
- get() method for dictionaries in Python
- Generating random number list in Python
- gcd() function Python
- frozenset() in Python
- floor() and ceil() function Python
- float() in Python
- Finding Mean, Median, Mode in Python without Libraries
- Find the k most frequent words from data set in Python
- Find size of a list in Python
- Find length of a string in python (3 ways)

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose in a social group, there are N different people, with unique integer ids from 0 to N-1. Here we have a list of logs, where each logs[i] = [time, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people. Each log is showing the time in which two different people became friends. If A is friends with B, then B is friends with A. Let's say that person A is acquainted with person B if A is friends with B, or A is a friend of someone acquainted with B. We have to find the earliest time for which every person became acquainted with every other person. If no such time has found, then return -1 So if the input is like − [[20190101,0,1], [20190104,3,4], [20190107,2,3], [20190211,1,5], [20190224,2,4], [20190301,0,3], [20190312,1,2], [20190322,4,5]], N = 6, Output will be 20190301. This is because the first event occurs at timestamp = 20190101 and after the person 0 and 1 become friends we have the friendship groups [0,1], [2], [3], [4], [5]. Then the second event occurs at timestamp = 20190104 and after person 3 and 4 become friends we have the following friendship groups [0,1], [2], [3,4], [5]. After that the third event occurs at timestamp = 20190107 and after person 2 and 3 become friends we have the following friendship groups [0,1], [2,3,4], [5]. Then the fourth event occurs at timestamp = 20190211 and after person 1 and 5 become friends we have the following friendship groups [0,1,5], [2,3,4]. There after the fifth event occurs at timestamp = 20190224 and as the person 2 and 4 are already friend anything happens. Finally, the sixth event occurs at timestamp = 20190301 and after person 0 and 3 become friends we have that all become friends.

To solve this, we will follow these steps −

Define a method called find(), this will take a value x, this will work as follows −

if parents[x] is -1, then return x

parents[x] := find(parents[x])

return parents[x]

In the main method, it will work as follows −

Define two arrays called parents and rank, of size N, fill parents with -1, and fill rank with 1s

sort the logs

for each element i in logs −

perform union on i[1] and i[2]

find(i[2]) and find(i[1])

if N is in the rank array, then return i[0]

return -1

Let us see the following implementation to get better understanding −

class Solution { public: int longestSubsequence(vector<int>& arr, int d) { int n = arr.size(); map <int,int> m; int ans = 0; for(int i =0;i<n;i++){ int x = arr[i]; m[x] = 1 + (m[x-d]); ans = max(ans,m[x]); } return ans; } };

[[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]] 6

20190301

Advertisements