Rotate and delete gfg potd | 02-10-24 | GFG Problem of the day

preview_player
Показать описание
Geeks for Geeks Problem of the Day(POTD) in C++ | Rotate and delete | Fully Explained🧠

Solution Code :
🌐 Connect with Me:

#GFG #POTD #geeksforgeeks #problemoftheday #c++
Gfg potd today
Рекомендации по теме
Комментарии
Автор

keep going brother it helps me a lot
i watch it everyday don't stop this series plese i am new to all this

jvinay
Автор

You need to perform half the times of original array size, at that time whatever the first element is is your answer.
int rotateDelete(vector<int> &arr) {
// Your code here
int sz=arr.size()/2;
int k=1;
while(n)
{
arr.insert(arr.begin(), arr.back()); //Last ele added to start
arr.pop_back(); //removed ele from last
//Array is now rotated
arr.erase(arr.end()-k); //Erase k-th element from last
k++;
n--;
}
return arr[0];
}

ritwicksingharoy
Автор

I solved it through recursion -- TC - 0(N) and SC - 0(N) -- Recursive call stack space

int solve(vector<int> &arr, int k, int n){
if(n == 1) return 0;

int ind = (k == 0) ? solve(arr, k + 1, n) : solve(arr, k + 1, n - 1);
int deletedInd = n - k;
int newInd = (ind == 0) ? n - 1 : ind;

if(newInd > deletedInd){ // from example 1
return newInd;
}
else if(newInd == deletedInd) return newInd - 1; // from example 2

return newInd - 1;
}

int rotateDelete(vector<int> &arr) {
int n = arr.size();
int ind = solve(arr, 0, n);

return arr[ind];
}

omkumarjha
Автор

Subscribe kar diya sir par please java mai bhi solution samjaya karo

T_M_T_K
Автор

main raat ko dekha tha tab kth element tha 2nd point pe main dry run nahi kar paya ab samjh aya ki wrong tha constrain

manojitsaha
Автор

Keep it up!!! ❤beautiful explanation 🥹

nandinigupta