find the index of subarray from a parent array Are there any improvements that can be made on the below code, especially in terms of memory and time? I feel bad about setting found = false in every iteration of the for loop.

public static void main(String[] args) {
    System.out.println(getSubArrayIndex(new int[]{2,3,4,2,2,3,5,5,4,2,3,4}, new int[]{2,3,4}));
}

static int getSubArrayIndex(int[] parentArr, int[] subArr) {
    boolean found = false;
    for (int i = 0; i &lt;= parentArr.length - subArr.length; i++) {
        if(parentArr[i] == subArr[0]){
            for(int j = 1 ; j &lt; subArr.length ; j++){
                if(!(parentArr[i+j] == subArr[j])){
                    found = false;
                    break;
                }
                else{
                    found = true;
                }
            }
        }
        if(found)
            return i;
    }
    return -1;
}
Advice 1

Your method is a little bit overdone. Consider this:

public static int getSubArrayIndexV2(int[] haystack, int[] needle) {
    outer:
    for (int i = 0; i &lt;= haystack.length - needle.length; ++i) {
        for (int j = 0; j &lt; needle.length; ++j) {
            if (haystack[i + j] != needle[j]) {
                continue outer;
            }
        }

        return i;
    }

    return -1;
}


Advice 2

This is actually pattern matching in "strings" consisting of integers instead of characters. As such, you can use, say, Knuth-Morris-Pratt algorithm that runs in time \$\mathcal{O}(n + m)\$ (where \$n\$ is the length of the integer array to search in, and \$m\$ is the length of the integer array to search for). The complexity of your solution is worst-case \$\mathcal{O}(nm)\$.

(Take a look at this program.)