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. int n = sc.nextInt();
  15. int arr1[] = new int[n];
  16. int m = sc.nextInt();
  17. int arr2[] = new int[m];
  18.  
  19. if(m>n){
  20. System.out.println("arr2 is not a subset of arr1");
  21. return;
  22. }
  23.  
  24. HashMap<Integer,Integer> map = new HashMap<>();
  25.  
  26. for(int i=0;i<n;i++){
  27. int val = sc.nextInt();
  28. map.put(val, map.getOrDefault(val,0)+1);
  29. }
  30.  
  31. for(int i=0;i<m;i++){
  32. int val = sc.nextInt();
  33. if(map.getOrDefault(val,0)==0){
  34. System.out.println("arr2 is not a subset of arr1");
  35. return;
  36. }
  37. map.put(map.get(val), map.get(val)-1);
  38. }
  39. System.out.println("arr2 is a subset of arr1");
  40.  
  41. }
  42. }
Success #stdin #stdout 0.15s 56564KB
stdin
6
4
9 3 1 5 2 1
9 1 1 1 
stdout
arr2 is not a subset of arr1