- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 a better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: int earliestAcq(vector<vector<int>>& logs, int N) { vector<int> ds (N, -1); sort(begin(logs), end(logs)); for(vector<int> &k : logs) { if(un(k[1], k[2], ds) == N) return k[0]; } return -1; } int un(int u, int v, vector<int> & ds) { u = find(u, ds); v = find(v, ds); if(u != v) { ds[v] += ds[u]; ds[u] = v; } return -ds[v]; } int find(int u, vector<int> & ds) { return ds[u] < 0? u : ds[u] = find(ds[u], ds); } }; main(){ vector<vector<int>> v = { {20190101,0,1},{20190104,3,4},{20190107,2,3},{20190211,1,5}, {20190224,2,4},{20190301,0,3},{20190312,1,2},{20190322,4,5} }; Solution ob; cout <<ob.earliestAcq(v, 6); }

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

- Related Questions & Answers
- Friends Of Appropriate Ages in C++
- Earliest data output time considering TACC in 8085 Microprocessor
- Earliest data output time considering TCE in 8085 Microprocessor
- Earliest data output time considering TOE in 8085 Microprocessor
- What are the three books that everyone should read once in their lifetime?
- How to spend quality time with friends?
- What are the unprofessional habits that make everyone at work hate you?
- What is the easy way to make friends in a new school?
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- WhatsApp Tricks to become INVISIBLE
- What are some facts about forests in India which everyone should know?
- How to become an efficient listener?
- How to become a Chartered Accountant?
- What are the requirements to become a Public Relations Officer?
- Who is the first Indian Lady to become a doctor?

Advertisements