Rotate Array in O(1) Space? 🤯
#leetcode #dailycoding #geeksforgeeks
Rotate Array in O(1) Space? 🤯
Agar array ko rotate karna hai bina extra memory use kiye, toh Reversal Algorithm use karo!
The Pro Way: 3 Reverse Calls. Only O(N) Time and O(1) Space. 🔥
code:
void rotate(int a[], int n, int k) {
k = k % n; // Handle cases where k > n
reverse(a, a + n);
reverse(a, a + k);
reverse(a + k, a + n);
}
Simple, Clean, and Optimized. Share this with a friend who writes long code for simple problems! 😂
#leetcode #dailycoding #geeksforgeeks #cpp #java #python #interviewquestions #sde #tech #coding #cpp #java #interviewquestions #bitwise #xor #programmer #softwareengineer #dsa #techreels #sde #interview #placement #college #student #btech #1styearstudent #google #microsoft #amazon #trending #coderestro
Comments
Comments from the original post on Instagram
No comments yet
