Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Print n times

Easy
0/40
profile
Contributed by
138 upvotes

Problem statement

You are given an integer ‘n’.



Example:
Input: ‘n’  = 4

Output:
Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas 

Explanation: “Coding Ninjas” is printed 4 times. 


Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of the input contains an integer ‘n’.


Output Format:
Print 'n' lines each having the text “Coding Ninjas”.


Note:
You don’t need to print anything. Just implement the given function.


Sample Input 1:
3


Sample Output 1:
Coding Ninjas Coding Ninjas Coding Ninjas 


Explanation of sample output 1:
“Coding Ninjas” is printed 3 times. 


Sample Input 2:
5


Sample Output 2:
Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas Coding Ninjas 


Expected Time Complexity:
Try to solve this in O(n).


Expected Space Complexity:
Try to solve this in O(n).


Constraints:
1 <= 'n' <= 10^4

Time Limit: 1 sec
Hint

Try using recursion.

Approaches (1)
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’)

  1. If 'n' = 0 then return/terminate the current function call.
  2. Add “Coding Ninjas” in ‘ans’.
  3. Call the function recursiveFunction('n' - 1, ‘ans’).

void printNTimes(int ‘n’)

  1. Let ‘ans’ be a list.
  2. Call recursiveFunction(‘n’, ‘ans’).
  3. Return ‘ans’.
Time Complexity

O(n), where ‘n’ is the given integer. 

We are making ‘n’ function calls to recursiveFunction().

Hence, the time complexity is O(n).

Space Complexity

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).

Code Solution
(100% EXP penalty)
Print n times
All tags
Sort by
Search icon

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;

}

6 views
0 replies
0 upvotes

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);

}

86 views
0 replies
1 upvote

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 x
93 views
0 replies
0 upvotes

Interview 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;

}

127 views
0 replies
0 upvotes

Interview problems

regarding the list

these compiler does not accept the code without using list?

18 views
0 replies
0 upvotes

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()));

    }

}

196 views
0 replies
0 upvotes

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;

    }

}

96 views
0 replies
1 upvote
profile

Interview problems

Optimal yet Easy in Cpp[C++].

//Askit-Endo

vector<string> printNTimes(int n) {
	vector<string> r;
	if(n == 0)	return r;
r = printNTimes(n-1);
r.push_back("Coding Ninjas");
return r;
}
251 views
0 replies
4 upvotes

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;
	
}
99 views
0 replies
0 upvotes

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);

}

160 views
0 replies
1 upvote
Full screen
Console