Re 2. Problems on Recursion | Strivers A2Z DSA Course
Vložit
- čas přidán 23. 12. 2021
- Check our Website:
Notes:
Understand recursion by printing something N times: takeuforward.org/recursion/in...
Print name N times using recursion: takeuforward.org/recursion/pr...
Print 1 to N using recursion: takeuforward.org/recursion/pr...
Print N to 1 using recursion: takeuforward.org/recursion/pr...
Sum of first N numbers: takeuforward.org/data-structu...
Factorial of N numbers: takeuforward.org/data-structu...
Reverse an array: takeuforward.org/data-structu...
Check if a string is palindrome or not: takeuforward.org/data-structu...
Fibonacci Number: takeuforward.org/arrays/print...
In case you are thinking to buy courses, please check below:
Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
---------------------------------------------------------------------------------------------------------------------------------------------------- Check Codestudio: bit.ly/3G61sZZ
Please check out the entire channel for other sets of series on tougher and complex topics. Also do consider subscribing :)
Please check out the SDE sheet which the entire country is using and getting placed at top-notch companies: takeuforward.org/interviews/s...
Checkout Striver's Handles: linktr.ee/takeUforward
Please do comment how was it ? :)
public class recursiveCode {
public static void main(String[] args) {
//print n to 1 using backtrackign, i.e without using n-1 approach
backTrack(0,4);
}
static void backTrack(int start, int n)
{
if (n == start)
{
return;
}
backTrack(start+1,n);
System.out.println(start+1);
}
}
void f(int i, int j){
if(i==-1) return;
f(i-1, j);
if(i==j) return;
cout
Thanks striver for this series, waiting for DP series
As always fantastic
Soln:
#include
using namespace std;
void print(int i, int n)
{
if (i > n) return;
print(i + 1, n);
cout N;
print(1, N);
return 0;
}
Bhai thank u so much. Only coz of u i am finally understanding recursion and backtracking
In recursion while going, things happen while in backtracking, in coming back things happen, that's why the term backtracking, so well explained!!
i missed it, thanks'
Code for printing N to 1 using backtracking concept:
#include
using namespace std;
void print(int n , int i)
{
if(n
Bro i didn't understand how this code was executed if we write i +1 then why code was not starting from 1 to n? Please give answer
@@souravjoshishorts7199 bhai isten to what striver said he said ki aap print ko pehle likhne ke jagah function ko wapis call kar rhe ho toh woh jo number pehle print hona tha last mei print hoga and so on...jasa n= 4 then woh stack mei store hote jaega in order 1,2,3,4 uske baad jab base case reach hoga toh woh function call karte rahega so order will be changed to 4,3,2,1 😉
Nice answer, it does require two vars...
@@user-ri5gw3ty1y
#include
using namespace std;
void f(int cnt, int n) {
if (cnt == 0) return;
cout n;
cnt = n;
f(cnt, n);
return 0;
}
@@souravjoshishorts7199 notice carefully void function k bad wo firse bar bar function ko call karrha ha jis karan use print karne ka mauka hi nahi milraha in short print line hit he nahi karraha ha and jab wo base condition ko hit karega last m tb jab wo back jayega yafir backtrack krega har function m tab wo ak ak kar k print krega .
For the problems #3 and #4, you don't need to pass two parameters. f(i) will be sufficient!
I think for the first one only one parameter is enough.But for the second one i have used two.
@@nikhildotasara8788 for second too you can do with 1 parameter
@@omkarraskar8664 How bro?
@@nikhildotasara8788 u r correct bro we need another parameter in #5th problem
@@ramanahlawat398 for all problems only one parameter is sufficient bcoz if we see every prob statement clearly
Although, he was teaching , so he need to take such example
Dude you are underrated, amazing amount of effort it takes!
He is No more UNDERRATED..!!
yeahhhh
@@aadityabuchale15
This gave me some confidence and understanding of Recursion. Thank You so much!
Question 5 - Java Code :
public class PrintNumbersinReverseUsingBackTracking{
static void reverse(int i,int N){
if(i>N){
return ;
}
reverse(i+1,N);
System.out.println(i);
}
public static void main(String args[]){
int N=10;
reverse(1,N);
}
}
@clawseekgaming4681N is input and compareing with so i cant exceed the limit of i=1; other wise you will face swgmental fault error 🙃
With each passing vdo of Striver bhaiya i saw , I am becmoing fan of him. When it comes to DSA i always recmmmend to watch Striver's vdo to my friends and juniors. The clarity in each topic is just wow.
Can't believe all of this is for free!!! Thank you so much take U forward team!
Now I'm gaining more confidence in recursion. Amazing explaination. 😃
Question 5 -> Print no from N to 1 using backtracking
#include
using namespace std;
void printName(int i , int n){
if(i>n){
return;
}
printName(i+1 ,n);
std::cout
Brother why use std:: when u have imported the whole std library itself
@@lex-zt6uc it was auto completed by the compiler so i do not erase the std
Why std::
@@Akash-yr2if if you are not using "using namespace std" then we have to use Std in every cin and cout
@@ayushrawat9252 I knw that...But even after using it in the header, Why have u used it.
I really don't know if someone explains as good as you do !!!! 👏 Unbelievable Efforts
zero effort cz he is a genius.
This video really boosted my confidence for recursion. Thank you so much
One of the best playlist on recursion, thank you so much bhaiya.
The videos are absolutely from basic. Great Explanation
public static void main(String[] args)
{
Scanner input = new Scanner ( System.in );
System.out.print("Enter the value of n : ");
int n = input.nextInt();
int i=1;
printNumbers(i,n);
input.close();
}
static void printNumbers(int i , int n )
{
if(i>n)
{
return;
}
else{
printNumbers(i+1,n);
System.out.println(i);
}
}
}
You are one of the best DSA tutor
PRINT FOR N To 1 using Backtracking
#include
using namespace std;
void print(int i, int n){
if (n
the base condition should be if (n>3){return;}
@@photozoaaree kya bol rhe😂
@@photozoano...
for N to 1
we only need one parameter
#include
using namespace std;
void print(int n){
if(n == 0){
return;
}
cout n;
print(n);
return 0;
}
backtracking se nhi hora y
@@anordinaryguy207 ++
Write if(n
Bhaiya you understand the concept of recursion in a very simple way, hats of for that and I am blessed that I can learn from such a guru.
Finally understood the meaning of printing after and before function call,Thank you striver 🙌🙌
def backtrack(i, n):
if i > n:
return
backtrack(i+1, n)
print(i)
backtrack(1, 5)
Correct
You're a teacher. I just understood what I thought was magic
just do condition(i>n) return ;
then apply function (i+1,n)
then cout
EZ
you completely nailed it bruhh!!!!!!!!!!! thanks for making such a beautiful series
Start at: 1:08
Back Tracking : 15:25
Thanks man 😄
Best material out their ... love the way u connected/distinguished recursion from backtracking
thanks!The Explanation of your is at God Level,Best on the internet and planet earth., Please keep doing such work.,Thanks you.Love from Mumbai. Thanks a lot.
hi, thank you so much for this video!! i've always struggled with backtracking and for the first time ever, i felt like i understood it.
Solution of the assignment given by you : print n to 1 using Backtracking :
private static void print(int i, int n){
if(i>n)
return;
print(i+1,n);
System.out.println(i); // Backtracking !!
}
public static void main(String[] args) {
print(1,5);
}
Thankyou bhaiya for making this concept in such a crystal clear way !! Loved the way u simplify the things to us , May wish you will achieve all your sky offers to you ..
You are right brother , he is great at teaching...
@@Iqbal00123 yes brother
#include
using namespace std;
void f(int i,int N){
if(i>N)return;
f(i+1,N);
cout
This is head Recursion.
@@consolecoder6724 issi se toh krna h bro
@@nishantpruthi1172 yup.
@@mukulchopra3378 ohfo, coding seekhi ja rhi h, mukul chopra dwara
is there any other approach if we take both i and n equal to n ?
Thankyou Striver. Before wathing this video.. in my mind, I'm feared a lot of the names say Backtracking, Segmentation, Brute force approach. .. But you cleared my doubt. Not, it's easy to analyse every term
Thank you.
I understood it completely.
May this comment make youtube feature this video to all users.
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
fun(1,n);
}
static void fun(int i,int n){
if(i>n)
return ;
fun(i+1,n);
System.out.println(i);
}
recurse(int N,int final){
if(final>N) return;
recurse(N,final+1);
print(final);
}
Best video for Recursion basics on CZcams!!!
Loved the way you explained the concept of recursion and Backtracking ,i was able to get a clear picture of it and Thanks fot it
I don't think,we need to carry value of N with i everytime if base case dont need it.Rest explanation is amazing.
Yaar thanks a lot striver, I was unable to understand the recursion even after doing 30 to 35 questions ,and ur this single video helped me in getting the concept easily,
After watching this video I was like my life was lie 😂
bro we are sailing on the same boat , would like to discuss something related recursion to you !! shall we?
Thank for making such a wonderful series.
Excellent teaching! understood the concept of Recursion
Class A{
public void print(int n){
if(n
the thing is you can not use n-1;
this is wrong brother. this will print from 1 to n
C++ - Print N to 1 using recursion but with backtracking:
#include
using namespace std;
int n, i = 1;
void f(int x,int y);
void f(int i,int n)
{
if(i>n) return;
f(i+1,n);
cout
Thanks for all you do Striver ❤
Thanks for the video. 1st time I understand the backtracking.
for the third question we could have a single function call with n only
#include
using namespace std;
void printn_1_backTracking(int n, int i) {
if (i > n) {
return;
}
printn_1_backTracking(n, i + 1);
cout
understoood
Thank you striver for such an amazing explanation
Just started watching your recursion playlist
class Recursion
{
public static void main(String[] args) {
int n = Integer.parseInt("5");
printReverse(5, 5);
}
static void printReverse(int n, int num)
{
if(n == 0)
return;
printReverse(n-1, num);
System.out.println(num-n+1);
}
}
minus use ni krskete vro
def f(a,b):
if a>b:
return
f(a+1,b)
print(a)
f(1,3)
#thanks for the content
Excellant getting confident day by day
if(i>n) return;
print5(i+1,n);
cout
// Use backtracking to print N to 1
#include
using namespace std;
void print(int i, int N)
{
if(i > N)
{
return;
}
print(i + 1, N);
cout
Question 5:- [Python Solution (BackTracking)]
def printNumber(i,n):
if(i>n):
return
printNumber(i+1,n)
print(i)
number = int(input())
printNumber(1, number)
oooooo
very good explanation i have cleary understood the concept of backtracking.😎
def backtrackNum1(i,n):
if i>n:
return
backtrackNum1(i+1,n)
print(i)
backtrackNum1(1,3)
Thank you so much, Sir.
Very nice explanation...God Bless You
Very nice choice of question to make the base strong❤
can you please make a video on deriving the time complexity and space complexity of a given algorithm?
void print(int i, int n){
if(i>n)return;
print(i+1,n);
cout
def func(i,n):
if(n
Great video Striver
function print(i, n) {
if (i == n + 1) {
return
}
print(i + 1, n);
console.log(i);
}
Understood!! Awesome explanation
Does this mean dfs-prefix of BT is recursion and dfs-postfix is backtracking? What will infix will be then? Or is it like backtracking is basically fancy name for recursion, or does it have unique cases where we can differentiate between recursion and backtracking?
def number(i,n):
if n
Really very thankful to you sir, so well explained
Wonderful series
Why we need two parameters to pass ?
Instead we can use only one parameter by
Void print(int i)
{
If(i
thank you striver . you made my day
vaiya totally clear the basic recursion😍
Thank you ! Understood :))
amazing playlist of dsa it's like wow
Thank You Striver!!
def back1(n):
if n > 5:
return
back1(n+1)
print(n, end=" ")
back1(1)
I am finally getting it, thankyou so much
For the last question
void printNto1_backtracking(int n, int i = 1) {
if (i > n) return;
printNto1_backtracking(n, i+1);
cout
Python implementation of print N to 1(using backtracking):
def printNto1(lower,upper):
if(lower==upper):
print(lower)
else:
printNto1(lower+1,upper)
print(lower)
printNto1(1,7)
In python(for python users :) ):
def gg1(n,i):
if n
Understood awesome.....
Striver's backtracking understood!
Thank you for good explanation and last hometask)
All questions done
Thankyou bhaiya bhot khushi ho rhi ye concepts finally smjh k
#include
using namespace std;
void print(int i, int n){
if(i>n){
return;
}
cout
void print(i,N)
{
if (i>N) return;
print(i+1,N);
cout
Thank you for the video with good explanation.
In 3rd question, There is no need of 2 parameters. It could be solved using single parameter only. Even you have not used 'n' anywhere in the code.
void printN(int N)
{
if(N < 1) return;
System.out.println(N);
printN(N-1);
}
yeah right
void linear(int i,int n){
if(i>n){
return;
}
linear(i+1,n);
cout
For Printing the counting 2 parameters makes it complex .
void fun(int n){
if(n==0)
return;
cout
20:52
ques 5
void printNToOne(int i, int n)
{
if(i > n)
return;
printNToOne(i + 1, n);
cout
You are great sir .....can you start comparative programing sir .... please .....
Now today i understand the recursion concept
public class sec {
static void f(int n,int i){
if (i>n) return;
f(n,i+1);
System.out.println(i);
}
public static void main(String[] args) {
int n=50;
f(n,1);
}
}
java approach for printing n to 1 using backtrack (thanks striver bro)
for Q5 in C++
#include
using namespace std;
void num(int a,int n){
if(a>n){
return;
}
num(a+1,n);
cout
Are the last 2 questions using 'backtracking' or simply 'head recursion' ?
#include
using namespace std;
void backTrack(int i,int n){
if(i>n)
return;
backTrack(i+1,n);
cout
This code is printing the numbers or only i n times??
Print N to 1 using backtrack (C++)
#include
using namespace std;
void print(int n, int i)
{
if (n == i - 1)
return;
// cout
thanks alot bhaiya for the best explanation!
😊
Understood , super explanation
void print5(int i, int n){
if(i>n) return ;
print5(i+1,n);
cout
20:51
#include
using namespace std;
void print (int i, int n)
{
if (i > n) return;
print(i+1 ,n);
cout n;
print (1, n);
return 0;
}
Thankyou so much just amazing explanation
Well explained 🔥
Damn Bro. W Teaching. Good Job 🔥🔥