Last Index of Occurrence - Solution | Recursion | Data Structures and Algorithms in JAVA

preview_player
Показать описание


#pepcoding #programming #freeonlinecourses
Рекомендации по теме
Комментарии
Автор

Literally I have tried to understand recursion alot. I have invested lot of time in it but I was not getting feel of recursion. I used to hear the word that recursion works like magic, have faith in recursion and all. But this is the first time I am feeling the connection with recursion hats off to you sirr..
This proves that teaching is whole different thing. Today's main problem that I am feeling is everyone who is good at coding starting to teach. I am not against it but teaching is really very different thing than achieving some thing. The way you frame the questions sequentially. Lot more to say but I have to learn recursion thank you sir ❤

neerajmahapatra
Автор

excellent sir i cant express my happiness after learn this question concept from you

sagartaak
Автор

Han Ji, Sir.. ☺️
I scared of learning recursion, and now after watching your tutorials I love to learn

archanasarang
Автор

I tried doing this by traversing this from the end of array. Thanks for making my concepts clear.

public static int lastOccurenceFinder(int arr[], int idx, int element){
if(idx == -1) return -1;

if (arr[idx] == element )return idx;

int theIndex = lastOccurenceFinder(arr, idx - 1, element);

if(theIndex != -1 ) return theIndex;
else return -1;

}

akashsharma
Автор

we can also do it like this, where we start from end of array and return as soon as we found out the element...
code:-]
public class Main {

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
int x = sc.nextInt();
int li = lastIndex(arr, n-1, x);
System.out.println(li);
}

public static int lastIndex(int[] arr, int idx, int x){
if(idx == -1) return -1;

if(arr[idx] == x) return idx;
int liisa = lastIndex(arr, idx-1, x);
return liisa;
}

}

shivanishikha
Автор

int last =last(arr, idx+1, d)
if(arr[idx]==d && idx>last)
return idx;
else
return last;


My Solution, Thanks Sir its because of pausing your video and got this solution by my own thinking love u

rutulnandanwar
Автор

Sir, once again brilliant explanation.thanks a lot

aashishgoyal
Автор

The way you analyse the problem is

Thank you Sir

hd
Автор

sir jitna effort aap daalte ho pdhate time aur koi teacher nhi daalta including pepcoding staff

AYUSHBAJPAIBAI
Автор

my mind sometime blows up seeing the explanation...

sonomrubayat
Автор

that team example is hilarious. I laughed out loud on that one. But excellent way to explain. Thanks!

vibhusha
Автор

Sir ji gajab ka explanation hai, thank you sir

saurabhshekharrai
Автор

Best recursion explanation available on YouTube for sure.Thank you sir.

rajsrivastava
Автор

i dont usually cmmnt
but this explanation is god tier :)

XtreamTechnology
Автор

As per the last question, you told this solution is good but can we make better then this so can we take index from last, call idx-1 when not equal to element else directly return the idx, (why to travel more). I have written the sample code.

public static int lastIndex(int[] arr, int idx, int x){
if(idx < 0) {
return -1;
}

if(arr[idx] == x) {
return idx;
}

else {
int li = lastIndex(arr, idx-1, x);
return li;
}

}

raghavagrawal
Автор

Love you man, now i am able to solve recursion prblm myself 😁

harpic
Автор

public static int findLast(int arr[], int key, int index)
{
if(index==arr.length)
return -1;
int last=findLast(arr, key, index+1);
if(arr[index]==key)
if(last==-1)
last=index;
return last;
}

OKONALAYASWANTHPHANIKRISHNARED
Автор

Watched on 16 October, 2021 - Thanks for the video.

raj_kundalia
Автор

sir what if call our function by passing index value from arr.length - 1 ? this could be more easier and we don't need to traverse recursion function till very end. We can return the first index from last which give the matched number. this will be similar to you firstIndex recursion question.

chinmaysahal
Автор

Thank u sir today I completely understood how recursive call works👍

arvindmenariya