Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. /*
  2. https://www.codewars.com/kata/twice-linear/train/java
  3. */
  4.  
  5. public class Main {
  6.     public static int dblLinear (int n) {
  7.         LinkedList<Integer> sequence = new LinkedList<>();
  8.         sequence.add(1);
  9.         for (int i = 0; i < n; i++) {
  10.             int x = sequence.get(i);
  11.             int y = x * 2 + 1;
  12.             int z = x * 3 +1;
  13.  
  14.             addToSortedList(sequence, y);
  15.             addToSortedList(sequence, z);
  16.         }
  17.  
  18. //        System.out.println(sequence);
  19. //        // check for duplicates
  20. //        for (int i = 0; i < sequence.size() - 1; i++) {
  21. //            if (sequence.get(i).equals(sequence.get(i + 1))) {
  22. //                System.out.println("ERROR FOUND: " + sequence.get(i));
  23. //            }
  24. //        }
  25.  
  26.         return sequence.get(n);
  27.     }
  28.  
  29.     private static <T extends Comparable> int addToSortedList (LinkedList<T> list, T val) {
  30.         if (list.size() == 0) {
  31.             list.add(val);
  32.             return 0;
  33.         }
  34.  
  35.         if (val.compareTo(list.get(list.size()-1)) == 1) {
  36.             list.add(val);
  37.             return list.size() - 1;
  38.         }
  39.  
  40.         int lowerBound = 0;
  41.         int upperBound = list.size()-1;
  42.         while (true) {
  43.             if (upperBound <= lowerBound) {  // have we found the proper position yet?
  44.                 if (val.compareTo(list.get(lowerBound)) == 1) {
  45.                     list.add(lowerBound + 1, val);
  46.                     return lowerBound + 1;
  47.                 }
  48.                 else if (val.compareTo(list.get(lowerBound)) == -1) {
  49.                     list.add(lowerBound, val);
  50.                     return lowerBound;
  51.                 }
  52.                 else {
  53. //                  list.add(lowerBound, val);  // ! uncomment if you want to add DUPLICATES to the list !
  54.                     return lowerBound;
  55.                 }
  56.             }
  57.             // compare value to the middle element of the [lowerBound, upperBound] range
  58.             int midPoint = lowerBound + (upperBound - lowerBound) / 2;
  59.             int compResult = val.compareTo(list.get(midPoint));
  60.             if (compResult == 0) {          // check if value equals list[midPoint]
  61. //              list.add(midPoint, val);    // ! uncomment if you want to add DUPLICATES to the list !
  62.                 return midPoint;            // !
  63.  
  64.             }
  65.             else if (compResult == -1) {    // check if value is lesser than list[midPoint]
  66.                 upperBound = midPoint - 1;
  67.             }
  68.             else {                          // check if value is greater than list[midPoint]
  69.                 lowerBound = midPoint + 1;
  70.             }
  71.         }
  72.     }
  73.  
  74.     public static void main(String[] args) {       
  75.         System.out.println(dblLinear(10));          // should be 22
  76.         System.out.println(dblLinear(20));          // should be 57
  77.         System.out.println(dblLinear(30));          // should be 91
  78.         System.out.println(dblLinear(50));          // should be 175
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement