Array for leetcode (Hinglish)
Ek Kahani: Lockers Ki Duniya
Ek din, Pallav aur uske doston ne ek school event organize karne ka plan banaya. Is event mein unhe bahut saari cheezein kharidni thi, jaise candies, drinks, aur games. Isliye unhone socha ki kyun na is sab ko ek list mein likh lein?
Lockers Ki Tarah Array
Pallav ne ek row of lockers ki tarah socha. Har locker ek item (yaani, kharidi gayi cheez) rakhta hai. Unhone socha ki har locker ko ek number (index) dena chahiye, jisse wo asaani se dekh saken ki kaunsa item kaunse locker mein hai.
let lockerRow = [10, 20, 30, 40, 50]; // Ye hai unke lockers ki list
Lockers Ka Check Karna (Traversing)
Pallav ne socha, “Chalo sab lockers ko check karte hain!” Unhone ek loop banaya taaki wo har locker ke andar ki cheez dekh saken.
let sum = 0;
for (let i = 0; i < lockerRow.length; i++) {
sum += lockerRow[i]; // Har locker ka item sum mein add karte hain
}
console.log(sum); // Output: 150
Koi Item Find Karna (Searching)
Agar unhe kisi specific item (jaise ki ek candy) ko dhoondhna hota, toh wo kya karte? Wo sochte, “Chalo dekhte hain ki candy kis locker mein hai.”
let target = 30; // Wo item jo dhoondhna hai
let index = -1; // Agar nahi mila toh -1
for (let i = 0; i < lockerRow.length; i++) {
if (lockerRow[i] === target) {
index = i; // Us locker ka index store kar lo
break;
}
}
console.log(index); // Output: 2 (teesra locker)
Lockers Ko Arrange Karna (Sorting)
Pallav ne socha, “Acha hoga agar hum lockers ko arrange kar dein taaki humein cheezein dhoondhne mein asaani ho.” Isliye unhone lockers ko sort kiya.
lockerRow.sort((a, b) => a - b); // Ascending order mein sort kiya
console.log(lockerRow); // Output: [10, 20, 30, 40, 50]
Lockers Ka Order Palatna (Reversing)
Ek din Pallav ne socha, “Chalo lockers ka order palat dete hain!” Toh unhone lockers ko reverse kar diya.
lockerRow.reverse();
console.log(lockerRow); // Output: [50, 40, 30, 20, 10]
Do Doston Ki Technique (Two-Pointer Technique)
Pallav aur uska dost sochte hain, “Agar hum dono alag-alag taraf se lockers ko check karein, toh kya hoga?” Toh wo ek ek karke lockers ko dekhte hain.
let start = 0;
let end = lockerRow.length - 1;
let targetSum = 60;
while (start < end) {
let sum = lockerRow[start] + lockerRow[end];
if (sum === targetSum) {
console.log([start, end]); // Indices ko output karo
break;
} else if (sum < targetSum) {
start++;
} else {
end--;
}
}
Khidki Ka Technique (Sliding Window)
Pallav sochta hai, “Agar hum ek window le lein jo sirf 3 lockers ko dekhe, toh hum kya kar sakte hain?” Is technique se wo consecutive lockers ki sum nikaalte hain.
let k = 3; // Window ka size
let maxSum = 0;
for (let i = 0; i < k; i++) {
maxSum += lockerRow[i]; // Pehle window ka sum
}
let windowSum = maxSum;
for (let i = k; i < lockerRow.length; i++) {
windowSum += lockerRow[i] - lockerRow[i - k]; // Window ko slide karte hain
maxSum = Math.max(maxSum, windowSum); // Compare karte hain
}
console.log(maxSum); // Output: maximum sum of 3 consecutive elements
In-Place Operations
Kabhi-kabhi Pallav ko lockers ko bina extra space use kiye rearrange karna padta hai. Jaise ki zeros ko lockers ke end mein shift karna.
let lockerRow = [0, 10, 0, 20, 30];
let index = 0;
for (let i = 0; i < lockerRow.length; i++) {
if (lockerRow[i] !== 0) {
lockerRow[index++] = lockerRow[i];
}
}
while (index < lockerRow.length) {
lockerRow[index++] = 0; // Baaki lockers ko zero se bharo
}
console.log(lockerRow); // Output: [10, 20, 30, 0, 0]
Kahani Ka Moral
Pallav aur uske doston ne seekha ki array ki tarah lockers ko kaise efficiently use karna hai. Is kahani se unhe samajh aaya ki arrays ke through kaise alag-alag techniques ka istemal karke wo apne tasks ko asaani se kar sakte hain. Har ek technique unhe alag types ki LeetCode questions solve karne mein madad karegi!
Is tarah se aap bhi array concepts samajh kar LeetCode ke questions ko aasani se tackle kar sakte hain. Practice karte rahiye, aur sab kuch second nature ban jayega!
Subscribe to my newsletter
Read articles from Pallav Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pallav Singh
Pallav Singh
I am a software developer from India. I'm currently working full-time at an IT Service Company. I'm here to share my journey and my learnings with the community.