给定一个由N 个整数和一个整数X组成的数组arr[] ,任务是在执行由数组操作 []表示的X查询之后打印该数组。每个查询的任务如下:
- 如果数组包含整数operations[i] ,则将子数组从找到operations[i]的索引开始反转到数组的末尾。
- 否则,将操作 [i]插入到数组的末尾。
例子:
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
方法:最简单的方法是对每个查询搜索整个数组以检查相关整数是否存在。如果存在于索引i并且数组的当前大小为N ,则反转子数组{arr[i], … arr[N – 1]} 。否则,将搜索到的元素插入到数组的末尾。请按照以下步骤解决问题:
- 创建一个函数来线性搜索数组中元素的索引。
- 现在对于每个查询,如果给定的元素不存在于给定的数组中,则将其附加到数组的末尾。
- 否则,如果它出现在任何索引i 处,则反转从索引i开始到末尾的子数组。
- 完成上述步骤后,打印结果数组。
下面是上述方法的实现:
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live