Last Updated: 24 Apr, 2021

The Hero Test - 1

Easy
Asked in company
Nagarro Software

Problem statement

Ninja wants the hero certificate to get the support of local police and government to help the people. But to get the hero certificate he needs to clear an exam.

The exam consists of ‘N’ questions from ‘1’ to ‘N’. To clear the exam Ninja needs to solve in a particular order. He needs to leave one question after solving one question(assume that after ‘N’th question he will come back to the first question). He will perform this action till all questions are solved. Help Ninja in exams by telling him the order.

For Example:
If there are ‘5’ questions then the order will be:
“ 2 4 1 5 3 ”
Input Format:
The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains a single integer ‘N’ denoting the number of questions.
Output Format :
For each test case, return the order of the questions separated by space.

The output for each test case is printed in a separate line.

Note:

You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 3000

Where ‘T’ is the number of test cases, ‘N’ is the number of questions.

Time limit: 1 second.

Approaches

01 Approach

The idea here is to implement the questions as it says because even in the worst case the constraints allow brute force solution.

We will maintain an array/list to check whether a particular question is solved or not.

Algorithm:

  • Declare an array ‘VIS’ of size ‘N’ initialized with ‘0’.
  • Declare two integers ‘V’ := 0 and ‘I’ := 1.
  • Repeat the steps until ‘V’ < ‘N’
    • ‘I’ := findNext(‘VIS’, ‘I’, ‘N’)
    • ‘I’ ++
    • ‘I’ %= ‘N’
    • If(‘I’ == 0)
      • ‘I’ = ‘N’.
    • ‘SOL’.push(‘I’)
    • VIS[‘I’] = 1.
    • ‘V’++
  • Return ‘SOL’.

 

Description of ‘findNext’ function.

 

This function will take three parameters :

  • ‘I’: An integer to keep track of the current index of the array.
  • ‘N’: An integer denoting the total numbers of elements.
  • ‘VIS’: An array ‘VIS’ to check whether a number is already visited or not.

int findNext(VIS, I, N):

  • Repeat the steps until VIS[I] != 0
    • I++
    • I %= N
    • If( I == 0)
      • I = N
  • Return I.