hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Ninja and power of 2
Problem: Ninja and power of 2
Problem Link: www.codingninjas.com/codestudio/problems/repeating-numbers_893305
Companies Asked: Cognizant
Approach:
Initiate a 2D vector to store the result. Initiate a variable, value = 1.
Run a loop from i = 0 to N times.
Inside run another loop for 2^N times.
Insert the value in the resultant vector for the ith row and increase the value by 1.
Reset the value to 1 if it crosses 9.
Return the resultant 2D vector.
In case your approach is the same but not getting AC, just insert a -1 at the end of each row.
Time and Space Complexity:
Time Complexity: O(N*2^N)
Space Complexity: O(N*2^N)
Code [C++]
#include <bits/stdc++.h>
vector < vector < int > > numberPattern(int n) {
vector<vector<int>>ans;
int run = 1, mul = 1;
for(int i=1; i<=n; i++){
vector<int>tmp;
for(int j=1; j<=mul; j++){
tmp.push_back(run);
run++;
if(run >= 10) run = 1;
}
tmp.push_back(-1);
mul *= 2;
ans.push_back(tmp);
}
return ans;
}