Skip to content
Related Articles

Related Articles

Improve Article

Construct array B as last element left of every suffix array obtained by performing given operations on every suffix of given array

  • Last Updated : 28 Jul, 2021

Given an array arr[] of N integers, the task is to print the last element left of every suffix array obtained by performing the following operation on every suffix of the array, arr[]:

  1. Copy the elements of the suffix array into an array suff[].
  2. Update ith suffix element as suff[i] = (suff[i] OR suff[i+1]) – (suff[i] XOR suff[i+1]) reducing the size of suffix array by 1.
  3. Repeat the above step, until the size of the suffix array is not 1.

Examples:

Input: arr[] = {2, 3, 6, 5}
Output: 0 0 4 5
Explanation: 
Perform the operations as follows:

  1. Suffix array {2, 3, 6, 5}:
    1. In the first step, the array modifies to {2, 2, 4}.
    2. In the second step, the array modifies to {2, 0}
    3. In the third step, the array modifies to {0}.
    4. Therefore, the last element left is 0.
  2. Suffix array {3, 6, 5}:
    1. In the first step, the array modifies to {2, 4}.
    2. In the second step, the array modifies to {0}
    3. Therefore, the last element left is 0.
  3. Suffix array {6, 5}:
    1. In the first step, the array modifies to {4}
    2. Therefore, the last element left is 4.
  4. Suffix array {5}:
    1. It has only one element. Therefore, the last element left is 5.

Input: arr[] = {1, 2, 3, 4}
Output: 0 0 0 4

Naive Approach: The simplest approach is to traverse every suffix array and perform the above-given operations by iterating over the suffix array and then print the value obtained.



Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The given problem can be solved based on the following observations: 

  1. From the bitwise property:
    • (X | Y) — (X ^ Y) = (X & Y)
  2. Therefore, from the above, the last value obtained is the bitwise AND of all the elements of the suffix array after performing the given operation on the suffix array.

Follow the steps below to solve the problem:

  • Iterate in the range [0, N-2] and in reverse order using the variable i and in each iteration update the arr[i] to arr[i] & arr[i+1].
  • Iterate in the range [0, N-1] and using a variable i and perform the following steps:
    • Print the value stored in arr[i] as the answer for the suffix array over the range [i, N-1].

Below is the implementation of the above approach:

C++14




// C++ program for the above approach
#include <bits/stdc++.h>
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);
}

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




<script>
 
        // JavaScript 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
 
        function performOperation(arr, N) {
            // Iterate until i is greater than
            // or equal to 0
            for (let i = N - 2; i >= 0; i--) {
                arr[i] = arr[i] & arr[i + 1];
            }
 
            // Print the array arr[]
            for (let i = 0; i < N; i++)
                document.write(arr[i] + " ");
 
            document.write('<br>')
        }
        // Driver Code
 
        // Input
        let arr = [2, 3, 6, 5];
        let N = arr.length;
 
        // Function call
        performOperation(arr, N);
 
    // This code is contributed by Potta Lokesh
    </script>
Output
0 0 4 5 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :