📌  相关文章
📜  将数组 B 构造为通过对给定数组的每个后缀执行给定操作而获得的每个后缀数组的最后一个元素

📅  最后修改于: 2022-05-13 01:57:50.875000             🧑  作者: Mango

将数组 B 构造为通过对给定数组的每个后缀执行给定操作而获得的每个后缀数组的最后一个元素

给定一个包含N个整数的数组arr[] ,任务是打印通过对数组的每个后缀arr[]执行以下操作获得的每个后缀数组左侧的最后一个元素:

  1. 将后缀数组的元素复制到数组suff[]中。
  2. 第 i后缀元素更新为suff[i] = (suff[i] OR suff[i+1]) – (suff[i] XOR suff[i+1])将后缀数组的大小减少1
  3. 重复上述步骤,直到后缀数组的大小不是1

例子:

Naive Approach:最简单的方法是遍历每个后缀数组并通过迭代后缀数组来执行上述操作,然后打印获得的值。

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

有效方法:可以根据以下观察解决给定的问题:

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

  • 使用变量i[0, N-2]范围内以相反的顺序迭代,并在每次迭代中将arr[i]更新为arr[i] & arr[i+1]
  • [0, N-1]范围内迭代并使用变量i并执行以下步骤:
    • 打印存储在arr[i]中的值作为[i, N-1] 范围内的后缀数组的答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the last element
// left of every suffix of the array
// after performing the given operati-
// ons on them
 
void performOperation(int arr[], int N)
{
    // Iterate until i is greater than
    // or equal to 0
    for (int i = N - 2; i >= 0; i--) {
        arr[i] = arr[i] & arr[i + 1];
    }
 
    // Print the array arr[]
    for (int i = 0; i < N; i++)
        cout << arr[i] << " ";
    cout << endl;
}
// Driver Code
int main()
{
    // Input
    int arr[] = { 2, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    performOperation(arr, N);
}


Java
// Java program for the above approach
class GFG {
 
    // Function to find the last element
    // left of every suffix of the array
    // after performing the given operati-
    // ons on them
    public static void performOperation(int arr[], int N)
    {
       
        // Iterate until i is greater than
        // or equal to 0
        for (int i = N - 2; i >= 0; i--) {
            arr[i] = arr[i] & arr[i + 1];
        }
 
        // Print the array arr[]
        for (int i = 0; i < N; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    // Driver Code
    public static void main(String args[])
    {
       
        // Input
        int arr[] = { 2, 3, 6, 5 };
        int N = arr.length;
 
        // Function call
        performOperation(arr, N);
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python 3 program for the above approach
 
# Function to find the last element
# left of every suffix of the array
# after performing the given operati-
# ons on them
def performOperation(arr, N):
   
    # Iterate until i is greater than
    # or equal to 0
    i = N - 2
    while(i >= 0):
        arr[i] = arr[i] & arr[i + 1]
        i -= 1
 
    # Print the array arr[]
    for i in range(N):
        print(arr[i], end = " ")
     
# Driver Code
if __name__ == '__main__':
    # Input
    arr = [2, 3, 6, 5]
    N = len(arr)
 
    # Function call
    performOperation(arr, N)
     
    # This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the last element
// left of every suffix of the array
// after performing the given operati-
// ons on them
static void performOperation(int []arr, int N)
{
     
    // Iterate until i is greater than
    // or equal to 0
    for(int i = N - 2; i >= 0; i--)
    {
        arr[i] = arr[i] & arr[i + 1];
    }
 
    // Print the array arr[]
    for(int i = 0; i < N; i++)
        Console.Write(arr[i] + " ");
         
    Console.WriteLine();
}
 
// Driver Code
public static void Main()
{
     
    // Input
    int []arr = { 2, 3, 6, 5 };
    int N = arr.Length;
 
    // Function call
    performOperation(arr, N);
}
}
 
// This code is contributed by ipg2016107


Javascript


输出
0 0 4 5 

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