📌  相关文章
📜  通过相邻对的异或对数组元素进行 M 次替换后的第 K 个数组元素

📅  最后修改于: 2021-09-04 08:19:24             🧑  作者: Mango

给定一个大小为N的数组arr[]和两个整数MK ,任务是在对给定数组执行以下M 次操作后找到第K索引处的数组元素。

如果操作次数MM 次操作后的K值无效,则打印-1

例子:

方法:想法是在每次操作后生成数组,并在每次迭代中签入是否可以进一步进行操作。如果不可能,则返回“-1” 。以下是步骤:

  1. 检查是否可以对数组执行给定数量的操作。
  2. 每次迭代后,数组的长度减 1,这意味着如果M大于或等于N ,则无法执行该操作。因此,打印“-1”
  3. 如果M的值有效,则检查给定的索引K是否有效。
  4. 经过M 次操作后,数组中还剩下(N – M) 个元素。因此,如果索引值大于或等于(N – M)的值,则打印“-1”
  5. 如果MK值都有效,则迭代循环M次并执行以下操作:
    • 在每次迭代中,创建一个临时数组temp[] ,用于存储通过对原始数组的相邻值执行操作获得的值。
    • 遍历数组arr[]并计算相邻元素的按位异或值,并将这些计算值保存在临时数组temp[] 中
    • 使用temp[]更新当前数组arr []
  6. M次迭代之后,返回arr[K]的值,这是所需的输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Method that returns the
// corresponding output by
// taking the given inputs.
int xor_operations(int N, int arr[], 
                   int M, int K)
{
     
    // If this condition is satisfied,
    // value of M is invalid
    if (M < 0 or M >= N)
        return -1;
     
    // Check if index K is valid
    if (K < 0 or K >= N - M)
        return -1;
     
    // Loop to perform M operations
    for(int p = 0; p < M; p++)
    {
         
        // Creating a temporary list
        vectortemp;
     
        // Traversing the array
        for(int i = 0; i < N; i++)
        {
             
            // Calculate XOR values
            // of adjacent elements
            int value = arr[i] ^ arr[i + 1];
             
            // Adding this value to
            // the temporary list
            temp.push_back(value);
             
            // Update the original array
            arr[i] = temp[i];
        }
    }
     
    // Getting value at index K
    int ans = arr[K];
     
    return ans;
}
 
// Driver Code
int main()
{
    // Number of elements
    int N = 5;
     
    // Given array arr[]
    int arr[] = {1, 4, 5, 6, 7};
    int M = 1, K = 2;
     
    // Function call
    cout << xor_operations(N, arr, M, K);
    return 0;
}
 
// This code is contributed by gauravrajput1


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Method that returns the
// corresponding output by
// taking the given inputs.
static int xor_operations(int N, int arr[],
                          int M, int K)
{
     
    // If this condition is satisfied,
    // value of M is invalid
    if (M < 0 || M >= N)
        return -1;
     
    // Check if index K is valid
    if (K < 0 || K >= N - M)
        return -1;
     
    // Loop to perform M operations
    for(int p = 0; p < M; p++)
    {
         
        // Creating a temporary list
        Vectortemp = new Vector();
     
        // Traversing the array
        for(int i = 0; i < N - 1; i++)
        {
             
            // Calculate XOR values
            // of adjacent elements
            int value = arr[i] ^ arr[i + 1];
             
            // Adding this value to
            // the temporary list
            temp.add(value);
             
            // Update the original array
            arr[i] = temp.get(i);
        }
    }
     
    // Getting value at index K
    int ans = arr[K];
     
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Number of elements
    int N = 5;
     
    // Given array arr[]
    int arr[] = { 1, 4, 5, 6, 7 };
    int M = 1, K = 2;
     
    // Function call
    System.out.print(xor_operations(N, arr, M, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Method that returns the
# corresponding output by
# taking the given inputs.
def xor_operations(N, arr, M, K):
     
    # If this condition is satisfied,
    # value of M is invalid
    if M < 0 or M >= N:
        return -1
     
    # Check if index K is valid
    if K < 0 or K >= N-M:
        return -1
     
 
    # Loop to perform M operations
    for _ in range(M):
     
        # Creating a temporary list
        temp = []
     
        # Traversing the array
        for i in range(len(arr)-1):
             
            # Calculate XOR values
            # of adjacent elements
            value = arr[i]^arr[i + 1]
             
            # Adding this value to
            # the temporary list
            temp.append(value)
         
        # Update the original array
        arr = temp[:]
     
    # Getting value at index K
    ans = arr[K]
    return ans
 
# Driver Code
 
# Number of elements
N = 5
 
# Given array arr[]
arr = [1, 4, 5, 6, 7]
M = 1
K = 2
 
# Function Call
print(xor_operations(N, arr, M, K))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Method that returns the
// corresponding output by
// taking the given inputs.
static int xor_operations(int N, int []arr,
                          int M, int K)
{
  // If this condition is satisfied,
  // value of M is invalid
  if (M < 0 || M >= N)
    return -1;
 
  // Check if index K is valid
  if (K < 0 || K >= N - M)
    return -1;
 
  // Loop to perform M operations
  for(int p = 0; p < M; p++)
  {
    // Creating a temporary list
    Listtemp = new List();
 
    // Traversing the array
    for(int i = 0; i < N - 1; i++)
    {
      // Calculate XOR values
      // of adjacent elements
      int value = arr[i] ^ arr[i + 1];
 
      // Adding this value to
      // the temporary list
      temp.Add(value);
 
      // Update the original array
      arr[i] = temp[i];
    }
  }
 
  // Getting value at index K
  int ans = arr[K];
 
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Number of elements
  int N = 5;
 
  // Given array []arr
  int []arr = {1, 4, 5, 6, 7};
  int M = 1, K = 2;
 
  // Function call
  Console.Write(xor_operations(N, arr,
                               M, K));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
3

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

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