Find the positions of given people after T time using their starting time and direction

• Last Updated : 15 Dec, 2021

There’s a circular track of length N and given two arrays start[] and direct[] of size M and an integer T, where start[I] represents the starting point of the ith person and direct[I] represents the direction, clockwise if direct[I] is 1, anti-clockwise if direct[I] is -1, the task is to find the positions of all people after T units of time.

Note: Every person moves 1 unit distance in 1 unit of time.

Examples:

Input: N = 5, M = 2, T = 1, start[] = {2, 3}, direct[] = {1, -1}
Output: 3 2
Explanation: Given circle has 5 points from 1 to 5 and there are two men let say A and B. A starts from 2nd point and moves clockwise as direct[0] = 1, so after 1 minutes he will be at point 3. Similarly, B starts from 3rd point moving anticlockwise, so after 1 min he will be at point 2. So, ans array contains [3, 2] after sorting it becomes [2, 3].

Input: N = 4, M = 3, T = 2, start[] = {2, 1, 4}, direct[] = {-1, 1, -1}
Output: 4 3 2

Approach: The idea to solve this problem is based on the observation of taking advantage of having a circular track. Find the point after T units of time and take the modulo to find the answer. Follow the steps below to solve the problem:

• Iterate over the range [0, M) using the variable i and perform the following tasks:
• Initialize the variable t_moves as direct[I]*T.
• Initialize the variable end_pos as (((start[i] + t_moves) % N) + N) % N.
• If end_pos is 0, then set the value of start[I] as N else set it as end_pos.
• After performing the above steps, print the values of the array start[] as the answer.

Below is the implementation of the above approach:

C++

 `// C++ program for the above approach``#include ``#include ``using` `namespace` `std;` `// Function to find the final position``// of men's after T minutes``int``* positionAfterTMin(``int` `N, ``int` `M, ``int` `T, ``int` `start[],``                       ``int` `direct[])``{``    ``// Find the location of the i-th``    ``// person after T minutes``    ``for` `(``int` `i = 0; i < M; i++)``    ``{``      ` `        ``// Total points moved m by i-th``        ``// person in T minutes``        ``int` `t_moves = direct[i] * T;` `        ``// As the path is circular then``        ``// there is a chance that the``        ``// person will traverse the same``        ``// points again``        ``int` `end_pos = (((start[i] + t_moves) % N) + N) % N;` `        ``// Storing location of the``        ``// i-th person``        ``start[i] = (end_pos == 0) ? N : end_pos;``    ``}` `    ``// Returning array which contains``    ``// location of every person moving``    ``// in time T units``    ``return` `start;``}` `// Driver Code``int` `main()``{` `    ``int` `N = 4;``    ``int` `M = 3;``    ``int` `T = 2;``    ``int` `start[] = { 2, 1, 4 };``    ``int` `direct[] = { -1, 1, -1 };``    ``int``* ans = positionAfterTMin(N, M, T, start, direct);``    ``for` `(``int` `i = 0; i < M; i++) {``        ``cout << *(ans + i) << ``" "``;``    ``}``    ``return` `0;``}` `// This code is contributed by Kdheeraj.`

Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `    ``// Function to find the final position``    ``// of men's after T minutes``    ``public` `static` `int``[] positionAfterTMin(``        ``int` `N, ``int` `M, ``int` `T,``        ``int``[] start, ``int``[] direct)``    ``{` `        ``// Find the location of the i-th``        ``// person after T minutes``        ``for` `(``int` `i = ``0``; i < M; i++) {` `            ``// Total points moved m by i-th``            ``// person in T minutes``            ``int` `t_moves = direct[i] * T;` `            ``// As the path is circular then``            ``// there is a chance that the``            ``// person will traverse the same``            ``// points again``            ``int` `end_pos``                ``= (((start[i] + t_moves)``                    ``% N)``                   ``+ N)``                  ``% N;` `            ``// Storing location of the``            ``// i-th person``            ``start[i] = (end_pos == ``0``) ? N : end_pos;``        ``}` `        ``// Returning array which contains``        ``// location of every person moving``        ``// in time T units``        ``return` `start;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `N = ``4``;``        ``int` `M = ``3``;``        ``int` `T = ``2``;``        ``int``[] start = { ``2``, ``1``, ``4` `};``        ``int``[] direct = { -``1``, ``1``, -``1` `};` `        ``int``[] ans = positionAfterTMin(``            ``N, M, T, start, direct);` `        ``for` `(``int` `i = ``0``; i < ans.length; i++) {``            ``System.out.print(ans[i] + ``" "``);``        ``}``    ``}``}`

Python3

 `# Python program for the above approach` `# Function to find the final position``# of men's after T minutes``def` `positionAfterTMin(N, M, T, start, direct):` `    ``# Find the location of the i-th``    ``# person after T minutes``    ``for` `i ``in` `range``(M):` `        ``# Total points moved m by i-th``        ``# person in T minutes``        ``t_moves ``=` `direct[i] ``*` `T` `        ``# As the path is circular then``        ``# there is a chance that the``        ``# person will traverse the same``        ``# points again``        ``end_pos ``=` `(((start[i] ``+` `t_moves) ``%` `N) ``+` `N) ``%` `N` `        ``# Storing location of the``        ``# i-th person``        ``if` `end_pos ``=``=` `0``:``            ``start[i] ``=` `N``        ``else``:``            ``start[i] ``=` `end_pos` `    ``# Returning array which contains``    ``# location of every person moving``    ``# in time T units``    ``return` `start` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``N ``=` `4``    ``M ``=` `3``    ``T ``=` `2``    ``start ``=` `[``2``, ``1``, ``4``]``    ``direct ``=` `[``-``1``, ``1``, ``-``1``]``    ``ans ``=` `positionAfterTMin(N, M, T, start, direct)``    ``print``(ans)` `# This code is contributed by Potta Lokesh`

C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `    ``// Function to find the final position``    ``// of men's after T minutes``    ``public` `static` `int``[] positionAfterTMin(``        ``int` `N, ``int` `M, ``int` `T,``        ``int``[] start, ``int``[] direct)``    ``{` `        ``// Find the location of the i-th``        ``// person after T minutes``        ``for` `(``int` `i = 0; i < M; i++) {` `            ``// Total points moved m by i-th``            ``// person in T minutes``            ``int` `t_moves = direct[i] * T;` `            ``// As the path is circular then``            ``// there is a chance that the``            ``// person will traverse the same``            ``// points again``            ``int` `end_pos``                ``= (((start[i] + t_moves)``                    ``% N)``                   ``+ N)``                  ``% N;` `            ``// Storing location of the``            ``// i-th person``            ``start[i] = (end_pos == 0) ? N : end_pos;``        ``}` `        ``// Returning array which contains``        ``// location of every person moving``        ``// in time T units``        ``return` `start;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `N = 4;``        ``int` `M = 3;``        ``int` `T = 2;``        ``int``[] start = { 2, 1, 4 };``        ``int``[] direct = { -1, 1, -1 };` `        ``int``[] ans = positionAfterTMin(``            ``N, M, T, start, direct);` `        ``for` `(``int` `i = 0; i < ans.Length; i++) {``            ``Console.Write(ans[i] + ``" "``);``        ``}``    ``}``}` `// This code is contributed by shivanisinghss2110`

Javascript

 `// Javascript program for the above approach` `// Function to find the final position``// of men's after T minutes``function` `positionAfterTMin(N, M, T, start, direct)``{` `  ``// Find the location of the i-th``  ``// person after T minutes``  ``for` `(let i = 0; i < M; i++)``  ``{``  ` `    ``// Total points moved m by i-th``    ``// person in T minutes``    ``let t_moves = direct[i] * T;` `    ``// As the path is circular then``    ``// there is a chance that the``    ``// person will traverse the same``    ``// points again``    ``let end_pos = (((start[i] + t_moves) % N) + N) % N;` `    ``// Storing location of the``    ``// i-th person``    ``start[i] = end_pos == 0 ? N : end_pos;``  ``}` `  ``// Returning array which contains``  ``// location of every person moving``  ``// in time T units``  ``return` `start;``}` `// Driver Code``let N = 4;``let M = 3;``let T = 2;``let start = [2, 1, 4];``let direct = [-1, 1, -1];``let ans = positionAfterTMin(N, M, T, start, direct);``for` `(let i = 0; i < 3; i++) {``  ``document.write(ans[i] + ``" "``);``}` `// This code is contributed by _saaurabh_jaiswal.`

Output:

`4 3 2`

Time Complexity: O(M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up