From 6e050794a9501e13b104b3e5496cf9d6f6bf219e Mon Sep 17 00:00:00 2001 From: KillerAV Date: Thu, 1 Oct 2020 09:50:07 +0530 Subject: [PATCH] Create Find Latest Group of Size M - Problem Link: https://leetcode.com/contest/weekly-contest-203/problems/find-latest-group-of-size-m/ - Asked in Leetcode Weekly Contest #203 Problem C. --- .../Find Latest Group of Size M | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 leet_code_problems/Find Latest Group of Size M diff --git a/leet_code_problems/Find Latest Group of Size M b/leet_code_problems/Find Latest Group of Size M new file mode 100644 index 0000000..5274ed4 --- /dev/null +++ b/leet_code_problems/Find Latest Group of Size M @@ -0,0 +1,86 @@ +#include +#define mod 1000000007 +#define ll long long +using namespace std; + +int arr[100001],size_[100001],count_[100001],mark[100001]; +int root(int x) +{ + while(arr[x]!=x) + x=arr[x]; + return x; +} +void func(int x, int y) +{ + int root_x=root(x); + int root_y=root(y); + if(root_x==root_y) + return; + if(size_[root_x]<=size_[root_y]) + { + count_[size_[root_x]]--; + count_[size_[root_y]]--; + size_[root_y]+=size_[root_x]; + size_[root_x]=0; + count_[0]++; + count_[size_[root_y]]++; + arr[root_x]=root_y; + } + else + { + count_[size_[root_x]]--; + count_[size_[root_y]]--; + size_[root_x]+=size_[root_y]; + size_[root_y]=0; + count_[size_[root_x]]++; + arr[root_y]=root_x; + } +} +int findLatestStep(vector& A, int m) { + int N=A.size(); + for(int i=1;i<=N;i++) + { + arr[i]=i; + size_[i]=0; + count_[i]=0; + mark[i]=0; + } + count_[0]=N; + + int ans=-1; + for(int i=0;i=1 && mark[x-1]) + func(x-1,x); + if(x+1<=N && mark[x+1]) + func(x+1,x); + if(count_[m]!=0) + ans=i+1; + } + return ans; +} +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + int T; + cin>>T; + while(T--) + { + int N; + cin>>N; + vector A(N); + for(int i=0;i>A[i]; + int m; + cin>>m; + cout<