Given an integer ‘N’, you are supposed to return the square of the given integer without using multiplication (*), division (/) or power function (pow()).
The first line contains a single integer ‘T’ denoting the number of test cases.
Each test case contains a single line with a single integer ‘N’ denoting the given number.
Output format:
For each test case, print the square of the given number in a separate line.
Note:
You do not need to print anything; it has already been taken care of.
1 <= T <= 50
-10000 <= N <= 10000
Time Limit: 1 sec.
2
3
-4
9
16
In the first test case, the square of 3 is 9.
In the second test case, the square of -4 is 16.
2
-13
1
169
1
Think of simplifying multiplication into repetitive addition.
Our first intuition is to simplify multiplication into repetitive addition.
Steps are as follows:
O(N), where ‘N’ is the given integer.
Since we are going through ‘N’ iterations so the overall time complexity will be O(N).
O(1)
Since no extra space is required, the overall space complexity will be O(1).