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[]:
- Copy the elements of the suffix array into an array suff[].
- 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.
- 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:
- Suffix array {2, 3, 6, 5}:
- In the first step, the array modifies to {2, 2, 4}.
- In the second step, the array modifies to {2, 0}
- In the third step, the array modifies to {0}.
- Therefore, the last element left is 0.
- Suffix array {3, 6, 5}:
- In the first step, the array modifies to {2, 4}.
- In the second step, the array modifies to {0}
- Therefore, the last element left is 0.
- Suffix array {6, 5}:
- In the first step, the array modifies to {4}
- Therefore, the last element left is 4.
- Suffix array {5}:
- 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.
Algorithm
Define a function called last_elements_left that takes a vector of integers as input.
Get the size of the input vector arr and loop through each index i from 0 to n-1.
Create a new vector called suff that contains all the elements from arr starting from index i.
While the size of suff is greater than 1, do the following:
a. Create a new vector called new_suff with size suff.size() - 1.
b. Loop through each index j in new_suff and do the following:
i. Compute suff[j] | suff[j + 1] and store the result in temp.
ii. Compute suff[j] ^ suff[j + 1] and subtract the result from temp.
iii. Store the result of step ii in new_suff[j].
c. Set suff to new_suff.
Print the first and only element in suff.
C++
#include <iostream>
#include <vector>
using namespace std;
void last_elements_left(vector< int > arr) {
int n = arr.size();
for ( int i = 0; i < n; i++) {
vector< int > suff(arr.begin() + i, arr.end());
while (suff.size() > 1) {
vector< int > new_suff(suff.size() - 1);
for ( int j = 0; j < suff.size() - 1; j++) {
new_suff[j] = (suff[j] | suff[j + 1]) - (suff[j] ^ suff[j + 1]);
}
suff = new_suff;
}
cout << suff[0] << " " ;
}
}
int main() {
vector< int > arr = {2, 3, 6, 5};
last_elements_left(arr);
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
public static void lastElementsLeft(ArrayList<Integer> arr) {
int n = arr.size();
for ( int i = 0 ; i < n; i++) {
ArrayList<Integer> suff = new ArrayList<>(arr.subList(i, n));
while (suff.size() > 1 ) {
ArrayList<Integer> newSuff = new ArrayList<>(suff.size() - 1 );
for ( int j = 0 ; j < suff.size() - 1 ; j++) {
newSuff.add((suff.get(j) | suff.get(j + 1 )) - (suff.get(j) ^ suff.get(j + 1 )));
}
suff = newSuff;
}
System.out.print(suff.get( 0 ) + " " );
}
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add( 2 );
arr.add( 3 );
arr.add( 6 );
arr.add( 5 );
lastElementsLeft(arr);
}
}
|
Python3
def last_elements_left(arr):
n = len (arr)
for i in range (n):
suff = arr[i:]
while len (suff) > 1 :
new_suff = [ 0 ] * ( len (suff) - 1 )
for j in range ( len (suff) - 1 ):
new_suff[j] = (suff[j] | suff[j + 1 ]) - (suff[j] ^ suff[j + 1 ])
suff = new_suff
print (suff[ 0 ], end = " " )
arr = [ 2 , 3 , 6 , 5 ]
last_elements_left(arr)
|
C#
using System;
using System.Collections.Generic;
class MainClass
{
static void lastElementsLeft(List< int > arr)
{
int n = arr.Count;
for ( int i = 0; i < n; i++)
{
List< int > suff = new List< int >(arr.GetRange(i, n - i));
while (suff.Count > 1)
{
List< int > newSuff = new List< int >(suff.Count - 1);
for ( int j = 0; j < suff.Count - 1; j++)
{
newSuff.Add((suff[j] | suff[j + 1]) - (suff[j] ^ suff[j + 1]));
}
suff = newSuff;
}
Console.Write(suff[0] + " " );
}
}
static void Main()
{
List< int > arr = new List< int >();
arr.Add(2);
arr.Add(3);
arr.Add(6);
arr.Add(5);
lastElementsLeft(arr);
}
}
|
Javascript
function last_elements_left(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
let suff = arr.slice(i);
while (suff.length > 1) {
let new_suff = new Array(suff.length - 1);
for (let j = 0; j < suff.length - 1; j++) {
new_suff[j] = (suff[j] | suff[j + 1]) - (suff[j] ^ suff[j + 1]);
}
suff = new_suff;
}
console.log(suff[0] + " " );
}
}
let arr = [2, 3, 6, 5];
last_elements_left(arr);
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The given problem can be solved based on the following observations:
- From the bitwise property:
- (X | Y) — (X ^ Y) = (X & Y)
- 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
#include <bits/stdc++.h>
using namespace std;
void performOperation( int arr[], int N)
{
for ( int i = N - 2; i >= 0; i--) {
arr[i] = arr[i] & arr[i + 1];
}
for ( int i = 0; i < N; i++)
cout << arr[i] << " " ;
cout << endl;
}
int main()
{
int arr[] = { 2, 3, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
performOperation(arr, N);
}
|
Java
import java.io.*;
class GFG {
public static void performOperation( int arr[], int N)
{
for ( int i = N - 2 ; i >= 0 ; i--) {
arr[i] = arr[i] & arr[i + 1 ];
}
for ( int i = 0 ; i < N; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 2 , 3 , 6 , 5 };
int N = arr.length;
performOperation(arr, N);
}
}
|
Python3
def performOperation(arr, N):
i = N - 2
while (i > = 0 ):
arr[i] = arr[i] & arr[i + 1 ]
i - = 1
for i in range (N):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 2 , 3 , 6 , 5 ]
N = len (arr)
performOperation(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void performOperation( int []arr, int N)
{
for ( int i = N - 2; i >= 0; i--)
{
arr[i] = arr[i] & arr[i + 1];
}
for ( int i = 0; i < N; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
public static void Main()
{
int []arr = { 2, 3, 6, 5 };
int N = arr.Length;
performOperation(arr, N);
}
}
|
Javascript
<script>
function performOperation(arr, N) {
for (let i = N - 2; i >= 0; i--) {
arr[i] = arr[i] & arr[i + 1];
}
for (let i = 0; i < N; i++)
document.write(arr[i] + " " );
document.write( '<br>' )
}
let arr = [2, 3, 6, 5];
let N = arr.length;
performOperation(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Aug, 2023
Like Article
Save Article