📌  相关文章
📜  查询以搜索数组中的元素并根据给定条件修改数组

📅  最后修改于: 2021-04-29 04:16:36             🧑  作者: Mango

给定一个由n个整数和一个整数X组成的数组arr [] ,任务是在执行由array operation []表示的X个查询后打印该数组。每个查询的任务如下:

  • 如果数组包含整数操作[i] ,则将子数组从找到操作[i]的索引开始,反转到数组的末尾。
  • 否则,将操作[i]插入到数组的末尾。

例子:

方法:最简单的方法是,对于每个查询,搜索整个数组以检查所关注的整数是否存在。如果存在于索引i处,并且数组的当前大小为N ,则反转子数组{arr [i],…arr [N – 1]} 。否则,将搜索到的元素插入数组的末尾。请按照以下步骤解决问题:

  1. 创建一个函数以线性搜索数组中元素的索引。
  2. 现在,对于每个查询,如果给定数组中不存在给定元素,请将其附加到数组末尾。
  3. 否则,如果它存在于任何索引i处,则将子数组从索引i到末尾反转。
  4. 完成上述步骤后,打印结果数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to reverse the subarray
// over the range [i, r]
void rev(vector &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 &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 &arr)
{
     
    // Traverse the array arr[]
    for(int x : arr)
    {
         
        // Print element
        cout << x << " ";
    }
}
 
// Function to perform operations
void operations(vector &queries,
                vector &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 queries({ 12, 2, 13 });
 
    // Add elements to the vector
    vector 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 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 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 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 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 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 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 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 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 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 arr1 = new List();
 
    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


输出
1 12 4 3 2 13 

时间复杂度: O(N * X),其中N是给定数组的大小,X是查询数。
辅助空间: O(N)