fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. // size of arr1
  15. int n=sc.nextInt();
  16.  
  17. // size of arr2
  18. int z=sc.nextInt();
  19.  
  20. int arr1[]=new int[n];
  21. int arr2[]=new int[z];
  22.  
  23. for(int i=0;i<n;i++)
  24. arr1[i]=sc.nextInt();
  25.  
  26. for(int i=0;i<z;i++)
  27. arr2[i]=sc.nextInt();
  28.  
  29. Map<Integer,Integer> m=new HashMap<>();
  30.  
  31. for(int ele:arr1)
  32. m.put(ele,m.getOrDefault(ele,0)+1);
  33.  
  34. boolean flag=true;
  35.  
  36. for(int ele:arr2)
  37. {
  38. if(!m.containsKey(ele))
  39. {
  40. flag=false;
  41. break;
  42. }
  43.  
  44. if(m.containsKey(ele))
  45. {
  46. int freq=m.get(ele)-1;
  47. if(freq<=0)
  48. m.remove(ele);
  49. else
  50. m.put(ele,freq);
  51. }
  52. }
  53.  
  54. if(flag)
  55. System.out.println("arr2 is subset of arr1");
  56.  
  57. else
  58. System.out.println("arr2 is not a subset of arr1");
  59. }
  60. }
Success #stdin #stdout 0.13s 56696KB
stdin
6 
3
2 4 7 1 5 5 
5 4 2

stdout
arr2 is subset of arr1