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.


Comments

  1. Replies
    1. Hi Lokesh.
      I 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.

      Delete

Post a Comment

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks