0

How do I find the longest sequence in an array? I have an array of 5 (users input) and array of 10(random lotto numbers 1-100),I want to find the longest sequence the user guessed.For example : lottery numbers are : 10 12 13 15 17 18 19 20 32 65 and user guessed 1 2 15 17 18.This would be a 3 number sequence , I want to find out if the user guessed 2 ,3 , 4 or all numbers.How can I do this ? Here is my code , please help I have been stuck on this for hours.

// to check if user guessed a sequence

int counter=0;
String prize3;
int counter1,counter2,counter3,counter4;

for (int i = 0; i < lottery.length - 5; i++) { // 1-5
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 1; i < lottery.length - 4; i++) { // 2-6
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 2; i < lottery.length - 3; i++) { // 3 -7 numbers of lottery array
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 3; i < lottery.length - 2; i++) { // 4 - 8 numbers of lottery array
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 4; i < lottery.length - 1; i++) { // 5 -9 numbers of lottery array
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 5; i < lottery.length; i++) { // 6 -10 numbers of lottery array
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            counter++;
        }
    }
}

for (int i = 0; i < numbers.length && i < 2; i++) { // first 2 numbers of input
    counter1 = numbers[i];
}

for (int i = 0; i < numbers.length && i < 3; i++) { // first 3 numbers of input
    counter2 = numbers[i];
}

for (int i = 0; i < numbers.length && i < 4; i++) { // first 4 numbers of input
    counter3 = numbers[i];
}

for (int i = 0; i < numbers.length && i < 5; i++) { // all numbers of input
    counter4 = numbers[i];
}

        
5
  • the first 5 for loops represent the lottery numbers , I checked if user guessed numbers 1-5 of the lottery array , then 2-6, 3-7, 4-8, 5-10. Commented Nov 26, 2020 at 16:14
  • Basically you want to find out how many of the five user numbers are in the list of ten random numbers, right? Commented Nov 26, 2020 at 16:26
  • yeah but they have to be guessed in the same ordder Commented Nov 26, 2020 at 16:34
  • Since the two arrays are sorted anyway you must get them in the same order. Commented Nov 26, 2020 at 16:36
  • how do i do that ? On top of this I have to be able to print out a message like if user guessed 1 sequence : " You guessed one sequence you win...." Commented Nov 26, 2020 at 16:45

2 Answers 2

1
int[] numbers = new int[]{10, 12, 13, 15, 17, 18, 19, 20, 32, 65};
int[] lottery = new int[]{1, 2, 15, 17, 18};
int count = 0;
for (int i = 0; i < lottery.length; i++) {
    for (int j = 0; j < numbers.length; j++) {
        if (lottery[i] == numbers[j]) {
            count++;
            break;
        }
    }
}
System.out.printf("You guessed %d sequence.%n", count);
Sign up to request clarification or add additional context in comments.

3 Comments

does this program make sure the numbers are checked in order though ? not just random 2,3 or 4 numbers in the array?
I tried this before but after every sequence guessed it prints out something like this :You guessed 1 sequence you win €1,0000,You guessed 2 sequences you win 25,0000. I want to print out that the user guessed a certain amount of money if they guessed any of the sequences , for example 1 sequence = €10000
@MelissaSpivacenco I humbly beg your pardon, but if you wrote, in your question, how much money each sequence wins then I did not see it. And if you indeed did not write those details in your question, then again I ask your forgiveness but how am I supposed to know those details if you don't write them in your question?
1

Based on your input provided, I'm assuming that the lottery numbers array is always sorted. Under this assumption, it would be very appropriate to apply binary search.

Here is my very simple brute force approach.

    int[] random = {10, 12, 13, 15, 17, 18, 19, 20, 32, 65};
    int[] input = {1,2,15,17,18};
    int cnt=0;
    for(int i=0;i<input.length;i++)
    {
        int present = Arrays.binarySearch(random,input[i]);
        if(present>=0)
        {
            for(int j=present;j< random.length && i<input.length;j++, i++)
            {
                if(random[j]==input[i])
                    cnt++;
            }
        }
        if(cnt>0)
            break;
    }

The variable cnt will output you with the length of the sequence.

5 Comments

I want to add prized though, for example : if user guessed 2 numbers in a row = €10000, if user guessed 3 numbers in a row = €20000
@MelissaSpivacenco You would still be able to do this using the above implementation as you're returning how many numbers in a row were correctly guessed. A Map datastructure may be good for this.
Yeah thats the thing though, Im new at java so I don't really know other methods to use except loops for this, do you know how I might do this with only the things I know? I tried switch statements but doesn't work
Ok so the program works perfectly but to print out the prizes of each sequence I used the switch statement but it doesn't always give me the right values ? i switched 'cnt' and set it from 1-4
@MelissaSpivacenco could you help me understand what exactly you are doing when you say 1-4?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.