Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created October 26, 2021 12:34
Show Gist options
  • Save rozer007/b7e991d06b51fcd4379d2e17aa153daa to your computer and use it in GitHub Desktop.
Save rozer007/b7e991d06b51fcd4379d2e17aa153daa to your computer and use it in GitHub Desktop.
Print Decreasing
Q1) What is the time complexity?
ans: As n calls are made and work is done corresponding to these n calls therefore the time complexity becomes O(n).
Q2) What is the space complexity?
ans: Since no extra space is used, therefore space complexity is constant, however you should know that if the recursion call stack is taken into account, then space complexity will be O(n) as there are n recursive calls.
Q3) What is base condition?
ans: Since we are printing in decreasing order we are going to stop our recursive call when the n is 0.
Hint 1: Use the concept of Recursion with base condition n=0.
Hint 2: Try to call the recursive call from n to n==0.
Hint 2: Beware of overflow.
Q1) What will be the base condition?
if (n == _ )
return;
a) 1
b) 0
c) -1
d) 2
ans: b) 0
Q2) What will be the recursive call?
printDecreasing(_____);
a) n-2;
b) n+1;
c) n-1;
d) n+2;
ans: c)n-1
Q3) what is the output if n==3?
printDecreasing(int n) {
if (n == 2)
return;
System.out.print(n + "\t");
printDecreasing(n - 1);
}
a) 3 2 1
b) 2 1
c) 3
d) 3 2 1 0
ans: c) 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment