📜  所需的最小子阵列反转,使得所有相邻元素对的总和为奇数

📅  最后修改于: 2021-09-08 12:25:13             🧑  作者: Mango

给定一个大小为N的数组arr[] ,具有相等数量的偶数和奇数整数,任务是找到需要反转的最小子数组数,以使相邻元素对的总和为奇数。

例子:

处理方法:使相邻元素之和为奇数,其思想是将元素以奇偶或偶奇的方式排列。要最小化反转操作的次数,请观察给定数组的以下属性:

  • 如果有任何长度为M的子数组具有相同奇偶校验的所有元素,那么也存在一个长度为M的子数组,它具有相反奇偶校验的所有元素,因为数组中偶数和奇数元素的计数相同。
  • 因此,生成交替奇偶校验长度为M的子数组所需的反转操作次数为(M – 1)

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

  • 遍历给定数组,求数组中每个连续相同的奇偶元素所需的子数组反转次数。
  • 令连续奇数和偶数元素的反转次数分别为cntOddcntEven
  • 最小反转计数由cntOddcntEven的最大值给出,因为任务是删除所有连续的相同元素和需要删除的最大连续元素。

下面是上述方法的实现:

C++14
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count reversals to
// seperate elements with same parity
int separate(int arr[], int n, int parity)
{
    int count = 1, res = 0;
 
    // Traverse the given array
    for (int i = 1; i < n; i++) {
 
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1)
            && ((arr[i - 1] + parity) & 1))
            count++;
 
        // Otherwise
        else {
 
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
 
            count = 1;
        }
    }
 
    // Return the total reversals
    return res;
}
 
// Function to print the array elements
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to count the mimimum reversals
// required to make make sum
// of all adjacent elements odd
void requiredOps(int arr[], int N)
{
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
 
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
 
    // Maximum of two is the return
    cout << max(res1, res2);
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    requiredOps(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to count reversals to
// seperate elements with same parity
static int separate(int arr[], int n, 
                    int parity)
{
    int count = 1, res = 0;
     
    // Traverse the given array
    for(int i = 1; i < n; i++)
    {
         
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1) != 0 &&
            ((arr[i - 1] + parity) & 1) != 0)
            count++;
 
        // Otherwise
        else
        {
             
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
 
            count = 1;
        }
    }
 
    // Return the total reversals
    return res;
}
 
// Function to print the array elements
void printArray(int arr[], int n)
{
    for(int i = 0; i < n; i++)
        System.out.println(arr[i] + " ");
}
 
// Function to count the mimimum reversals
// required to make make sum
// of all adjacent elements odd
static void requiredOps(int arr[], int N)
{
     
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
 
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
 
    // Maximum of two is the return
    System.out.print(Math.max(res1, res2));
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given array arr[]
    int arr[] = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
                   
    // Size of array
    int N = arr.length;
     
    // Function Call
    requiredOps(arr, N);
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program for the
# above approach
 
# Function to count reversals
# to seperate elements with
# same parity
def separate(arr, n, parity):
 
    count = 1
    res = 0
 
    # Traverse the given array
    for i in range(1, n):
 
        # Count size of subarray
        # having integers with
        # same parity only
        if (((arr[i] + parity) & 1) and
            ((arr[i - 1] + parity) & 1)):
            count += 1
 
        # Otherwise
        else:
 
            # Reversals required is
            # equal to one less than
            # subarray size
            if (count > 1):
                res += count - 1
 
            count = 1
 
    # Return the total
    # reversals
    return res
 
# Function to print the
# array elements
def printArray(arr, n):
 
    for i in range(n):
        print(arr[i],
              end = " ")
 
# Function to count the mimimum
# reversals required to make
# make sum of all adjacent
# elements odd
def requiredOps(arr, N):
 
    # Stores operations required
    # for separating adjacent
    # odd elements
    res1 = separate(arr, N, 0)
 
    # Stores operations required
    # for separating adjacent
    # even elements
    res2 = separate(arr, N, 1)
 
    # Maximum of two is the
    # return
    print(max(res1, res2))
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [13, 2, 6, 8, 3, 5,
           7, 10, 14, 15]
 
    # Size of array
    N = len(arr)
 
    # Function Call
    requiredOps(arr, N)
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to count reversals to
// seperate elements with same parity
static int separate(int[] arr, int n, 
                    int parity)
{
    int count = 1, res = 0;
      
    // Traverse the given array
    for(int i = 1; i < n; i++)
    {
         
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1) != 0 &&
            ((arr[i - 1] + parity) & 1) != 0)
            count++;
  
        // Otherwise
        else
        {
             
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
  
            count = 1;
        }
    }
  
    // Return the total reversals
    return res;
}
  
// Function to print the array elements
void printArray(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
  
// Function to count the mimimum reversals
// required to make make sum
// of all adjacent elements odd
static void requiredOps(int[] arr, int N)
{
      
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
  
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
  
    // Maximum of two is the return
    Console.Write(Math.Max(res1, res2));
}
 
// Driver code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
                    
    // Size of array
    int N = arr.Length;
      
    // Function Call
    requiredOps(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live