Last Updated: 22 Apr, 2021

Ninja And Movie Dilemma

Moderate
Asked in company
Facebook

Problem statement

Ninja has a list of movies and other details regarding the movies. The details of each Movie with Ninja are [ 'MovieID', 'Rating' of the Movie, Whether Movie is 'Age' Restricted or not, 'Price' for the movie ticket, 'Distance' of the theater]. If Ninja goes to the theater with his family, he will choose movies with 'Age' Restriction, but if he plans to go with his friends, he can select any movie from the movie list. Also, Ninja has constraints on the maximum 'Distance' of the theater he chooses and the 'Price' he pays for each ticket. Can you end the Ninja’s Dilemma?

You have to print the array/list of 'MovieID's from the list of movies, ordered by 'Rating' of the Movie from highest to lowest. For movies with the same 'Rating', order them by 'MovieID' from highest to lowest. Do not forget to consider the 'Age' Restriction factor!! ( 'Age' Restriction will be 1 for 'Age' Restricted Movies and 0 otherwise.)

Note : The list of movies is denoted by the array/list ‘movie’.

Input Format :
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain a single integer ‘N’ which denotes the number of Movies in the list with Ninja.

The next ‘N’ lines contain the five space-separated integers ‘movie[i][0]’,  ‘movie[i][1]’,  ‘movie[i][2]’,  ‘movie[i][3]’,  ‘movie[i][4]’ , where ‘movie[i][0]’ is the MovieID of the ‘i-th’ movie, ‘movie[i][1]’ is the rating of the ‘i-th’ movie,  ‘movie[i][2]’ indicates whether ‘i-th’ movie is Age Restricted or not,  ‘movie[i][3]’ is the price of the ticket for the ‘i-th’ movie and  ‘movie[i][4]’ is the distance of the theater where the ‘i-th’ movie is being shown.

The next line contains three space separated integers ‘AgeRes’ , ‘MaxPrice’, ‘MaxDistance’ where ‘AgeRes’ will be 1 if Age Restricted Movies has to be selected and 0 otherwise, ‘MaxPrice’ is the maximum amount of price that Ninja can spend on one ticket and ‘MaxDistance’ is the maximum distance ninja can travel from home. 
Output Format :
For each test case, print the array/list of ‘MovieID’s after filtering as discussed above.

Output for every test case will be printed in a separate line.

Note: You don’t need to print anything; It has already been taken care of.
Constraints :
1 <= T <= 100
1 <= N <= 10000
1 <= movie[ i ][ 0 ], movie[ i ][ 1 ], movie[ i ][ 3 ], movie[ i ][ 4 ] <= 100000  for each ‘i-th’ movie
0 <= movie[ i ][ 2 ], AgeRes <= 1
1 <= MaxPrice, MaxDistance <= 100000 
All movie[ i ][ 0 ] ( MovieIDs ) are distinct.

Where ‘T’ is the number of test cases.

Where 'N' is the number of movies, ’movie[i][0]’ is the ‘MovieID’ of the ‘i-th’ movie, ‘movie[i][1]’ is the ‘Rating’ of the ‘i-th’ movie,  ‘movie[i][2]’ indicates whether ‘i-th’ movie is ‘Age’ Restricted or not,  ‘movie[i][3]’ is the ‘Price’ of the ticket for the ‘i-th’ movie and  ‘movie[i][4]’ is the distance of the theater where the ‘i-th’ movie is being shown.

Where ‘AgeRes’ will be 1 if ‘Age’ Restricted Movies has to be selected and 0 otherwise, ‘MaxPrice’ is the maximum amount of price that Ninja can spend on one ticket and ‘MaxDistance’ is the maximum distance ninja can travel from home. 


Time limit: 1 sec

Approaches

01 Approach

The basic idea is to iterate the list of movies and filter the movies based on three conditions of Age Restriction, Maximum Price willing to pay for the ticket and the Maximum Distance willing to cover for the theater. If the movie passes through all the conditions, then its ‘Rating’ and ‘MovieID’ is stored as a ‘PAIR’ in the vector/list ‘result’. After the filtering, sort the obtained list of the filtered movies that is the vector/list ‘result’. Now, the vector/list ‘result’ will contain all the filtered movies in the sorted order from lowest to highest and hence reverse the vector/list ‘result’ because we wanted the list with the ordering from highest to lowest and at last return it.

 

Algorithm:

 

  1. Declare the vector/list ‘result’ which will store the pairs of [‘MovieID’, ‘Rating’] of the filtered movies from the movie list.
  2. Declare the vector/list ‘answer’ which will store the required order of ‘MovieID’ which needs to be returned.
  3. Run a loop for ‘i’ to the length of ‘movie’:
    • If ‘AgeRes’ is 1 and ‘movie[i][2]’ is 0:
      • Skip the current movie.
    • If ‘movie[i][3]’ is greater than ‘MaxPrice’:
      • Skip the current movie.
    • If ‘movie[i][4]’ is greater than ‘MaxDistance’:
      • Skip the current movie.
    • Add the [ ‘Rating’, ‘MovieID’ ] pair to the vector/list ‘result’.
  4. Sort the obtained vector/list ‘result’ after adding the filtered movies.
  5. Reverse the vector/list ‘result’.
  6. Store the ‘MovieID’ of every pair from vector/list ‘result’ to the vector/list ‘answer’.
  7. Finally, return ‘answer’.