📌  相关文章
📜  最长子数组中的最大元素,仅由偶数或奇数组成

📅  最后修改于: 2021-05-19 19:07:35             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是在最长的子数组中找到仅由偶数或奇数组成的最大元素。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,例如maxLen ,以存储直到i索引为止获得的最长子数组的长度,该索引仅包含偶数或奇数。
  • 初始化一个变量,例如Len ,以存储当前子数组直到第i数组元素的长度,该数组仅由偶数或奇数组成。
  • 初始化一个变量,例如MaxElem ,以存储获得的最长子数组的最大元素,直到i索引仅由偶数或奇数元素组成。
  • 使用变量i遍历数组。对于每个i数组元素,检查arr [i]%2是否等于arr [i – 1]%2 。如果发现为真,则增加Len的值。
  • 否则,更新Len = 1的值。
  • 如果Len> = maxLen ,则更新MaxElem = max(MaxElem,arr [i])
  • 最后,输出MaxElem的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only
int maxelementLongestSub(int arr[], int n)
{
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for (int i = 1; i < n; i++) {
 
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2) {
 
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen) {
                maxLen = Len;
 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
 
        else {
 
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen) {
 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem) {
 
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
 
    return MaxElem;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxelementLongestSub(arr, n);
 
    return 0;
}


C
// C program to implement
// the above approach
#include 
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only
int maxelementLongestSub(int arr[], int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
 
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    printf("%d", maxelementLongestSub(arr, n));
 
    return 0;
}
 
// This code is contributed by sourav singh


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only    
static int maxelementLongestSub(int arr[], int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
                 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
    int n = arr.length;
 
    System.out.print(maxelementLongestSub(arr, n));
}
}
 
// This code is contributed by sourav singh


Python3
# Python3 program to implement
# the above approach
 
# Function to find the largest element
# of the longest subarray consisting
# only of odd or even elements only
def maxelementLongestSub(arr, n):
     
    # Stores largest element of the
    # longest sub-array till i-th index
    MaxElem = arr[0]
 
    # Stores maximum length of the
    # longest sub-array till i-th index
    maxLen = 1
 
    # Stores length of the current
    # sub-array including the i-th element
    Len = 1
 
    # Stores largest element in
    # current sub-array
    Max = arr[0]
 
    for i in range(1, n):
         
        # If arr[i] and arr[i - 1]
        # are either even numbers
        # or odd numbers
        if arr[i] % 2 == arr[i - 1] % 2:
             
            # Update Len
            Len += 1
             
            # Update Max
            if arr[i] > Max:
                Max = arr[i]
                 
            # If Len greater than
            # maxLen
            if Len >= maxLen:
                maxLen = Len
                 
                # Update MaxElem
                if Max >= MaxElem:
                    MaxElem = Max
        else:
             
            # Update Len
            Len = 1
             
            # Update Max
            Max = arr[i]
             
            # If Len greater
            # than maxLen
            if Len >= maxLen:
                maxLen = Len
                 
                # If Max greater
                #   than MaxElem
                if Max >= MaxElem:
                    MaxElem = Max
                     
    return MaxElem
 
# Driver Code
arr = [ 1, 3, 5, 7, 8, 12, 10 ]
n = len(arr)
 
print(maxelementLongestSub(arr, n))
 
# This code is contributed by sourav singh


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only    
static int maxelementLongestSub(int[] arr,
                                int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
                 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
//  Driver Code
public static void Main(String[] args)
{
    int[] arr = { 1, 3, 5, 7, 8, 12, 10 };
    int n = arr.Length;
 
    // Function call
    Console.Write(maxelementLongestSub(arr, n));
}
}
 
// This code is contributed by sourav singh


Javascript


输出:
7

时间复杂度: O(N)
辅助空间: O(1)