Ninja's Encryption

Easy
0/40
Average time to solve is 15m
profile
Contributed by
5 upvotes
Asked in companies
AmazonMakeMyTripGrab

Problem statement

Ninja has created his own encryption technique to encrypt a number. He makes use of the logic behind factorial. In a factorial, we multiply the number by its previous number and so on but if we want to encrypt a number we don’t multiply in every step like in the case of factorial but multiply, divide, add and subtract and repeat in the same order.

So your task is to find the encrypted form of a number using the ninja encryption technique and you were being provided with the number.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains a single integer β€˜N’ denoting the given number.
Output Format:
For each test case, print a single line containing the encrypted form of the number.

The output of each test case will be 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 <= 5000

Time Limit: 1 sec
Sample Input 1:
2
5
8
Sample Output 1:
7
9
Explanation For Sample Input 1:
Test Case 1:
For the first test case, given number is β€˜5’ so using the ninja encryption technique we follow the steps: ( 5 * 4 / 3 + 2 - 1 ) = 7

Test Case 2:
For the first test case, the given number is  β€˜8’ so using the ninja encryption technique we follow the steps: ( 8 * 7 / 6 + 5 - 4 * 3 / 2 - 1 ) = 9

Sample Input 2:

1
12

Sample Output 2:

13
Hint

Can you think of applying operations in the given order until we reach β€˜1’?

Approaches (2)
Brute Approach

The idea here is very simple as you can see that we are simply applying operations like multiply and divide and alternatively we are adding and subtracting the result. So we just have to find the result of every three numbers in every iteration and we can simply do the required operations alternatively.

 

Algorithm:

 

  • Firstly we declare three variables β€˜ans’ for storing the final answer, β€˜result’ for storing the evaluation of multiplication, division, and a boolean type flag which help us in alternatively subtracting and adding.
  • Now iterate until our β€˜N’ is greater than β€˜0’.
  • In every iteration we check
    • If β€˜N >= 3 we store result = N * (N - 1) / (N - 2) and update β€˜N = N - 3’
    • Else if N == 2 we store result = N * N - 1 and update β€˜N = N - 2’
    • Else if N == 1 we store result = N and update β€˜N = N - 1’
  • Now we check β€˜flag’ == true then we add that β€˜result’ to β€˜ans’ i.e. do ans += result and make flag = false.
  • Else if flag == false then we subtract result from β€˜ans’ i.e.do  ans -= result
  • Now after division, we know we have to add so we check
    • If N >= 1 we add β€˜N’ in the result i.e do result += N and update β€˜N = N - 1’
  • After this, we simply return the β€˜ans’.
Time Complexity

O(N), where β€˜N’ represents the given number.

 

As we are traversing up to when the number is greater than 0 so overall complexity becomes equal to O(N).

Space Complexity

O(1)

 

As no extra space is required.

Code Solution
(100% EXP penalty)
Ninja's Encryption
Full screen
Console