Programming is an extremely vast concept and when you move towards more professional programming concepts, you learn how to solve problems in the least possible time and space complexity.

The Ninja Technique is one such highly effective and useful approach in programming that can help solve any programming problem.

While using this technique, the users can solve problems in half the time complexity than the regular approaches.

In this blog, we have incorporated an array-based problem to help you understand how to use ninja technique in a problem.

But, before we start the blog, learn all about the Ninja technique problem in the following section.

What is Ninja Technique in programming?

Ninja Technique in programming is basically an effective way of solving complex problems by basically using the dynamic programming approach.

We should be aware of the fact that programming is all about finding the fastest solution possible for a problem. This helps us in saving valuable time and money on any coding problem.

The dynamic programming approach facilitates solving problems at a much faster pace as compared to the other coding concepts. This is because it allows the system to memorize the solution to the problems so that they can be used in the future.

Hence, learning Ninja technique in programming is all about having mastery over the highly effective and useful concepts that can help save system space and time complexity of any given coding problem.

In our next section, we will try to demonstrate the use of ninja technique in a problem using the dynamic programming approach. 

This way, you can see how the time complexity gets reduced by a landslide after printing the output.

For this context, we are looking at an array based problem since they are one of the most common problems in programming.

How to tackle the Ninja Technique problem?

Let’s now talk about the problem based on 2D dynamic programming. We will use the Ninja Technique in order to understand the concept.

Keep in mind that before proceeding further, you need to have a basic level knowledge of dynamic programming in order to be able to solve this problem.

Problem Statement

A ninja is offered an N day training schedule. Within this schedule, he is entitled to perform three of the following activities:


Fighting practice 

Learning new moves

He is required to learn all of these activities every single day for his entire schedule. As a reward for completing these activities, he will be assigned merit points every single day.

He is not allowed to practice the same activity for two consecutive days within the training program. The challenge of this problem is to figure out the maximum number of merit points that can be earned by the Ninja within his N training days.

You have been provided a 2D array consisting of the points table of the size N3. This table tells us the merit points allotted for each activity for all the days within the training program. Calculate the maximum merit points that can be earned by the Ninja within N number of days.


Day 0 - 10, 50, 1 

Day 1 - 5, 100, 11

Answer Key

We will try the greedy algorithm first for solving this array-based problem.

Method 1: Using The Greedy Algorithm

The greedy algorithm basically considers the distinct characters from a set of data and finds the fastest possible solution to a problem.

Now, this may or may not make the algorithm entirely reliable hence, only by implementing this in a program can we confirm its reliability.

We are aiming to figure out the maximum number of merit points that can be earned by the Ninja.

For Day 0, we will try to consider the activity that earns 50 points to the Ninja

Similarly, for Day 1, we shall consider the activity that earns us 100 merit points but, since the same activity cannot be performed on consecutive days, our next activity should earn only 11 points

Hence, the total merit points that can be earned using the greedy algorithm is 50 + 11 = 61

Since the greedy algorithm tries to reach for the fastest possible solution, it is currently restricting us from attaining the maximum merit points i.e 110

Hence, a better choice would be to implement the recursive approach for solving this Ninja Technique problem.

Method 2: Using The Recursive Solution

We will implement the recursive solution in three different steps.

Begin by expressing the problem in terms of its indexes

The number of days is certainly the deciding parameter in this problem. Hence, if we consider the number of days as the index it would be comparatively easier to solve the problem

Figure out all the possible choices for the index

Now, based on the number of days allotted to the Ninja i.e 2, we can consider the distinct characters in a program and calculate all the possible merit points that are attainable in the problem

Consider the maximum merit from the choices

Finally, the only decision that we have to make is to pick the maximum merit points that can be derived from our index of the number of days. 

This will take us no time since we have already defined the possible number of solutions that can be applied for this problem.

Hence, we can see that even if some of the points might be overlapping in this approach, the system will find the correct format for the pointers by memorizing the solution recursively.

Wrapping Up

If you are currently at an advanced level of programming, then you might be already acquainted with the dynamic programming approach.

This is one of the most efficient approaches for solving any programming problem, such as finding distinct characters since it uses the memorization technique to remember the solution.

If you are a technical interview aspirant, check out the DP approach to use this in your programming interview.

Ishita Juneja

4 Stories