Queries to search for an element in an array and modify the array based on given conditions
Last Updated :
12 Jun, 2021
Given an array arr[] consisting of N integers and an integer X, the task is to print the array after performing X queries denoted by an array operations[]. The task for each query is as follows:
- If the array contains the integer operations[i], reverse the subarray starting from the index at which operations[i] is found, to the end of the array.
- Otherwise, insert operations[i] to the end of the array.
Examples:
Input: arr[] = {1, 2, 3, 4}, X = 3, operations[] = {12, 2, 13}
Output: 1 12 4 3 2
Explanation:
Query 1: arr[] does not contain 12. Therefore, append it to the last. Therefore, arr[] = {1, 2, 3, 4, 12}.
Query 2: arr[] contains 2 at index 1. Therefore, reverse the subarray {arr[1], arr[4]}. Therefore, arr[] = {1, 12, 4, 3, 2}.
Query 3: arr[] does not contain 13. Therefore, append it to the last. Therefore, arr[] = {1, 12, 4, 3, 2, 13}.
Input: arr[] = {1, 1, 12, 6}, X = 2, operations[] = {1, 13}
Output: 1 12 4 3 2
Approach: The simplest approach is that for each query search the whole array to check if the concerned integer is present or not. If present at an index i and the current size of the array is N, then reverse the subarray {arr[i], … arr[N – 1]} . Otherwise, insert the searched element at the end of the array. Follow the steps below to solve the problem:
- Create a function to linearly search for the index of an element in an array.
- Now for each query, if the given element is not present in the given array, append that to the end of the array.
- Otherwise, if it is present at any index i, reverse the subarray starting from index i up to the end.
- After completing the above steps, print the resultant array.
Below is the implementation for the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void rev(vector< int > &arr, int l, int r)
{
while (l < r)
{
int tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++;
r--;
}
}
void doOperation(vector< int > &arr, int o)
{
int ind = -1;
int n = arr.size();
for ( int i = 0; i < n; i++)
{
if (arr[i] == o)
{
ind = i;
break ;
}
}
if (ind == -1)
arr.push_back(o);
else
rev(arr, ind, n - 1);
}
void print(vector< int > &arr)
{
for ( int x : arr)
{
cout << x << " " ;
}
}
void operations(vector< int > &queries,
vector< int > &arr)
{
for ( auto x : queries)
doOperation(arr, x);
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int x = 3;
vector< int > queries({ 12, 2, 13 });
vector< int > arr1;
for ( int z : arr)
arr1.push_back(z);
operations(queries, arr1);
print(arr1);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void doOperation(
ArrayList<Integer> arr, int o)
{
int ind = - 1 ;
int n = arr.size();
for ( int i = 0 ; i < n; i++) {
if (arr.get(i) == o) {
ind = i;
break ;
}
}
if (ind == - 1 )
arr.add(o);
else
reverse(arr, ind, n - 1 );
}
static void reverse(
ArrayList<Integer> arr, int l,
int r)
{
while (l < r) {
int tmp = arr.get(l);
arr.set(l, arr.get(r));
arr.set(r, tmp);
l++;
r--;
}
}
static void print(ArrayList<Integer> arr)
{
for ( int x : arr) {
System.out.print(x + " " );
}
}
static void operations(
int queries[],
ArrayList<Integer> arr)
{
for ( int x : queries)
doOperation(arr, x);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int x = 3 ;
int queries[] = { 12 , 2 , 13 };
ArrayList<Integer> arr1
= new ArrayList<>();
for ( int z : arr)
arr1.add(z);
operations(queries, arr1);
print(arr1);
}
}
|
Python3
def rev(arr, l, r):
while (l < r):
arr[l], arr[r] = (arr[r],
arr[l])
l + = 1
r - = 1
def doOperation(arr, o):
ind = - 1
n = len (arr)
for i in range (n):
if (arr[i] = = o):
ind = i
break
if (ind = = - 1 ):
arr.append(o)
else :
rev(arr,
ind, n - 1 )
def print_array(arr):
for x in arr:
print (x, end = " " )
def operations(queries, arr):
for x in queries:
doOperation(arr, x)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
x = 3
queries = [ 12 , 2 , 13 ]
arr1 = []
for z in arr:
arr1.append(z)
operations(queries, arr1)
print_array(arr1)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void doOperation(List< int > arr, int o)
{
int ind = -1;
int n = arr.Count;
for ( int i = 0; i < n; i++)
{
if (arr[i] == o)
{
ind = i;
break ;
}
}
if (ind == -1)
arr.Add(o);
else
reverse(arr, ind, n - 1);
}
static void reverse(List< int > arr, int l,
int r)
{
while (l < r)
{
int tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++;
r--;
}
}
static void print(List< int > arr)
{
foreach ( int x in arr)
{
Console.Write(x + " " );
}
}
static void operations( int []queries,
List< int > arr)
{
foreach ( int x in queries)
doOperation(arr, x);
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int []queries = { 12, 2, 13 };
List< int > arr1 = new List< int >();
foreach ( int z in arr)
arr1.Add(z);
operations(queries, arr1);
print(arr1);
}
}
|
Javascript
<script>
function doOperation(arr, o)
{
let ind = -1;
let n = arr.length;
for (let i = 0; i < n; i++)
{
if (arr[i] == o)
{
ind = i;
break ;
}
}
if (ind == -1)
arr.push(o);
else
reverse(arr, ind, n - 1);
}
function reverse(arr, l, r)
{
while (l < r)
{
let tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++;
r--;
}
}
function print(arr)
{
document.write(arr.join( " " ));
}
function operations(queries, arr)
{
for (let x = 0; x < queries.length; x++)
doOperation(arr, queries[x]);
}
let arr = [ 1, 2, 3, 4 ];
let x = 3;
let queries = [ 12, 2, 13 ];
operations(queries, arr);
print(arr);
</script>
|
Time Complexity: O(N*X) where N is the size of the given array and X is the number of queries.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...