SHARE
TWEET

Untitled

a guest Mar 24th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.Arrays;
  7. import java.util.concurrent.*;
  8. import java.util.regex.*;
  9. import java.util.Collections;
  10.  
  11. public class Solution {
  12.  
  13.     // Complete the sockMerchant function below.
  14.     static int sockMerchant(int n, int[] ar) {
  15.        
  16.        
  17.      List<Integer> myArray = new ArrayList<Integer>();
  18. for (int i : ar)
  19. {
  20.     myArray.add(i);
  21. }
  22.    
  23.      int noOfPairs=0;
  24.      for(int i=0;i<myArray.size()-1;i++){
  25.          for(int j=i+1;j<myArray.size();j++){
  26.              if(myArray.get(i)==myArray.get(j) ){
  27.               noOfPairs++;
  28.               myArray.remove(j);
  29.              myArray.remove(i);
  30.              i=0;
  31.              j=0;
  32.              
  33.              }
  34.          
  35.              
  36.              
  37.          }
  38.          
  39.      }
  40.      return noOfPairs;
  41.     }
  42.      
  43.  
  44.    
  45.  
  46.     private static final Scanner scanner = new Scanner(System.in);
  47.  
  48.     public static void main(String[] args) throws IOException {
  49.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  50.  
  51.         int n = scanner.nextInt();
  52.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  53.  
  54.         int[] ar = new int[n];
  55.  
  56.         String[] arItems = scanner.nextLine().split(" ");
  57.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  58.  
  59.         for (int i = 0; i < n; i++) {
  60.             int arItem = Integer.parseInt(arItems[i]);
  61.             ar[i] = arItem;
  62.         }
  63.  
  64.         int result = sockMerchant(n, ar);
  65.  
  66.         bufferedWriter.write(String.valueOf(result));
  67.         bufferedWriter.newLine();
  68.  
  69.         bufferedWriter.close();
  70.  
  71.         scanner.close();
  72.     }
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top