My algorithm to find the maximum number of unique integers among all possible contiguous subarrays doesn't work for larger amounts of Integers and subarrays. For instance, I have to read a total amount of 6 Integers from the console and each subarray has a size of 3. So, for this kind of input 5 3 5 2 3 2 my program should print 3 and this works fine. The first subarray stores 5 3 5 so the number of unique Integers is 2. The second subarray stores 3 5 2 so the number of unique Integers is 3. The third subarray would also print 3 because it stores 5 2 3 and so on...

But, it seems like my algorithm can't handle a total amount of 100000 Integers with a subarray size of 99877. Can anyone explain me, what I have done wrong?
FYI: I have to use a Deque implementation like LinkedList or ArrayDeque

for (int i = 0; i < totalAmountOfIntegers; i++) {

    int anyIntegerNumber = consoleInput.nextInt();
    arrayDequeToStoreAllIntegers.addLast(anyIntegerNumber);
    hashSetToStoreUniqueIntegers.add(anyIntegerNumber);

    if (arrayDequeToStoreAllIntegers.size() == sizeOfEachArrayDequeAsSubArray) {

        if (hashSetToStoreUniqueIntegers.size() > quantityOfUniqueIntegersInSubarray) {
            quantityOfUniqueIntegersInSubarray = hashSetToStoreUniqueIntegers.size();
        }

        int firstNumberInDeque = arrayDequeToStoreAllIntegers.remove();
        if (hashSetToStoreUniqueIntegers.size() == sizeOfEachArrayDequeAsSubArray) {
            hashSetToStoreUniqueIntegers.remove(firstNumberInDeque);
        }


    }
}
3

There are 3 answers

3
marvel308 On

The answer would be simply the unique integers in the whole array, since the array is the superset of all subarrays, all numbers would be present in it
Just find how many unique element exist

4
bkis On

To be honest, i don't understand your algorithm. I don't really get what the variables are referring to (although they seem to be named in a semantic way).
But what about this:

import java.util.HashSet;
import java.util.Set;

public class UniqueIntegers {

    public static void main(String[] args) {
        UniqueIntegers ui = new UniqueIntegers();

        Integer[][] integers = {
                {3,5,3,4,6},
                {1,6,3,2,4},
                {2,3,4},
                {3,3,6,9,2}
        };

        Set<Integer> unique = ui.uniqueIntegers(integers);

        System.out.println("Unique Integers: " + unique.size());
        System.out.println("Integers: " + unique);
    }


    private Set<Integer> uniqueIntegers(Integer[][] ints){
        Set<Integer> result = new HashSet<Integer>();

        for (Integer[] iSub : ints){
            for (Integer i : iSub){
                result.add(i);
            }
        }

        return result;
    }

}

It prints:

Unique Integers: 7
Integers: [1, 2, 3, 4, 5, 6, 9]
0
Fahri Can On

After a day of researching, I have found my mistake. My third IF-Statement is wrong. I am comparing, if the size of my HashSet variable is equal to the maximum size of elements each subarray can hold.
Instead, I should compare, if my int variable firstNumberInDeque, which I remove first from my ArrayDeque variable, contains another int variable with the same value. So if this is true, my HashSet variable remains unchanged.
But, if my ArrayDeque variable doesn't contain another int with the same value of firstNumberInDeque than firstNumberInDeque should be removed from my HashSet variable. Here is the right code:

int firstNumberInDeque = arrayDequeToStoreAllIntegers.remove();
if (!arrayDequeToStoreAllIntegers.contains(firstNumberInDeque)) {
    hashSetToStoreUniqueIntegers.remove(firstNumberInDeque);
}