Last Updated: 17 Mar, 2021

Ninja Technique

Moderate
Asked in company
Adobe

Problem statement

Ninja has its own technique of making a decision to do something or not. This technique is known as the ninja technique. In this technique, Ninja generates a random string containing only digits, and if any substring whose integer value can be defined as the product of two consecutive integers i.e ‘X = N*(N+1)’ then Ninja do that work else didn’t. So now Ninja wants to invest in the stock market so he is using his ninja technique for deciding.

So your task is to write a code that can check whether the string contains any substring whose integer value can be defined as the product of two consecutive integers. If any substring exists you should return ‘True’else return‘False’.

Note :

Substring with integer value ‘0’is not considered as special and check only for integers up to length ‘9’as beyond ‘9’integer can’t hold values.

Example :

For the string ‘1242’ we return ‘True’ as for substring:
‘12’ we can be defined as the product of two consecutive integers i.e ‘3’ and ‘4’.
‘2’ we can be defined as the product of two consecutive integers i.e ‘1’ and ‘2’.
‘42’ can be defined as the product of two consecutive integers i.e ‘6’ and ‘7’.
 If the string passes the required condition we have to return ‘True’ else we have to return ‘False’.  

Input Format :

The first line of input contains a ‘T’ number of test cases.

In the second line, the string 'STR’ denoting the given string.

Output Format :

For each test case, print ‘True’ if any such substring exists else return ‘False’.
Note :
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.

Constraints :

1 <= T <= 10
1 <= | STR | <= 30

Where |STR| represents the length of the string 'STR'.

Time Limit: 1 sec

Approaches

01 Approach

  • We have to find out all the substring of our given string. For this, we use the function ‘subString’ which takes the length of our string and our string ‘str’.
  • We have to run three nested loops:
    • First loop iterate starting ‘i’ from ‘0’to size of string ‘str’
    • Iterate second nested loop  ‘i’ from ‘0’to ‘n-i+1
    • Through our third nested loop, we choose a starting point and ending point and provide values to ‘str1’
  • Now we convert our string ‘str1’into integer type and store it into ‘num’ variable  and checks for the condition by using :
    • If sqrt(num)==sqrt(num)*(sqrt(num)+1)
      • Return True
    • Else
      • Continue
  • Hence after the three nested loops, we arrive at our final answer and simply return that value.