Common Elements Geeks For Geeks
TIME O(NLOGN) [GFG TIME : 3.2/7.2]
SPACE O(N)
class Solution{
public static ArrayList<Integer> common_element(ArrayList<Integer>arr1, ArrayList<Integer>arr2)
{
int x=0;
int y=0;
int n=arr1.size();
int m=arr2.size();
Collections.sort(arr1);
Collections.sort(arr2);
ArrayList<Integer> set=new ArrayList<Integer>();
while(x<n && y<m){
if(arr1.get(x)>arr2.get(y)){
y++;
}
else if(arr1.get(x)<arr2.get(y)){
x++;
}
else{
set.add(arr1.get(x));
x++;
y++;
}
}
Collections.sort(set);
//System.out.println(set);
return set;
}
}
Thanks For Reading.
HI Lokesh is it working ?
ReplyDeleteHi Lokesh.
DeleteI think now they have modified it from 2 arrays to 3 arrays because when i solved this question it was for 2 arrays.
Thanks for pointing it out and Don't worry you I will solve this and post the solution.
Stay tuned and keep learning.