📜  将数字序列转换为 a[i]=a[i+2] 的序列所需的最少操作

📅  最后修改于: 2021-10-27 09:07:02             🧑  作者: Mango

给定偶数长度“n”的整数序列,任务是找到将序列转换为遵循规则 a[i]=a[i+2] 所需的最少操作次数,其中“i”是索引。
这里的操作是用任意元素替换序列中的任意元素。

例子:

Input : n=4 ; Array : 3, 1, 3, 2
Output : 1
If we change the last element to '1' then, 
the sequence will become 3, 1, 3, 1 (satisfying the condition)
So, only 1 replacement is required.

Input : n=6 ; Array : 105 119 105 119 105 119
Output : 0
As the sequence is already in the required state.
So, no replacement of elements is required. 

方法:如我们所见,索引 0、2、…、n-2 是独立连接的,而 1、3、5、…、n 是独立连接的,并且必须具有相同的值。所以,

  • 我们必须通过将数字及其频率存储在映射中来找到两个序列(偶数和奇数)中出现次数最多的数字。
  • 然后,该序列中的所有其他数字都必须替换为同一序列中出现次数最多的数字。
  • 最后,上一步的替换次数就是答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum replacements
int minReplace(int a[], int n)
{
    int i;
 
    // Map to store the frequency of
    // the numbers at the even indices
    map te;
 
    // Map to store the frequency of
    // the numbers at the odd indices
    map to;
 
    for (i = 0; i < n; i++)
    {
 
        // Checking if the index
        // is odd or even
        if (i % 2 == 0)
 
            // If the number is already present then,
            // just increase the occurrence by 1
            te[a[i]]++;
        else
 
            // If the number is already present then,
            // just increase the occurrence by 1
            to[a[i]]++;
    }
 
    // To store the character with
    // maximum frequency in even indices.
    int me = -1;
 
    // To store the character with
    // maximum frequency in odd indices.
    int mo = -1;
 
    // To store the frequency of the
    // maximum occurring number in even indices.
    int ce = -1;
 
    // To store the frequency of the
    // maximum occurring number in odd indices.
    int co = -1;
 
    // Iterating over Map of even indices to
    // get the maximum occurring number.
    for (auto it : te)
    {
        if (it.second > ce)
        {
            ce = it.second;
            me = it.first;
        }
    }
 
    // Iterating over Map of odd indices to
    // get the maximum occurring number.
    for (auto it : to)
    {
        if (it.second > co)
        {
            co = it.second;
            mo = it.first;
        }
    }
 
    // To store the final answer
    int res = 0;
 
    for (i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
 
            // If the index is even but
            // a[i] != me
            // then a[i] needs to be replaced
            if (a[i] != me) res++;
        }
         
        else
        {
 
            // If the index is odd but
            // a[i] != mo
            // then a[i] needs to be replaced
            if (a[i] != mo) res++;
        }
    }
    return res;
}
 
// Driver Code
int main()
{
    int n = 4;
    int a[] = {3, 1, 3, 2};
    cout << minReplace(a, n) << endl;
    return 0;
}
 
// This code is contributed by
// sanjeev2552


Java
// Java implementation of the approach
import java.util.HashMap;
class GFG {
 
    // Function to return the minimum replacements
    public static int minReplace(int a[], int n)
    {
        int i;
 
        // Map to store the frequency of
        // the numbers at the even indices
        HashMap te = new HashMap<>();
 
        // Map to store the frequency of
        // the numbers at the odd indices
        HashMap to = new HashMap<>();
 
        for (i = 0; i < n; i++) {
 
            // Checking if the index
            // is odd or even
            if (i % 2 == 0) {
 
                // If the number is already present then,
                // just increase the occurrence by 1
                if (te.containsKey(a[i]))
                    te.put(a[i], te.get(a[i]) + 1);
                else
                    te.put(a[i], 1);
            }
            else {
 
                // If the number is already present then,
                // just increase the occurrence by 1
                if (to.containsKey(a[i]))
                    to.put(a[i], to.get(a[i]) + 1);
                else
                    to.put(a[i], 1);
            }
        }
 
        // To store the character with
        // maximum frequency in even indices.
        int me = -1;
 
        // To store the character with
        // maximum frequency in odd indices.
        int mo = -1;
 
        // To store the frequency of the
        // maximum occurring number in even indices.
        int ce = -1;
 
        // To store the frequency of the
        // maximum occurring number in odd indices.
        int co = -1;
 
        // Iterating over Map of even indices to
        // get the maximum occurring number.
        for (Integer It : te.keySet()) {
            if (te.get(It) > ce) {
                ce = te.get(It);
                me = It;
            }
        }
 
        // Iterating over Map of odd indices to
        // get the maximum occurring number.
        for (Integer It : to.keySet()) {
            if (to.get(It) > co) {
                co = to.get(It);
                mo = It;
            }
        }
 
        // To store the final answer
        int res = 0;
 
        for (i = 0; i < n; i++) {
            if (i % 2 == 0) {
 
                // If the index is even but
                // a[i] != me
                // then a[i] needs to be replaced
                if (a[i] != me)
                    res++;
            }
            else {
 
                // If the index is odd but
                // a[i] != mo
                // then a[i] needs to be replaced
                if (a[i] != mo)
                    res++;
            }
        }
 
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n;
        n = 4;
        int a[] = { 3, 1, 3, 2 };
        System.out.println(minReplace(a, n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the minimum replacements
def minReplace(a: list, n) -> int:
 
    # Map to store the frequency of
    # the numbers at the even indices
    te = dict()
 
    # Map to store the frequency of
    # the numbers at the odd indices
    to = dict()
 
    for i in range(n):
 
        # Checking if the index
        # is odd or even
        if i % 2 == 0:
 
            # If the number is already present then,
            # just increase the occurrence by 1
            if a[i] not in te:
                te[a[i]] = 1
            else:
                te[a[i]] += 1
        else:
 
            # If the number is already present then,
            # just increase the occurrence by 1
            if a[i] not in to:
                to[a[i]] = 1
            else:
                to[a[i]] += 1
 
    # To store the character with
    # maximum frequency in even indices.
    me = -1
 
    # To store the character with
    # maximum frequency in odd indices.
    mo = -1
 
    # To store the frequency of the
    # maximum occurring number in even indices.
    ce = -1
 
    # To store the frequency of the
    # maximum occurring number in odd indices.
    co = -1
 
    # Iterating over Map of even indices to
    # get the maximum occurring number.
    for it in te:
        if te[it] > ce:
            ce = te[it]
            me = it
 
    # Iterating over Map of odd indices to
    # get the maximum occurring number.
    for it in to:
        if to[it] > co:
            co = to[it]
            mo = it
 
    # To store the final answer
    res = 0
 
    for i in range(n):
        if i % 2 == 0:
 
            # If the index is even but
            # a[i] != me
            # then a[i] needs to be replaced
            if a[i] != me:
                res += 1
        else:
 
            # If the index is odd but
            # a[i] != mo
            # then a[i] needs to be replaced
            if a[i] != mo:
                res += 1
 
    return res
 
# Driver Code
if __name__ == "__main__":
    n = 4
    a = [3, 1, 3, 2]
    print(minReplace(a, n))
 
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to return the minimum replacements
    public static int minReplace(int []a, int n)
    {
        int i;
 
        // Map to store the frequency of
        // the numbers at the even indices
        Dictionary te = new Dictionary();
 
        // Map to store the frequency of
        // the numbers at the odd indices
        Dictionary to = new Dictionary();
 
        for (i = 0; i < n; i++)
        {
 
            // Checking if the index
            // is odd or even
            if (i % 2 == 0)
            {
 
                // If the number is already present then,
                // just increase the occurrence by 1
                if (te.ContainsKey(a[i]))
                    te[a[i]] = te[a[i]] + 1;
                else
                    te.Add(a[i], 1);
            }
            else
            {
 
                // If the number is already present then,
                // just increase the occurrence by 1
                if (to.ContainsKey(a[i]))
                    to[a[i]] = te[a[i]] + 1;
                else
                    to.Add(a[i], 1);
            }
        }
 
        // To store the character with
        // maximum frequency in even indices.
        int me = -1;
 
        // To store the character with
        // maximum frequency in odd indices.
        int mo = -1;
 
        // To store the frequency of the
        // maximum occurring number in even indices.
        int ce = -1;
 
        // To store the frequency of the
        // maximum occurring number in odd indices.
        int co = -1;
 
        // Iterating over Map of even indices to
        // get the maximum occurring number.
        foreach (int It in te.Keys)
        {
            if (te[It] > ce)
            {
                ce = te[It];
                me = It;
            }
        }
 
        // Iterating over Map of odd indices to
        // get the maximum occurring number.
        foreach (int It in to.Keys)
        {
            if (to[It] > co)
            {
                co = to[It];
                mo = It;
            }
        }
 
        // To store the final answer
        int res = 0;
 
        for (i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
 
                // If the index is even but
                // a[i] != me
                // then a[i] needs to be replaced
                if (a[i] != me)
                    res++;
            }
            else
            {
 
                // If the index is odd but
                // a[i] != mo
                // then a[i] needs to be replaced
                if (a[i] != mo)
                    res++;
            }
        }
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n;
        n = 4;
        int []a = { 3, 1, 3, 2 };
        Console.WriteLine(minReplace(a, n));
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程