Jemma Smith
Jemma Smith

Reputation: 1

Java to find if an array is a subset of another

Im having trouble to find out if a specific amount of numbers is in another array.The first array generates 10 random numbers and in the second array the user guesses 5 numbers.Im trying to find out if the user guessed any sequences.I used for loops to find out if numbers of user input is in any of the numbers from 1-5 in the array of 10 , if not it will check numbers 2-6 and so on. For example, if the program had the following winning numbers: 23 56 67 06 43 22 59 24 90 66 and user entered: 01 06 43 22 89. I keep getting index out of bounds.How do I fix this ?

    // to check if user guessed a sequence
    
    boolean guessed = false;
    int counter = 0;
    int i , j = 0;
    
    for (i = 4; i < numbers.length; i++) {    // users numbers
        
        for ( j = 4; j < lottery.length; j++) {  // first 5 numbers from array 
            
            if ( lottery[i] == numbers[j]) {
                break;
            }
            if ( j == i) {
                guessed = true;
            }
        }
    }

Upvotes: 0

Views: 75

Answers (1)

Nowhere Man
Nowhere Man

Reputation: 19545

It seems that a method similar to String::indexOf should be implemented in this task for the arrays trying to find an index of a subarray int indexOf(int[] search, int[] input).

Also, it might be needed to look for all possible subarrays of the search subarray (lottery). Thus, the mentioned method should be extended to look for a subrange of the search argument: int indexOf(int[] search, int[] input)

Straightforward implementation would be:

static int indexOf(int search[], int from, int to, int[] input) {
    if (null == search || null == input || search.length > input.length) {
        return -1;
    }
    
    for (int i = 0, n = input.length - (to - from); i <= n; i++) {
        boolean found = true;
        for (int j = from; found && j < to; j++) {
            if (input[i + j - from] != search[j]) {
                found = false;
            }
        }
        if (found) {
            return i;
        }
    }
    return -1;
}

The widths and appropriate indexes from / to of the search subranges can be generated as follows (from the entire length of lottery to 2):

int[] numbers = {23, 56, 67, 06, 43, 22, 59, 24, 90, 66};
int[] lottery = {01, 06, 43, 22, 89};

for (int n = lottery.length; n > 1; n--) {
    for (int m = 0; m <= lottery.length - n; m++) {
        int ix = indexOf(lottery, m, m + n, numbers);
        if (ix > -1) {
            System.out.printf("Found subarray %s, width=%d from: %d to %d ",
                    Arrays.toString(Arrays.copyOfRange(lottery, m, m + n)), n, m, m + n - 1);
            System.out.printf("at index: %d%n", ix);
        }
    }
}

Output

Found subarray [6, 43, 22], width=3 from: 1 to 3 at index: 3
Found subarray [6, 43], width=2 from: 1 to 2 at index: 3
Found subarray [43, 22], width=2 from: 2 to 3 at index: 4

A more efficient implementation would use Knuth - Morris - Pratt algorithm to bypass recurrent checks of the same values in the input array.

Upvotes: 1

Related Questions