Advertisement
brilliant_moves

ArrayQuestion.java

Aug 2nd, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.28 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2. import java.util.Arrays;    // to sort array
  3.  
  4. public class ArrayQuestion {
  5.  
  6.     /**
  7.     *   Program: ArrayQuestion
  8.     *   Purpose: Yahoo! Answers
  9.     *   Creator: Chris Clarke
  10.     *   Created: 02.08.2012
  11.     */
  12.  
  13.  
  14. /*
  15. You are given two postive integers n and k.?
  16. Your task is to fill an array of size n such that
  17.  
  18. All elements of A are integers in the range 1,2,...,k.
  19. A is sorted in the increasing order.
  20. Each of the integers 1,2,...,k appears at least once in the array.
  21.  
  22. Write a recursive function to do the following:
  23.  
  24. Report all possible ways of generating the array A.
  25. Return the count of possibilities.
  26. */
  27.  
  28.     static int k, n;
  29.     static int[] theArray;
  30.     static int poss=0;
  31.  
  32.     public static void main(String[] args) {
  33.         Scanner scan = new Scanner(System.in);
  34.  
  35.         System.out.print("Enter array size: ");
  36.         n = scan.nextInt();
  37.  
  38.         theArray = new int[n];
  39.  
  40.         System.out.print("Array range = 1, 2, ..., k.  Enter value for k: ");
  41.         do {
  42.             k = scan.nextInt();
  43.             if (k>n) {
  44.                 System.out.println("k must not be more than array size (" + n + ")");
  45.                 System.out.print("Please re-enter k: ");
  46.             }
  47.         } while (k>n);
  48.    
  49.         int numDoubles = n-k;   // array size - range
  50.  
  51.         int[] addedArray = new int[numDoubles];
  52.  
  53.         for (int i=0; i<numDoubles; i++)
  54.             addedArray[i] = 1;
  55.  
  56.         int pos = numDoubles-1;
  57.        
  58.         poss = getArray(pos, addedArray, true);
  59.  
  60.         System.out.println("Number of possibilities = " + poss);
  61.     }
  62.  
  63.     public static void initialiseArray() {
  64.         int j=1;
  65.         for (int i=0; i<n; i++) {
  66.             if (j<=k) {
  67.                 theArray[i] = j++;
  68.             } else {
  69.                 theArray[i] = 0;
  70.             }
  71.         }
  72.     }
  73.  
  74.     public static int getArray(int pos, int[] anArray, boolean display) {
  75.         // recursive function
  76.         int len = anArray.length;
  77.        
  78.         if (display) {
  79.             initialiseArray();
  80.             for (int i=0; i<len; i++) {
  81.                 theArray[i+k] = anArray[i];
  82.             }
  83.  
  84.             Arrays.sort(theArray);
  85.  
  86.             for (int i=0; i<theArray.length; i++) {
  87.                 System.out.print(theArray[i]+" ");
  88.             }
  89.             System.out.println();
  90.             poss++;
  91.         }
  92.  
  93.         if (k==n) return 1;
  94.  
  95.         if (anArray[pos] < k) {
  96.             anArray[pos]++;
  97.             int temp = anArray[pos];
  98.             while (pos<len-1) {
  99.                 anArray[++pos]=temp;
  100.             }
  101.             getArray(pos, anArray, true);
  102.         } else if (pos>0) {
  103.             getArray(--pos, anArray, false);
  104.         }
  105.  
  106.         return poss;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement