# Longest Subarray With All Even or Odd Elements in Java

An array inputArr[] is given to us that contains non-negative numbers. Our task is to find the length of the longest subarray such that all of the elements of that subarray are either even or odd.

Example: 1

Input:

int arr[] = {5, 5, 3, 7, 9, 7, 0, 1, 2, 7}

Output: 6

Explanation: The subarray {5, 5, 3, 7, 9, 7} is the longest subarray that contains all the odd elements whose length is 6.

Example: 2

Input:

int arr[] = {9, 2, 0, 4, 8, 6, 2, 0, 0, 1, 5, 4, 4, 1, 7}

Output: 8

Explanation: The subarray {2, 0, 4, 8, 6, 2, 0, 0} is the longest subarray that contains all the even elements whose length is 8.

## Naïve Approach

The naïve of the basic approach is to compute all of the subarrays and then check the elements of those subarrays are even or odd. The subarray that contains all the odd or even elements and is the largest among all the subarrays with the given condition is our answer.

Step 1: Create a method findCountLongestSubArr() that accepts the input array.

Step 2: Inside the method, create nested for-loops. These nested for-loops will compute all subarrays.

Step 3: Using a flag isOdd, filter out either odd or even elements of the subarray but not both. The first element of the subarray decides whether the following elements of the subarray are even or odd.

Step 4: Create a variable countEle and ans. The variable countEle counts the valid elements of the subarray, while the ans stores the final answer to the problem.

Step 5: Inside the inner for-loop, check whether the current element is odd or not, along with the first element of the subarray. If the first element and the current element are either odd or even, increment the variable countEle by 1; otherwise, terminate the inner loop using break statement.

Step 6: After the inner loop is terminated, compare the value of the ans and countEle. Store the maximum between these two in the variable ans.

Step 7: In the end, return the variable ans.

## Implementation

Observe the following implementation based on the above steps.

FileName: LongestSubArr.java

Output:

```For the input array:
5 5 3 7 9 7 0 1 2 7
The length of the largest odd or even subarray is: 6

For the input array:
9 2 0 4 8 6 2 0 0 1 5 4 4 1 7
The length of the largest odd or even subarray is: 8
```

Complexity Analysis: Because of the nested for-loop, the time complexity of the program is O(n2), where n is the total number of elements present in the input array. The program is not using any data structure. Thus, the space complexity of the program is constant, i.e., O(1).

With the help of dynamic programming, we can reduce the time complexity of the program from O(n2). The following approach shows the same.

## Approach: Using Dynamic Programming

Step 1: Create an array dpArr[], where dpArr[p] is the length of the subarray terminating at the index p.

Step 2: Assign value 1 to dpArr = 1.

Step 3: Initialize the variable temp as 1 to store the answer.

Step 4: Iterate over the elements of the input array using a loop and do the following in each iteration. Note that iteration should range from (1 to N)

• If(inputArr[p] % 2 == inputArr[p - 1] % 2) then dpArr[p] = dpArr[p - 1] + 1
• Else, dpArr[p] = 1

Step 5: Iterative over the dpArr[] and find the maximum value.

Step 6: Assign the maximum value (found in previous step) to the variable temp.

Step 7: Return the temp.

Observe the following implementation that uses the above-mentioned steps.

FileName: LongestSubArr1.java

Output:

```For the input array:
5 5 3 7 9 7 0 1 2 7
The length of the largest odd or even subarray is: 6

For the input array:
9 2 0 4 8 6 2 0 0 1 5 4 4 1 7
The length of the largest odd or even subarray is: 8
```

Complexity Analysis: The program uses two for-loops. However, they are not nested. Thus, making the time complexity of the program O(n). Also, the program uses using the auxiliary array (dpArr[] in our case), making the space complexity of the program O(n), where n is the total number of elements present in the input array.

We have done the optimization in terms of time complexity. However, it has come at the cost of the auxiliary array making the space complexity O(n). Now, we will do the optimization to reduce the space complexity.

We see that dpArr[p] is only dependent on the dpArr[p - 1] (see the above-written program). Therefore, we can say that the current element of the dpArr[] is only dependent on the last element of the dpArr[] and therefore, we do not need an auxiliary array. We only need a variable to do our work. The illustration of the same is mentioned below.

FileName: LongestSubArr2.java

Output:

```For the input array:
5 5 3 7 9 7 0 1 2 7
The length of the largest odd or even subarray is: 6

For the input array:
9 2 0 4 8 6 2 0 0 1 5 4 4 1 7
The length of the largest odd or even subarray is: 8
```

Complexity Analysis: The time complexity of the program is the same as the previous program. The program is not using any data structure, making the space complexity of the program constant, i.e., O(1).

### Feedback   