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

1 to N Without Loop

Easy
0/40
Average time to solve is 15m
profile
Contributed by
366 upvotes

Problem statement

You are given an integer ‘n’.


Your task is to return an array containing integers from 1 to ‘n’ (in increasing order) without using loops.


Example:
Input: ‘n’ = 5

Output: 1 2 3 4 5

Explanation: An array containing integers from ‘1’ to ‘n’ is [1, 2, 3, 4, 5].
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first and only line of the input contains the integer 'n'.
Output format:
The first and only line of the output contains the returned array.
Note:-
You don't need to print anything. Just implement the given function.
Sample Input 1:
5
Sample Output 1 :
1 2 3 4 5
Explanation Of Sample Input 1:
The array contains all integers from 1 to 5 in ascending order.
Sample Input 2:
2
Sample Output 2:
1 2
Explanation Of Sample Input 2:
The array contains all integers from 1 to 2 in ascending order.
Expected Time Complexity:
The expected time complexity is O(n), where 'n' is the given integer.
Constraints:
1 <= n <= 10^6

Time Limit: 1-sec
Hint

Recursion does not need a loop

Approaches (2)
Recursion

We will use a recursive function because we can’t use a loop. This function takes an integer and an array as an argument. The base case for the function is when the ‘argument integer’ becomes zero. Otherwise, we will call the recursive function ‘current element - 1’ and insert the current element in the array.

 

The steps are as follows:-
 

// Recursive Function to push elements

function recursiveFunction(int x, int[] arr):

  1. If ‘x == 0’:
    • Return;
  2. Call recursiveFunction(x - 1, arr).
  3. Insert ‘x’ into the array ‘arr’.
     

// Function to get an array from ‘n’ to ‘1’ without loop

function printNos(int x):

  1. Create an empty integer array ‘arr’.
  2. Call recursiveFunction(x, arr).
  3. Return ‘arr’.
Time Complexity

O( n ), where n is the integer.
 

We will call the recursive function ‘n’ times.

 

Hence, the time complexity is O( n ).

Space Complexity

O( n ), where n is the integer.

 

‘n’ recursive functions will be called.
 

Hence, the space complexity is O( n ).

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
1 to N Without Loop
All tags
Sort by
Search icon

Interview problems

C++ Easy Solution

void printRecursion(vector<int>& ans,int target,int counter) {

    if(counter>target)

    return;

    ans.push_back(counter++);

    printRecursion(ans,target,counter);

}

 

vector<int> printNos(int x) {

    // Write Your Code Here

     vector<int> ans;

     printRecursion(ans,x,1);

     return ans;

}

28 views
0 replies
0 upvotes

Interview problems

1 to n without loop

public class Solution {

    public static int[] printNos(int x) {

        // Write Your Code Here

        int arr[]=new int[x];

        recFun(arr, x);

        return arr;

    }

    public static void recFun(int arr[],int x)

    {

        if(x==0){

        return;

        }

        arr[x-1]=x;

        recFun(arr, x-1);

    }

}

86 views
2 replies
1 upvote

Interview problems

Java

public class Solution {
     public static void fillArray(int current, int size, int[] result) {
        if (current > size) {
            return; 
        }
        result[current - 1] = current;
        fillArray(current + 1, size, result);
    }
    public static int[] printNos(int x) {
        // Write Your Code Here
        int[] result = new int[x];
        fillArray(1, x, result);
        return result; 
    }
}
36 views
0 replies
0 upvotes

Interview problems

Python - Solution

from typing import List

def printNos(n: int,arr:List = None) -> List[int]:

if arr is None:

arr = []

if n > 0:

printNos(n-1, arr)

arr.append(n)

return arr

42 views
0 replies
0 upvotes

Interview problems

100% better than other easy soln.

void fn(int i,int n){

    if(i>n){

        return;

    }

    else{

        cout<<i<<" ";

      fn(i+1,n);  

    }

    

}

vector<int> printNos(int x) {

   fn(1,x);

}

101 views
0 replies
0 upvotes

Interview problems

very easy solution in c++

vector<int> printNos(int x) {

  if(x==1) return {1};

  int y=x;

  vector<int> a = printNos(x-1);

  a.push_back(y);

  return a;

}

336 views
0 replies
0 upvotes

Interview problems

1 to n without loop in PYTHON

def printNos(x: int) -> List[int]: 

    # Write your code here

    cnt=1

    result=[]

    def printl(result,cnt):

        if cnt==x:

            result.append(cnt)

            return result

        result.append(cnt)

        return printl(result,cnt+1) 

    return printl(result,cnt)

90 views
0 replies
0 upvotes

Interview problems

recursion function are used to solve this question

here we use recursive call to solve this problem

78 views
0 replies
0 upvotes

Interview problems

1 to N without loop || Java

public class Solution {
    public static int[] printNos(int x) {
        int arr[] = new int[x];
        recFunc(arr, x);
        return arr;
    }
    public static void recFunc (int[] arr, int x) {
        if (x == 0) {    // base case
            return;
        }
        arr[x-1] = x;
        recFunc(arr, x-1);
    }
}
292 views
0 replies
1 upvote

Interview problems

Java Solution (recursion)

public class Solution {

    public static int[] printNos(int x) {

        // Write Your Code Here

        int arr[]=new int[x];

 

        nextarray(arr,1,x);

        return arr;

    }

    public static void nextarray(int arr[], int current, int x)

    {

        if(current>x)

        {

            return;

        }

 

        arr[current-1]=current;

        nextarray(arr, current+1, x);

    }

}

140 views
0 replies
0 upvotes
Full screen
Console