Interview question:- Subarray Swapping

Revision en4, by bablu_45, 2019-10-09 17:09:13

We are given an array of size 'N' with 'Q' queries. Each query contains 3 integers (A,B and C) which divides the entire array into 4 parts or subarrays, [0,A-1],[A,B],[B+1,C] and [C+1,N-1]. We need to swap first and second subarray and third and fourth subarray.

You need to output the final array after Q queries.

Constraints:

1<=N<=10^5

1<=Q<=10^5

1<=A[i]<=10^9.

Eg:- N=9 with 2 queries

9 2

1 2 3 4 5 6 7 8 9

2 4 6

1 3 5

In first query we are supposed to swap [0,1] and [2,4], [5,6] and [7,8]. So after first query array becomes [3,4,5,1,2,8,9,6,7]

In second query we are supposed to swap [0,0] and [1,3], [4,5] and [6,8] of modified array. So final array is [4,5,1,3,9,6,7,2,8]

How to solve this question?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English bablu_45 2019-10-09 17:09:13 30
en3 English bablu_45 2019-10-09 17:06:10 2 Tiny change: '\nEg:-\nN=7 with 2 qu' -> '\nEg:-\nN=9 with 2 qu'
en2 English bablu_45 2019-10-09 17:05:29 33
en1 English bablu_45 2019-10-09 17:04:37 742 Initial revision (published)