📌  相关文章
📜  修改数组以使相邻元素的奇偶性不同所需的最少操作

📅  最后修改于: 2021-06-26 11:01:48             🧑  作者: Mango

给定一个数组A [] ,任务是找到将数组转换为B []所需的最少操作数,以使得对于B中的每个索引(最后一个除外), parity(b [i])!= parity(b [i + 1])其中parity(x)= x%3

以下是要执行的操作:

例子:

方法:一种最佳方法是更新两个其他元素之间的元素,以便可以对其进行更新,以使其在同一个操作中,奇偶校验可以与之前的元素和紧随其后的元素不同(以数字表示)操作的数量需要最小化)。
因此,开始遍历数组从A [1]A [n – 2] ,并对每个元素A [i]进行奇偶校验(A [i])==奇偶校验(A [i – 1])奇偶校验(A [i])== parity(A [i + 1]) ,更新A [i]使其奇偶性不同于其周围的两个数字,并跟踪执行的操作数。最后打印计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the parity of a number
int parity(int a)
{
    return a % 3;
}
 
// Function to return the minimum
// number of operations required
int solve(int array[], int size)
{
    int operations = 0;
    for (int i = 0; i < size - 1; i++) {
 
        // Operation needs to be performed
        if (parity(array[i]) == parity(array[i + 1])) {
 
            operations++;
            if (i + 2 < size) {
 
                // Parity of previous element
                int pari1 = parity(array[i]);
 
                // Parity of next element
                int pari2 = parity(array[i + 2]);
 
                // Update parity of current element to be other than
                // the parities of the pervious and the next number
                if (pari1 == pari2) {
                    if (pari1 == 0)
                        array[i + 1] = 1;
                    else if (pari1 == 1)
                        array[i + 1] = 0;
                    else
                        array[i + 1] = 1;
                }
                else {
                    if ((pari1 == 0 && pari2 == 1)
                        || (pari1 == 1 && pari2 == 0))
                        array[i + 1] = 2;
                    if ((pari1 == 1 && pari2 == 2)
                        || (pari1 == 2 && pari2 == 1))
                        array[i + 1] = 0;
                    if ((pari1 == 2 && pari2 == 0)
                        || (pari1 == 0 && pari2 == 2))
                        array[i + 1] = 1;
                }
            }
        }
    }
 
    return operations;
}
 
// Driver Code
int main()
{
    int array[] = { 2, 1, 3, 0 };
    int size = sizeof(array) / sizeof(array[0]);
    cout << solve(array, size) << endl;
 
  return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the parity of a number
    static int parity(int a)
    {
        return a % 3;
    }
 
    // Function to return the minimum
    // number of operations required
    static int solve(int []array, int size)
    {
        int operations = 0;
        for (int i = 0; i < size - 1; i++)
        {
     
            // Operation needs to be performed
            if (parity(array[i]) == parity(array[i + 1]))
            {
     
                operations++;
                if (i + 2 < size)
                {
     
                    // Parity of previous element
                    int pari1 = parity(array[i]);
     
                    // Parity of next element
                    int pari2 = parity(array[i + 2]);
     
                    // Update parity of current
                    // element to be other than
                    // the parities of the pervious
                    // and the next number
                    if (pari1 == pari2)
                    {
                        if (pari1 == 0)
                            array[i + 1] = 1;
                        else if (pari1 == 1)
                            array[i + 1] = 0;
                        else
                            array[i + 1] = 1;
                    }
                    else
                    {
                        if ((pari1 == 0 && pari2 == 1) ||
                            (pari1 == 1 && pari2 == 0))
                            array[i + 1] = 2;
                        if ((pari1 == 1 && pari2 == 2)
                            || (pari1 == 2 && pari2 == 1))
                            array[i + 1] = 0;
                        if ((pari1 == 2 && pari2 == 0)
                            || (pari1 == 0 && pari2 == 2))
                            array[i + 1] = 1;
                    }
                }
            }
        }
     
        return operations;
    }
 
    // Driver Code
    public static void main (String arg[])
    {
        int []array = { 2, 1, 3, 0 };
        int size = array.length;
        System.out.println(solve(array, size));
    }
}
 
// This code is contributed by Ryuga


Python3
# Python3 implementation of the approach
 
# Function to return the parity
# of a number
def parity(a):
    return a % 3
 
# Function to return the minimum
# number of operations required
def solve(array, size):
 
    operations = 0
    for i in range(0, size - 1):
 
        # Operation needs to be performed
        if parity(array[i]) == parity(array[i + 1]):
 
            operations += 1
            if i + 2 < size:
 
                # Parity of previous element
                pari1 = parity(array[i])
 
                # Parity of next element
                pari2 = parity(array[i + 2])
 
                # Update parity of current element to
                # be other than the parities of the
                # previous and the next number
                if pari1 == pari2:
                    if pari1 == 0:
                        array[i + 1] = 1
                    elif pari1 == 1:
                        array[i + 1] = 0
                    else:
                        array[i + 1] = 1
                 
                else:
                    if ((pari1 == 0 and pari2 == 1) or
                        (pari1 == 1 and pari2 == 0)):
                        array[i + 1] = 2
                    if ((pari1 == 1 and pari2 == 2) or
                        (pari1 == 2 and pari2 == 1)):
                        array[i + 1] = 0
                    if ((pari1 == 2 and pari2 == 0) and
                        (pari1 == 0 and pari2 == 2)):
                        array[i + 1] = 1
 
    return operations
 
# Driver Code
if __name__ == "__main__":
 
    array = [2, 1, 3, 0]
    size = len(array)
    print(solve(array, size))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the parity of a number
static int parity(int a)
{
    return a % 3;
}
 
// Function to return the minimum
// number of operations required
static int solve(int []array, int size)
{
    int operations = 0;
    for (int i = 0; i < size - 1; i++)
    {
 
        // Operation needs to be performed
        if (parity(array[i]) == parity(array[i + 1]))
        {
 
            operations++;
            if (i + 2 < size)
            {
 
                // Parity of previous element
                int pari1 = parity(array[i]);
 
                // Parity of next element
                int pari2 = parity(array[i + 2]);
 
                // Update parity of current
                // element to be other than
                // the parities of the pervious
                // and the next number
                if (pari1 == pari2)
                {
                    if (pari1 == 0)
                        array[i + 1] = 1;
                    else if (pari1 == 1)
                        array[i + 1] = 0;
                    else
                        array[i + 1] = 1;
                }
                else
                {
                    if ((pari1 == 0 && pari2 == 1) ||
                        (pari1 == 1 && pari2 == 0))
                        array[i + 1] = 2;
                    if ((pari1 == 1 && pari2 == 2)
                        || (pari1 == 2 && pari2 == 1))
                        array[i + 1] = 0;
                    if ((pari1 == 2 && pari2 == 0)
                        || (pari1 == 0 && pari2 == 2))
                        array[i + 1] = 1;
                }
            }
        }
    }
 
    return operations;
}
 
// Driver Code
public static void Main()
{
    int []array = { 2, 1, 3, 0 };
    int size = array.Length;
    Console.WriteLine(solve(array, size));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
1

时间复杂度: O(N)

辅助空间: O(1)