给定一个由N 个非负整数组成的数组A[] ,任务是找到最长子数组的长度,使得该子数组中的所有元素都是奇数或偶数。
例子:
Input: A[] = {2, 5, 7, 2, 4, 6, 8, 3}
Output: 4
Explanation: Sub-array {2, 4, 6, 8} of length 4 has all even elements
Input: A[] = {2, 3, 2, 5, 7, 3}
Output: 3
Explanation: Sub-array {5, 7, 3} of length 3 has all odd elements
朴素的方法:解决这个问题的朴素的方法是考虑所有连续的子数组,对于每个子数组,检查所有元素是偶数还是奇数。其中最长的将是答案。
时间复杂度: O(N^2)
辅助空间: O(1)
高效的方法:解决这个问题的主要思想是使用动态规划(它具有两个属性——最优子结构和重叠子问题),如果有一些连续的奇数元素,那么下一个奇数元素会增加那个的长度一个连续的数组。对于偶数元素也是如此。请按照以下步骤解决问题:
- 初始化数组dp[ ] ,其中dp[i]存储以A[i]结尾的子数组的长度。
- 用1初始化dp[0] 。
- 将变量ans初始化为1以存储答案。
- 使用变量i迭代范围[1, N]并执行以下步骤:
- 如果A[i]%2等于A[i-1]%2,则设置dp[i]的值为dp[i-1]+1。
- 否则,将dp[i]的值设置为1。
- 使用变量i在范围[0, N] 上迭代并执行以下步骤:
- 将ans的值设置为ans或dp[i]的最大值。
- 执行完上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate longest substring
// with odd or even elements
int LongestOddEvenSubarray(int A[], int N)
{
// Initializing dp[]
int dp[N];
// Initializing dp[0] with 1
dp[0] = 1;
// ans will store the final answer
int ans = 1;
// Traversing the array from index 1 to N - 1
for (int i = 1; i < N; i++) {
// Checking both current and previous element
// is even or odd
if ((A[i] % 2 == 0 && A[i - 1] % 2 == 0)
|| (A[i] % 2 != 0 && A[i - 1] % 2 != 0)) {
// Updating dp[i] with dp[i-1] + 1
dp[i] = dp[i - 1] + 1;
}
else
dp[i] = 1;
}
for (int i = 0; i < N; i++)
// Storing max element to ans
ans = max(ans, dp[i]);
// Returning the final answer
return ans;
}
// Driver Code
int main()
{
// Input
int A[] = { 2, 5, 7, 2, 4, 6, 8, 3 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << LongestOddEvenSubarray(A, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate longest substring
// with odd or even elements
static int LongestOddEvenSubarray(int A[], int N)
{
// Initializing dp[]
int dp[] = new int[N];
// Initializing dp[0] with 1
dp[0] = 1;
// ans will store the final answer
int ans = 1;
// Traversing the array from index 1 to N - 1
for (int i = 1; i < N; i++) {
// Checking both current and previous element
// is even or odd
if ((A[i] % 2 == 0 && A[i - 1] % 2 == 0)
|| (A[i] % 2 != 0 && A[i - 1] % 2 != 0)) {
// Updating dp[i] with dp[i-1] + 1
dp[i] = dp[i - 1] + 1;
}
else
dp[i] = 1;
}
for (int i = 0; i < N; i++)
// Storing max element to ans
ans = Math.max(ans, dp[i]);
// Returning the final answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Input
int A[] = { 2, 5, 7, 2, 4, 6, 8, 3 };
int N = A.length;
// Function call
System.out.println(LongestOddEvenSubarray(A, N));
}
}
// This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate longest substring
// with odd or even elements
static int LongestOddEvenSubarray(int[] A, int N)
{
// Initializing dp[]
int[] dp = new int[N];
// Initializing dp[0] with 1
dp[0] = 1;
// ans will store the final answer
int ans = 1;
// Traversing the array from index 1 to N - 1
for (int i = 1; i < N; i++) {
// Checking both current and previous element
// is even or odd
if ((A[i] % 2 == 0 && A[i - 1] % 2 == 0)
|| (A[i] % 2 != 0 && A[i - 1] % 2 != 0)) {
// Updating dp[i] with dp[i-1] + 1
dp[i] = dp[i - 1] + 1;
}
else
dp[i] = 1;
}
for (int i = 0; i < N; i++)
// Storing max element to ans
ans = Math.Max(ans, dp[i]);
// Returning the final answer
return ans;
}
// Driver Code
public static void Main()
{
// Input
int[] A = { 2, 5, 7, 2, 4, 6, 8, 3 };
int N = A.Length;
// Function call
Console.Write(LongestOddEvenSubarray(A, N));
}
}
// This code is contributed by target_2.
Python3
# Python 3 implementation for the above approach
# Function to calculate longest substring
# with odd or even elements
def LongestOddEvenSubarray(A, N):
# Initializing dp[]
dp = [0 for i in range(N)]
# Initializing dp[0] with 1
dp[0] = 1
# ans will store the final answer
ans = 1
# Traversing the array from index 1 to N - 1
for i in range(1, N, 1):
# Checking both current and previous element
# is even or odd
if ((A[i] % 2 == 0 and A[i - 1] % 2 == 0) or (A[i] % 2 != 0 and A[i - 1] % 2 != 0)):
# Updating dp[i] with dp[i-1] + 1
dp[i] = dp[i - 1] + 1
else:
dp[i] = 1
for i in range(N):
# Storing max element to ans
ans = max(ans, dp[i])
# Returning the final answer
return ans
# Driver Code
if __name__ == '__main__':
# Input
A = [2, 5, 7, 2, 4, 6, 8, 3]
N = len(A)
# Function call
print(LongestOddEvenSubarray(A, N))
# This code is contributed by SURENDRA_GANGWAR.
Javascript
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate longest substring
// with odd or even elements
int LongestOddEvenSubarray(int A[], int N)
{
// Initializing dp
int dp;
// Initializing dp with 1
dp = 1;
// ans will store the final answer
int ans = 1;
// Traversing the array from index 1 to N - 1
for (int i = 1; i < N; i++) {
// Checking both current and previous element
// is even or odd
if ((A[i] % 2 == 0 && A[i - 1] % 2 == 0)
|| (A[i] % 2 != 0 && A[i - 1] % 2 != 0)) {
// Updating dp with (previous dp value) + 1
dp = dp + 1;
// Storing max element so far to ans
ans = max(ans, dp);
}
else
dp = 1;
}
// Returning the final answer
return ans;
}
// Driver code
int main()
{
// Input
int A[] = { 2, 5, 7, 2, 4, 6, 8, 3 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << LongestOddEvenSubarray(A, N);
return 0;
}
4
时间复杂度: O(N)
辅助空间: O(N)
空间优化:通过观察,对于计算dp[i] ,只有dp[i-1] 的值是相关的,可以进一步优化上述方法的空间复杂度。因此,将dp[i-1]存储在一个变量中并在每次迭代中更新该变量。此外,在每次迭代中更新答案。请按照以下步骤解决问题:
- 将变量dp初始化为1以存储子数组的长度直到i-1 ,将ans初始化为1以存储答案。
- 使用变量i迭代范围[1, N]并执行以下步骤:
- 如果A[i]%2等于A[i-1]%2,则将dp的值设为dp+1 ,将ans的值设为ans或dp的最大值。
- 否则,将dp的值设置为1。
- 执行完上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate longest substring
// with odd or even elements
int LongestOddEvenSubarray(int A[], int N)
{
// Initializing dp
int dp;
// Initializing dp with 1
dp = 1;
// ans will store the final answer
int ans = 1;
// Traversing the array from index 1 to N - 1
for (int i = 1; i < N; i++) {
// Checking both current and previous element
// is even or odd
if ((A[i] % 2 == 0 && A[i - 1] % 2 == 0)
|| (A[i] % 2 != 0 && A[i - 1] % 2 != 0)) {
// Updating dp with (previous dp value) + 1
dp = dp + 1;
// Storing max element so far to ans
ans = max(ans, dp);
}
else
dp = 1;
}
// Returning the final answer
return ans;
}
// Driver code
int main()
{
// Input
int A[] = { 2, 5, 7, 2, 4, 6, 8, 3 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << LongestOddEvenSubarray(A, N);
return 0;
}
4
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。