vector<string> printNTimes(int n) {
vector<string>result;
for(int i=0;i<n;i++)
{
result.push_back("Coding Ninjas ");
}
return result;
}
Problem of the day
You are given an integer ‘n’.
Print “Coding Ninjas ” ‘n’ times, without using a loop.
Input: ‘n’ = 4
Output:
Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas
Explanation: “Coding Ninjas” is printed 4 times.
The first line of the input contains an integer ‘n’.
Print 'n' lines each having the text “Coding Ninjas”.
You don’t need to print anything. Just implement the given function.
3
Coding Ninjas Coding Ninjas Coding Ninjas
“Coding Ninjas” is printed 3 times.
5
Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas
Try to solve this in O(n).
Try to solve this in O(n).
1 <= 'n' <= 10^4
Time Limit: 1 sec
Try using recursion.
The idea of recursion is that we call smaller versions of the given problem.
We can make a function recursiveFunctiont('n', 'ans'). This function will add the text “Coding Ninjas” to the list 'ans' 'n' times. What can be the smaller version of this problem?
We have to add 'n' times. So we can add it once and then add it 'n' - 1 times by calling the function recursiveFunction('n' - 1, 'ans'). This will add the text remaining 'n' - 1 times.
What will be the base case? The function call recursiveFunction(0) will add the text 0 times, which is equivalent to not adding anything. So we will terminate the function when 'n' = 0.
The steps are as follows:
void recursiveFunction(int ‘n’, list ‘ans’)
void printNTimes(int ‘n’)
O(n), where ‘n’ is the given integer.
We are making ‘n’ function calls to recursiveFunction().
Hence, the time complexity is O(n).
O(n), Where 'n' is the number given.
In recursion, there is a function call before the previous function ends. So each function will consume its own space in the stack.
Hence the space complexity is O(n).
Interview problems
Print name N times
vector<string> printNTimes(int n) {
vector<string>result;
for(int i=0;i<n;i++)
{
result.push_back("Coding Ninjas ");
}
return result;
}
Interview problems
C++ easy solution
vector<string> printNTimes(int n) {
// Write your code here.
vector<string>vec;
if(n<1){
return vec;
}
cout<<"Coding Ninjas"<<" ";
printNTimes(n-1);
}
Interview problems
Python solution
from typing import *
def printNtimes(n: int) -> List[str]:
x = []
if n==0:
return x
x = printNtimes(n-1)
x.append("Coding Ninjas")
return xInterview problems
whats the issue here?
#include<bits/stdc++.h>
using namespace std;
void printNTimes(int n,int i) {
// Write your code here.
if (i == n) {
return;
}
else {
cout << "Coding Ninjas" << " ";
++i;
printNTimes(n,i);
}
return;
}
int main(){
int n,i;
i=0;
cin >> n ;
printNTimes(n,i);
return 0;
}
Interview problems
regarding the list
these compiler does not accept the code without using list?
Interview problems
java(17) ease made by hardik.
import java.util.*;
import java.util.List;
public class Solution {
static List<String> l=new ArrayList<>();
public static List<String> printNtimes(int n){
// Write your code here.
if(n==0)
return l;
l.add("Coding Ninjas");
n--;
return printNtimes(n);
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(printNtimes(sc.nextInt()));
}
}
Interview problems
Best Soln in Java
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<String> printNtimes(int n){
List<String> arr = new ArrayList<String>();
return print(n, arr);
}
public static List<String> print(int n , List<String> arr1){
if(n==0){
return arr1;
}
arr1.add("Coding Ninjas");
print(n-1, arr1);
return arr1;
}
}
Interview problems
c++ || using pass by reference || return string || no console
Create new function with passing vector string in it as ref and
void update(vector<string>&s,int x){
if(x==0){
return;
}
s.push_back("Coding Ninjas");
update(s,x-1);
}
vector<string> printNTimes(int n) {
vector<string> s;
update(s,n);
return s;
}Interview problems
Easy c++ solution
vector<string> printNTimes(int n, int i=1) {
// Write your code here.
if(i>n)
{
return{};
}
cout<<"Coding Ninjas"<<" ";
i++;
return printNTimes(n,i);
}