- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find time taken to Execute the Tasks in A Based on the Order of Execution in B

The goal is to determine the minimum time required to complete the tasks in queue A based on the order of execution in queue B, given two queues A and B, each of size N, where:

Pop this task and run it if the task identified at the head of queue B is also at the head of queue A.

Pop the current task from queue A and push it at the end if the task discovered at the front of queue B is not also found at the front of queue A.

One unit of time is spent on each push and pop operation in a queue, and each job is completed in a fixed amount of time.

To solve this problem, we can simulate the execution of tasks in the order given by queue B, while keeping track of the current state of queue A. We will need to perform the following steps for each task in queue B:

Check if the task at the front of queue B matches the task at the front of queue A.

If the tasks match, execute the task by popping it from both queues.

If the tasks do not match, pop the task from the front of queue A and push it to the back of queue A.

Increment the time counter by 1 for each push and pop operation.

## Pseudocode

Here's the pseudocode for the execute_tasks function:

function execute_tasks(A, B): time = 0 while B is not empty: if A[0] == B[0]: task = A.pop(0) B.pop(0) time = time + 1 else: task = A.pop(0) A.append(task) time = time + 1 return time

Note that this assumes that A and B are both lists (or arrays) representing queues. The pop(0) method removes the first element from the list and returns it, while append(task) adds task to the end of the list. In Python, A[0] can be used to access the first element of the list A. The is not empty comparison checks whether the list is non-empty, and the while loop repeats until B is empty.

## Java Implementation

Following is the Java implementation of the above pseudocode

### Example

import java.util.ArrayList; import java.util.List; public class Main{ public static int execute_tasks(List<Integer> A, List<Integer> B) { int time = 0; while (!B.isEmpty()) { if (A.get(0).equals(B.get(0))) { int task = A.remove(0); B.remove(0); time++; } else { int task = A.remove(0); A.add(task); time++; } } return time; } public static void main(String args[]) { List<Integer> arrayList1 = new ArrayList<Integer>(); arrayList1.add(3); arrayList1.add(2); arrayList1.add(1); List<Integer> arrayList2 = new ArrayList<Integer>(); arrayList2.add(1); arrayList2.add(2); arrayList2.add(3); int time = Main.execute_tasks(arrayList1, arrayList2); System.out.println("Time Taken: "+time); } }

### Output

Time Taken: 6

The time complexity of this algorithm is O(N^2) because we may need to iterate over all tasks in queue A for each task in queue B. However, since the input size is small (N <= 100), this algorithm should be efficient enough for practical purposes.

The

**execute_tasks()**method in the provided solution has an O(N^2) time complexity, where N is the combined size of queues A and B. This is due to the possibility that, in the worstâˆ’case situation, we would have to repeat the process for every task in queue B. The operations done on queue A (pop and append) have an O(1) time complexity, but iterating through all of the tasks in queue A can have an O time complexity.(N).The function has an O(N) space complexity because both lists A and B must be kept in memory. The total quantity of tasks is what determines how much memory is used; not their execution order. The time counter and the present task are also stored in a few constantâˆ’size variables, but their impact on the overall space complexity is minimal.

## Using Java Map Data Structure

You can optimize this code by using a Map data structure to store the indices of tasks in queue A. This will allow us to quickly find the position of a task in A when it is not at the front of the queue, instead of having to search the entire queue every time.

## Java Implementation

### Example

import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; public class Demo{ public static int execute_tasks(List<Integer> A, List<Integer> B) { int time = 0; Map<Integer, Integer> taskIndex = new HashMap<>(); for (int i = 0; i < A.size(); i++) { taskIndex.put(A.get(i), i); } int nextIndex = 0; for (int i = 0; i < B.size(); i++) { int task = B.get(i); if (taskIndex.containsKey(task)) { int index = taskIndex.get(task); int numMoves = index - nextIndex; A.remove(index); A.add(0, task); time += numMoves + 1; nextIndex++; } else { A.remove(0); A.add(task); time += 2; } } return time; } public static void main(String args[]) { List<Integer> arrayList1 = new ArrayList<Integer>(); arrayList1.add(3); arrayList1.add(2); arrayList1.add(1); List<Integer> arrayList2 = new ArrayList<Integer>(); arrayList2.add(1); arrayList2.add(2); arrayList2.add(3); int time = Demo.execute_tasks(arrayList1, arrayList2); System.out.println("Time Taken: "+time); } }

### Output

Time Taken: 3

The **taskIndex()** map is used to store the indices of tasks in A. The for loop at the beginning of the function fills this map by iterating over the elements of A and storing their indices. The nextIndex() variable is used to keep track of the expected position of the next task in B in A.

In the for loop over B, if the current task in B is found in A, we use the taskIndex map to find its index in A and compute the number of moves required to bring it to the front of the queue. We then remove the task from its original position in A, add it to the front of A, and update time and nextIndex accordingly.

If the current task in B is not found in A, we simply remove the task at the front of A, add it to the end of A, and update time by 2.

## Using map in Python

We decrease the time complexity of locating a task's position from O(N) to O(1) by using a map to store the task indices in A, resulting in an overall time complexity of O(N), where N is the size of the queues. Because the taskIndex map is being used, the space complexity is still O(N).

## Implementation

### Example

def min_time(A, B): time = 0 i = 0 A_set = set(A) for b in B: if b in A_set: while A[i] != b: i = (i+1) % len(A) time += 1 time += 1 i = (i+1) % len(A) else: time += 1 return time A = [3, 2, 1] B = [1, 2, 3] time = min_time(A, B); print("Time Taken: ",time);

### Output

Time Taken: 7