📌  相关文章
📜  合并数组中所有偶数元素的子数组中的元素

📅  最后修改于: 2021-05-17 02:42:06             🧑  作者: Mango

给定一个包含N个数字的数组arr [] ,任务是通过用该子数组的第一个偶数元素替换所有连续的偶数来合并连续偶数的子数组。
注意:如果在给定序列中至少有三个偶数,则一系列偶数整数被认为是连续的。因此,在子数组中合并具有至少3个连续偶数的元素。
例子:

方法:为了解决此问题,我们首先需要确定是否存在大小大于3的连续偶数子序列。因此,想法是遍历给定的数组并检查数字是否为偶数。

  • 遍历数组。
  • 检查元素是否为偶数。
  • 如果是偶数,则创建一个临时数组来存储下一个连续的偶数,直到下一个数为奇数为止。
  • 继续在临时数组中添加元素,直到出现奇数为止。
  • 如果此临时数组的大小大于3,则从给定数组中删除这些元素,并用该临时数组的第一个元素替换它们。
  • 清空临时数组以计算下一组偶数子序列。

下面是上述方法的实现:

C++
// C++ program to merge the array
// as per the given condition
#include
using namespace std;
 
// Function to merge the array
// as per the given condition
vector merge(vector arr)
{
     
    // Variable to store the final
    // sequence
    vector ans;
 
    // Temporary array to store the
    // even numbers while traversing
    vector e;
    int i = 0;
    int j = 0;
    int count = 0;
     
    // Iterating through the array
    while(i < arr.size())
    {
         
        // If the element is even
        if(arr[i] % 2 == 0)
        {
            j = i;
             
            // Iterating till an odd element
            // is found
            while(j < arr.size())
            {
                 
                // Keep appending into the
                // temporary array if the
                // even number is occurred
                if (arr[j] % 2 == 0)
                {
                    e.push_back(arr[j]);
                    count += 1;
                }
 
                // Break if an odd number
                // has occurred
                else
                    break;
                 
                j += 1;
            }
     
            // If the series has at least
            // three elements, then merge
            if(count >= 3)
               ans.push_back(e[0]);
 
            // Else, add all elements to the
            // answer array
            else
            {
                for (auto i: e)
                     ans.push_back(i);
            }
                 
            // Reseting the count and
            // temp array
            count = 0;
            e.clear();
            i = j;
        }
             
        // If the element is odd, add
        // it to the answer array
        else
        {
            ans.push_back(arr[i]);
            i += 1;
        }
    }
     
    return ans;
}
         
// Driver code
int main()
{
 
    vector arr({ 2, 2, 2, 100, 5, 4,
                      2, 9, 10, 88, 24 });
    vector ans = merge(arr);
     
    cout << "[";
    for(int i= 0; i < ans.size(); i++)
    {
        if(i == ans.size() - 1)
        cout << ans[i] << "]";
        else
        cout << ans[i] << ", ";
    }
 
}
     
// This code is contributed by Samarth


Java
// Java program to merge the array
// as per the given condition
import java.util.*;
  
class GFG{
  
// Function to merge the array
// as per the given condition
static Vector merge(int []arr)
{
      
    // Variable to store the final
    // sequence
    Vector ans = new Vector();
  
    // Temporary array to store the
    // even numbers while traversing
    Vector e = new Vector();
     
    int i = 0;
    int j = 0;
    int count = 0;
      
    // Iterating through the array
    while (i < arr.length)
    {
          
        // If the element is even
        if (arr[i] % 2 == 0)
        {
            j = i;
              
            // Iterating till an odd element
            // is found
            while (j < arr.length)
            {
                  
                // Keep appending into the
                // temporary array if the
                // even number is occurred
                if (arr[j] % 2 == 0)
                {
                    e.add(arr[j]);
                    count += 1;
                }
  
                // Break if an odd number
                // has occurred
                else
                    break;
                  
                j += 1;
            }
      
            // If the series has at least
            // three elements, then merge
            if (count >= 3)
               ans.add(e.get(0));
  
            // Else, add all elements to the
            // answer array
            else
            {
                for(int ii : e)
                     ans.add(ii);
            }
                  
            // Reseting the count and
            // temp array
            count = 0;
            e.clear();
            i = j;
        }
              
        // If the element is odd, add
        // it to the answer array
        else
        {
            ans.add(arr[i]);
            i += 1;
        }
    }
    return ans;
}
          
// Driver code
public static void main(String[] args)
{
  
    int []arr = { 2, 2, 2, 100, 5, 4,
                  2, 9, 10, 88, 24 };
                   
    Vector ans = merge(arr);
      
    System.out.print("[");
    for(int i= 0; i < ans.size(); i++)
    {
        if (i == ans.size() - 1)
            System.out.print(ans.get(i) + "]");
        else
            System.out.print(ans.get(i) + ", ");
    }
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to merge the array
# as per the given condition
 
# Function to merge the array
# as per the given condition
def merge(arr):
     
    # Variable to store the final
    # sequence
    ans = []
 
    # Temporary array to store the
    # even numbers while traversing
    e =[]
    i = 0
    j = 0
    count = 0
     
    # Iterating through the array
    while i= 3):
                ans.append(e[0])
 
            # Else, add all elements to the
            # answer array
            else:
                for i in e:
                    ans.append(i)
                 
            # Reseting the count and
            # temp array
            count = 0
            e =[]
            i = j
 
        # If the element is odd, add
        # it to the answer array
        else:
            ans.append(arr[i])
            i+= 1
             
    return ans
        
# Driver code
if __name__ == "__main__":
     
    arr = [2, 2, 2, 100, 5, 4, 2, 9, 10, 88, 24]
     
    print(merge(arr))


C#
// C# program to merge the array
// as per the given condition
using System;
using System.Collections.Generic;
class GFG{
  
// Function to merge the array
// as per the given condition
static List merge(int []arr)
{
  // Variable to store the final
  // sequence
  List ans = new List();
 
  // Temporary array to store the
  // even numbers while traversing
  List e = new List();
 
  int i = 0;
  int j = 0;
  int count = 0;
 
  // Iterating through the array
  while (i < arr.Length)
  {
    // If the element is even
    if (arr[i] % 2 == 0)
    {
      j = i;
 
      // Iterating till an odd element
      // is found
      while (j < arr.Length)
      {
        // Keep appending into the
        // temporary array if the
        // even number is occurred
        if (arr[j] % 2 == 0)
        {
          e.Add(arr[j]);
          count += 1;
        }
 
        // Break if an odd number
        // has occurred
        else
          break;
 
        j += 1;
      }
 
      // If the series has at least
      // three elements, then merge
      if (count >= 3)
        ans.Add(e[0]);
 
      // Else, add all elements to the
      // answer array
      else
      {
        foreach(int ii in e)
          ans.Add(ii);
      }
 
      // Reseting the count and
      // temp array
      count = 0;
      e.Clear();
      i = j;
    }
 
    // If the element is odd, add
    // it to the answer array
    else
    {
      ans.Add(arr[i]);
      i += 1;
    }
  }
  return ans;
}
          
// Driver code
public static void Main(String[] args)
{
  int []arr = {2, 2, 2, 100, 5, 4,
               2, 9, 10, 88, 24};
 
  List ans = merge(arr);
  Console.Write("[");
   
  for(int i= 0; i < ans.Count; i++)
  {
    if (i == ans.Count - 1)
      Console.Write(ans[i] + "]");
    else
      Console.Write(ans[i] + ", ");
  }
}
}
 
// This code is contributed by 29AjayKumar


输出:
[2, 5, 4, 2, 9, 10]



时间复杂度: O(N 2 ) ,其中N是数组的长度。