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++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; Â
// Function to reverse the subarray // over the range [i, r] void rev(vector< int > &arr, int l, int r) { Â Â Â Â Â Â Â Â Â // Iterate over the range [l, r] Â Â Â Â while (l < r) Â Â Â Â { Â Â Â Â Â Â Â Â int tmp = arr[l]; Â Â Â Â Â Â Â Â arr[l] = arr[r]; Â Â Â Â Â Â Â Â arr[r] = tmp; Â Â Â Â Â Â Â Â l++; Â Â Â Â Â Â Â Â r--; Â Â Â Â } } Â
// Function that perform the given // queries for the given array void doOperation(vector< int > &arr, int o) {          // Search for the element o     int ind = -1; Â
    // Current size of the array     int n = arr.size(); Â
    for ( int i = 0; i < n; i++)     {                  // If found, break out of loop         if (arr[i] == o)         {             ind = i;             break ;         }     } Â
    // If not found, append o     if (ind == -1)         arr.push_back(o); Â
    // Otherwise, reverse the     // subarray arr[ind] to arr[n - 1]     else         rev(arr, ind, n - 1); } Â
// Function to print the elements // in the vector arr[] void print(vector< int > &arr) {          // Traverse the array arr[]     for ( int x : arr)     {                  // Print element         cout << x << " " ;     } } Â
// Function to perform operations void operations(vector< int > &queries, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â vector< int > &arr) { Â Â Â Â for ( auto x : queries) Â Â Â Â Â Â Â Â doOperation(arr, x); } Â
// Driver Code int main() { Â Â Â Â Â Â Â Â Â // Given array arr[] Â Â Â Â int arr[] = { 1, 2, 3, 4 }; Â Â Â Â int x = 3; Â
    // Given queries     vector< int > queries({ 12, 2, 13 }); Â
    // Add elements to the vector     vector< int > arr1; Â
    for ( int z : arr)         arr1.push_back(z); Â
    // Perform queries     operations(queries, arr1); Â
    // Print the resultant array     print(arr1); } Â
// This code is contributed by SURENDRA_GANGWAR |
Java
// Java program for the above approach Â
import java.io.*; import java.util.*; Â
class GFG { Â
    // Function that perform the given     // queries for the given array     static void doOperation(         ArrayList<Integer> arr, int o)     { Â
        // Search for the element o         int ind = - 1 ; Â
        // Current size of the array         int n = arr.size(); Â
        for ( int i = 0 ; i < n; i++) { Â
            // If found, break out of loop             if (arr.get(i) == o) {                 ind = i;                 break ;             }         } Â
        // If not found, append o         if (ind == - 1 )             arr.add(o); Â
        // Otherwise, reverse the         // subarray arr[ind] to arr[n - 1]         else             reverse(arr, ind, n - 1 );     } Â
    // Function to reverse the subarray     // over the range [i, r]     static void reverse(         ArrayList<Integer> arr, int l,         int r)     {         // Iterate over the range [l, r]         while (l < r) {             int tmp = arr.get(l);             arr.set(l, arr.get(r));             arr.set(r, tmp);             l++;             r--;         }     } Â
    // Function to print the elements     // in the ArrayList arr[]     static void print(ArrayList<Integer> arr)     {         // Traverse the array arr[]         for ( int x : arr) { Â
            // Print element             System.out.print(x + " " );         }     } Â
    // Function to perform operations     static void operations(         int queries[],         ArrayList<Integer> arr)     {         for ( int x : queries)             doOperation(arr, x);     } Â
    // Driver Code     public static void main(String[] args)     {         // Given array arr[]         int arr[] = { 1 , 2 , 3 , 4 };         int x = 3 ; Â
        // Given queries         int queries[] = { 12 , 2 , 13 }; Â
        // Add elements to the arraylist         ArrayList<Integer> arr1             = new ArrayList<>(); Â
        for ( int z : arr)             arr1.add(z); Â
        // Perform queries         operations(queries, arr1); Â
        // Print the resultant array         print(arr1);     } } |
Python3
# Python3 program for # the above approach Â
# Function to reverse the # subarray over the range # [i, r] def rev(arr, l, r): Â
    # Iterate over the     # range [l, r]     while (l < r):         arr[l], arr[r] = (arr[r],                           arr[l])         l + = 1         r - = 1 Â
# Function that perform the given # queries for the given array def doOperation(arr, o): Â
    # Search for the     # element o     ind = - 1 Â
    # Current size of     # the array     n = len (arr) Â
    for i in range (n): Â
        # If found, break out         # of loop         if (arr[i] = = o): Â
            ind = i             break Â
    # If not found, append o     if (ind = = - 1 ):         arr.append(o) Â
    # Otherwise, reverse the     # subarray arr[ind] to     # arr[n - 1]     else :         rev(arr,             ind, n - 1 ) Â
# Function to print the # elements in the vector # arr[] def print_array(arr): Â
    # Traverse the     # array arr[]     for x in arr: Â
        # Print element         print (x, end = " " ) Â
# Function to perform # operations def operations(queries, arr): Â
    for x in queries:         doOperation(arr, x) Â
# Driver Code if __name__ = = "__main__" : Â
    # Given array arr[]     arr = [ 1 , 2 , 3 , 4 ]     x = 3 Â
    # Given queries     queries = [ 12 , 2 , 13 ] Â
    # Add elements to the vector     arr1 = [] Â
    for z in arr:         arr1.append(z) Â
    # Perform queries     operations(queries, arr1) Â
    # Print the resultant array     print_array(arr1) Â
# This code is contributed by Chitranayal |
C#
// C# program for the above approach using System; using System.Collections.Generic; Â
class GFG{ Â
// Function that perform the given // queries for the given array static void doOperation(List< int > arr, int o) {          // Search for the element o     int ind = -1; Â
    // Current size of the array     int n = arr.Count; Â
    for ( int i = 0; i < n; i++)     {                  // If found, break out of loop         if (arr[i] == o)         {             ind = i;             break ;         }     } Â
    // If not found, append o     if (ind == -1)         arr.Add(o); Â
    // Otherwise, reverse the     // subarray arr[ind] to arr[n - 1]     else         reverse(arr, ind, n - 1); } Â
// Function to reverse the subarray // over the range [i, r] static void reverse(List< int > arr, int l, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int r) { Â Â Â Â Â Â Â Â Â // Iterate over the range [l, r] Â Â Â Â while (l < r) Â Â Â Â { Â Â Â Â Â Â Â Â int tmp = arr[l]; Â Â Â Â Â Â Â Â arr[l] = arr[r]; Â Â Â Â Â Â Â Â arr[r] =Â tmp; Â Â Â Â Â Â Â Â l++; Â Â Â Â Â Â Â Â r--; Â Â Â Â } } Â
// Function to print the elements // in the List []arr static void print(List< int > arr) {          // Traverse the array []arr     foreach ( int x in arr)     { Â
        // Print element         Console.Write(x + " " );     } } Â
// Function to perform operations static void operations( int []queries, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â List< int > arr) { Â Â Â Â foreach ( int x in queries) Â Â Â Â Â Â Â Â doOperation(arr, x); } Â
// Driver Code public static void Main(String[] args) {          // Given array []arr     int []arr = { 1, 2, 3, 4 };     //int x = 3; Â
    // Given queries     int []queries = { 12, 2, 13 }; Â
    // Add elements to the arraylist     List< int > arr1 = new List< int >(); Â
    foreach ( int z in arr)         arr1.Add(z); Â
    // Perform queries     operations(queries, arr1); Â
    // Print the resultant array     print(arr1); } } Â
// This code is contributed by gauravrajput1 |
Javascript
<script> Â
// Javascript program for the above approach Â
// Function that perform the given // queries for the given array function doOperation(arr, o) {          // Search for the element o     let ind = -1; Â
    // Current size of the array     let n = arr.length; Â
    for (let i = 0; i < n; i++)     {                  // If found, break out of loop         if (arr[i] == o)         {             ind = i;             break ;         }     } Â
    // If not found, append o     if (ind == -1)         arr.push(o); Â
    // Otherwise, reverse the     // subarray arr[ind] to arr[n - 1]     else         reverse(arr, ind, n - 1); } Â
// Function to reverse the subarray // over the range [i, r] function reverse(arr, l, r) { Â Â Â Â Â Â Â Â Â // Iterate over the range [l, r] Â Â Â Â while (l < r) Â Â Â Â { Â Â Â Â Â Â Â Â let tmp = arr[l]; Â Â Â Â Â Â Â Â arr[l] = arr[r]; Â Â Â Â Â Â Â Â arr[r] = tmp; Â Â Â Â Â Â Â Â l++; Â Â Â Â Â Â Â Â r--; Â Â Â Â } } Â
// Function to print the elements // in the ArrayList arr[] function print(arr) { Â Â Â Â document.write(arr.join( " " )); } Â
// Function to perform operations   function operations(queries, arr) {     for (let x = 0; x < queries.length; x++)         doOperation(arr, queries[x]); } Â
// Driver Code let arr = [ 1, 2, 3, 4 ]; let x = 3; Â
// Given queries let queries = [ 12, 2, 13 ]; Â
// Perform queries operations(queries, arr); Â
// Print the resultant array print(arr); Â
// This code is contributed by avanitrachhadiya2155 Â
</script> |
1 12 4 3 2 13
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!