MISSION WITH MI6: Last year a hacking took place in the safest and crucial defense network of the US. Hackers downloaded crucial defense-related confidential files and documents from their network and after that, they flew to Dubai for delivery of the documents to some terror-related outfits. Investigation Agent MI6 found that there is a meeting scheduled between parties in the hotel room of Burj Khalifa. MI6 agency formed a team and send their best agents for recovering documents and catching hackers.
After reaching Dubai, agents check-in to a hotel and found a way to go to the room by hacking surveillance cameras but there is very high security on the floor where the meeting is supposed to be held. So they decided to take another route by walking on the wall of the hotel building with the help of electromagnetic repulsive force. They have a gadget having this type of technology that helps them in walking on the wall of the hotel.
There are various metal square-shaped sheets attached to the outside wall of the hotel. A specialist agent Bob decided to walk on the wall and his colleague guide him and instruct him that on which square sheet he had to walk. Bob’s colleague sends him a set of instructions which helps in determining which particular sheet he has to stand. A set of instructions is a string that consists of letters ‘L’, ‘R’, ‘U’, ‘D’ .
For simplicity let’s assume that Bob is staying at point (0, 0) on a big wall. He consistently fulfills all the instructions sent by his colleague. Let’s assume that now he is staying at point (X, Y), then depending on what is the current instruction he moves in one direction:
‘L’: from (X,Y) moves to point (X,Y-1) ‘R’: from (X,Y) moves to point (X,Y+1) ‘U’: from (X,Y) moves to point (X-1,Y) 'D' : from (X,Y) moves to point (X+1,Y)
Initially, all the points are good ones and there is no crack on any point or sheet. But if Bob already visited some point at any time then next time this point cracked. Every time Bob makes a step on the cracked points he slips on the wall.
You are given a string S which tells the set of instructions for Bob. Your work is to calculate how many times Bob slips from his position.
Input 1: It will be a string that tells the string S which denotes the set of instructions for Bob.
Constraints 1 ≤ S ≤ 105
Output Format: It will be an integer which tells that how many times Bob slips from his position
Sample TestCase 1
Input RRULDL Output 2 Here is my solution:
Bob route consists of the following points:
0,0 ---> 0,1 ---> 0,2 ----> -1,2 ---> -1,1 ----> 0,1(cracks) ----> 0,0 (cracks)
So, there will be 2 points where Bob visited again, and so he slips 2 steps.
Craziest Prisons and Jails: Some prisons are known for being violent and treating prisoners like animals, leading to inhumane and unethical tactics for keeping the peace. There is a prison located in Norway, Halden is often described as peaceful and relaxing. The prisoners are treated to good food, hot coffee, and cells that boast televisions, mini-fridges, private bathrooms, and scenic views of the surrounding forest.
Norway’s government build 3 new prisons each one is having its own unique design different from the other. Each prison consists of cells arranged into a one, two, or three-dimensional grid. Jailer can fill N prisoners into the cells.
The Distance between two cells is the smallest number of moves that a prisoner would need in order to reach one cell from the other. In one move, the prisoner may step into one of the adjacent cells.
Two prisoners can hear each other if the distance between their cells is at most D. Your task is to calculate how many pairs of prisoners there are such that one prisoner can hear the other prisoner.
Example: Suppose the prison will of 3-dimensional type then it will look like below
Input 1: It will be the integer which tells the type of prison P
Input 2: It will be the integer which tells the number of prisoners N
Input 3: It will be the integer which tells the largest distance D at which two prisoners can hear each other
Input 4: It will be the integer which tells the size of the prison S; (the largest coordinate allowed to appear in the input):
When P=1, S will be at most 75000000.
When P=2, S will be at most 75000.
When P=3, S will be at most 75.
Input 5: It will be a string array which tells the coordinates of each prisoner. Array format will be like:
First-line will tell the total number of rows i.e. total number of prisoners N
Each of the following N lines contains P integers separated by single commas – the coordinates of one prisoner. Each coordinate will be between 1 and S (inclusive). More than one prisoner may occupy the same cell. If the P is one, then there will be no comma – only single value will be there in each line.
1 ≤ P ≤ 3 1 ≤ N ≤ 100000 1 ≤ D ≤ 100000000
Output Format: It will be the single integer, the number of pairs of prisoners that can hear each other.
Sample TestCase 1:
Input 3 8 10 20 8 10,10,10 10,10,20 10,20,10 10,20,20 20,10,10 20,10,20 20,20,10 20,20,20 Output 12
Solution in C++:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course. In case you are prepared, test your skills using TCS, Wipro, Amazon and Microsoft Test Serieses.