**School Renovation: ** Alice and Bob want to renovate and old school in his city. There are N classrooms in a school and ith classroom have a capacity of A[i] students. Bob is a builder and follows the instruction of alice.

Alice gives Q instructions of the following type

- 1L: Move L classrooms Left
- 2R: Move R classrooms Right
- 3XY: Remove the next classroom and add new classrooms of capacity X and Y respectively to the right of the current classroom.(after performing this operation classroom number changes accordingly).

Note: The queries are always valid

**Task:**

Initially Bob is in ith classroom after performing all instructions of alice, print the capacity of all classroom from 1 to total classrooms.

Examples:

**Assumptions:**

N=5 , Q=2

A={1,2,3,4,5}

queries={{2,3},{3,2,6}}

Initially, class_no=1

**Approach:**

- Alice instructs him to go right by 3 classrooms so class_no=(1+3)=4
- Now he has to remove the next classroom so new A will be {1,2,3,4} and he has to add two classrooms with capacites of 2 and 6 so the new A will be {1,2,3,4,2,6} and class_no=4.

**Input Format:**

- The first line contains two space seperated integers N,Q denoting the initial number of classrooms and the number of instructions.
- The second line contains N space separated integers denoting initial classroom capacity .
- Next Q lines contain queries of the form
- 1L
- 2R
- 3XY

**Output Format:**

After performing all instructions of alice, print the capacity of all classrooms from 1 to k (k- total number of the classroom in renovated classroom).

**Example2:**

N=5 , Q=4

A={1,2,3,4,5}

queries={{2,3},{3,1,1,},{1,2},{3,5,7}}

**Output:**

1,2,5,7,4,1,1

**Algorithm to Solve:**

- Initially take every input from user like values of N,Q,A,queries
- Then convert array A to “ArrayList” type (as we need to grow arraylist size in future).
- Then use 2 outer for loops for iterating through elements of 2D array.
- Inside for loops have 3 if conditions each for 1 , 2, 3.
- Then according perform the tasks as mentioned in problem like if initial number in query is 1 then move class_no pointer towards left by specified positions.
- Similarly do same thing for other queries as well.
- At the end if first element in query is 3 then remove the next element of class_no pointer and add two elements in that position.
- Finally print an array of elements representing final capacity of classrooms.

**JAVA Code **

package arrayproblems; import java.util.ArrayList; class Problem22 { public static void main(String[] args) { // written by VRASHIKESH PATL int N=5; int Q=4; int[] array1= {1,2,3,4,5}; int[][] array2= {{2,3},{3,1,1},{1,2},{3,5,7}}; int class_no=1; ArrayList array3=new ArrayList(); for(int i=0;i<array1.length;i++) { array3.add(array1[i]); } for(int i=0;i<array2.length;i++) { for(int j=0;j<array2[i].length;j++) { if(array2[i][j]==1) { class_no=class_no-array2[i][j+1]; break; } else if(array2[i][j]==2) { class_no=class_no+array2[i][j+1]; break; } else if(array2[i][j]==3) { array3.remove(class_no); array3.add(class_no, array2[i][j+1]); array3.add(class_no+1, array2[i][j+2]); break; } } } for(int i=0;i<array3.size();i++) { System.out.print(array3.get(i)+" "); } } }

**Watch Step by Step Explanation for Above Code **

**Similar off campus coding questions **

SHL off campus coding questions

Count Buildings Hacker Earth coding question with solution

Pingback: Dynamic Programming HACKER EARTH questions - Is It Actually

Pingback: Off Campus Coding Question on Arrays - Is It Actually