SHARE
TWEET

Untitled

a guest Nov 13th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class SequenceRec {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int[] input = Arrays.stream(scanner.nextLine().split("\\s+")).
  11.                 mapToInt(Integer::parseInt).toArray();
  12.  
  13.         int number = input[0];
  14.         int target = input[1];
  15.  
  16.         int[] intArray = new int[target];
  17.         intArray[0] = number;
  18.  
  19.         System.out.println(getElement(number, target, intArray, 1, 1));
  20. //        for (Integer elem : intArray) {
  21. //            System.out.print(elem + " ");
  22. //        }
  23.     }
  24.  
  25.     private static int getElement(int k, int n, int[] array, int count, int index) {
  26.  
  27.         if (count >= n) {
  28.             return array[n - 1];
  29.         }
  30.  
  31.         array[count] = k + 1;
  32.  
  33.         if (count + 1 < n) {
  34.             array[count + 1] = (2 * k) + 1;
  35.         }
  36.         if (count + 2 < n) {
  37.             array[count + 2] = k + 2;
  38.         }
  39.  
  40.         return getElement(array[index], n, array, count + 3, index + 1);
  41.     }
  42. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top