Search This Blog

Tuesday, April 15, 2014

Algorithms - Sorting- Two Arrays

 Is Fibo

The following is the solution to Hacker Rank problem Two Arrays using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 30/30
/**
 *
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author Arun.G
 *
 */

public class Solution{

       /**
        * @param args
        */
       static BufferedReader in = new BufferedReader(new InputStreamReader(
                     System.in));

       public static String isTwoArray(ArrayList<Integer> a, ArrayList<Integer> b,
                     int k) {
              String result = "NO";
              int flag = 0;
              for (int i = 0; i < a.size(); i++) {
                     int numRequired = k - a.get(i);
                     //check if we have the exact required number
                     if (b.contains(numRequired)) {
                           b.remove(b.indexOf(numRequired));
                           result = "YES";
                           continue;
                     } else {
                           //check if we have at-least a number that is greater than or equal to K
                           for (int j = 0; j < b.size(); j++) {

                                  if (Math.abs(b.get(j) + a.get(i)) >= k) {
                                         b.remove(j);
                                         result = "YES";
                                         flag = 1;
                                         break;
                                  } else
                                         flag = 0;
                           }
                           //if flag==0 then it is not a Two Array
                           if (flag == 0) {
                                  result = "NO";
                                  break;
                           }

                     }
              }

              return result;
       }

       public static void main(String[] args) throws NumberFormatException,
                     IOException {
              // TODO Auto-generated method stub
              int T = Integer.parseInt(in.readLine());
              // T Test cases
              for (int i = 0; i < T; i++) {
                     String[] line = in.readLine().split(" ");
                     int N = Integer.parseInt(line[0]);
                     int K = Integer.parseInt(line[1]);
                     ArrayList<Integer> a = new ArrayList<Integer>();
                     ArrayList<Integer> b = new ArrayList<Integer>();
                     // get A array
                     String[] stringArray = in.readLine().split(" ");
                     for (int k = 0; k < stringArray.length; k++) {
                           a.add(Integer.parseInt(stringArray[k]));
                     }
                     // get B array
                     stringArray = in.readLine().split(" ");
                     for (int k = 0; k < stringArray.length; k++) {
                           b.add(Integer.parseInt(stringArray[k]));
                     }
                     Collections.sort(a);
                     Collections.sort(b);

                     System.out.println(isTwoArray(a, b, K));

              }
       }

}

No comments:

Post a Comment

Labels