We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Arrays
- Dynamic Array
- Discussions
Dynamic Array
Dynamic Array
Sort by
recency
|
1974 Discussions
|
Please Login in order to post a comment
Hi Team,
I have added my solution in C#, strangly it doesn't work in hackerrank. But it does work in Visual studio. Any suggestions on this?
My code goes like this:
Solution In C++
1 Create the Sequence List
3 process queries
Step 1: Function Declaration
The function
dynamicArray
is declared with two parameters:n
, which is the number of arrays in the dynamic array, andqueries
, which is an array of queries to be processed.Step 2: Initialization
lastAnswer
is initialized to0
. This variable will store the last answer to a query of type 2.idx
is initialized to-1
. This variable will be used to store the index calculated from the query.result
is initialized as an empty array. This array will store the results of queries of type 2.matrix
is initialized as an array ofn
empty arrays. This represents the dynamic array where elements will be added or queried.Step 3: Processing Queries
This
for...of
loop iterates over each query in thequeries
array.Step 4: Handling Query Type 1
1
, it means we are adding an element to the dynamic array.(query[1] ^ lastAnswer) % n
. This is a bitwise XOR operation between the second element of the query andlastAnswer
, followed by a modulo operation withn
.matrix
array using thepush
method.Step 5: Handling Query Type 2
1
, it means we are querying an element from the dynamic array.idx
.lastAnswer
, and it's also added to theresult
array.Step 6: Returning the Result
After processing all queries, the function returns the
result
array, which contains the results of all queries of type 2.This example demonstrates how to use the
dynamicArray
function. It initializesn
to5
and provides a set of queries. The function processes these queries and returns the results, which are then logged to the console.