Posts

JavaScript arrays

JavaScript arrays   ek mast aur versatile tool hai jo tumhare DSA (Data Structures and Algorithms) mein bahut kaam aate hain! ๐Ÿ“š๐Ÿ’ป Ab samjho  DSA ke context mein , arrays kaise kaam karte hain aur kya-kya operations perform ho sakte hain. ๐Ÿš€ Array Basics in JavaScript JavaScript mein, arrays ekdum  flexible  hote hain: Tum  different types ke data  ek hi array mein rakh sakte ho. ๐Ÿงƒ๐Ÿ•๐Ÿ’ฐ Example: let arr = [ 10 , "hello" , true , 42 ]; Tum  dynamic size  wale arrays bana sakte ho. Na koi size batana, na koi tension! ๐ŸŒ€ Common Operations in JavaScript Arrays (DSA Style) 1️⃣  Traversal (Array ke elements ko access karna) Traversal  ka matlab hai ek-ek karke har element ko dekhna. ๐Ÿ‘€ Example: let arr = [ 10 , 20 , 30 , 40 ]; for ( let i = 0 ; i < arr.length; i++) { console .log(arr[i]); // Output: 10, 20, 30, 40 } ๐Ÿ‘‰  Big O Time Complexity :  O(n) Tumhe (n) elements check karne padenge. 2️⃣  Searching (Koi particular...

Array ko Big O Notation๐Ÿš€

Array kya karta hai Big O ke context mein? Arrays ka kaam seedha-saadha hai, ek jagah pe saare data ko store karna aur use access, add ya delete karna. Har kaam ke liye arrays ka time complexity alag hota hai, jo Big O notation ke through samjha jaata hai. ๐Ÿง ๐Ÿ“Š 1. Access (Read) : Jab tum array se kisi specific element ko access karte ho, bas uski position (index) dekh kar value nikal lo. ๐Ÿ’ก Example: Tum arr[2] likhte ho, aur directly third item mil jata hai. ๐Ÿ‘‰ Time Complexity : O(1) (constant time) Mast fast hai! ๐ŸŽฏ 2. Search : Agar tumhe array mein ek particular value dhoondhni hai (e.g., "Mujhe 42 kaha rakha hai?"), to tumhe har element check karna padta hai. ๐Ÿ” Example: Tumhare array me arr = [10, 20, 30, 40] hai. Agar 30 dhoondhna hai, to pehle 10, phir 20, aur phir 30 pe pahuchega. ๐Ÿ‘‰ Time Complexity : O(n) Jitna bada array, utna zyada time lagega. ๐Ÿ˜… 3. Insertion : End me item add karna: Agar tum array ke end me ek value add kart...

Big O Notation Graph

Image
  Yeh graph  Big O Notation  ka comparison dikhata hai, jo hume yeh samjhata hai ki algorithms ka  performance  kaise change hota hai jab input size nn badhta hai. ๐Ÿ’ป๐Ÿ“ˆ 1. O(1) - Constant Time (Light Blue Line) Mast wali line!  ๐Ÿ’™ Iska matlab hai:  Input size nn  kitna bhi ho, operations ka time same rahega. ๐Ÿ˜Ž Example: Ek box se ek item nikalna, koi farak nahi padta kitne items hain. ๐ŸŽ ๐Ÿ‘‰  Super fast & efficient ! 2. O(log n) - Logarithmic Time (Cyan Curve) Slow growth wali line!  ๐Ÿ”ต Jab input nn badhta hai, operations bahut dheere-dheere badhte hain. Example: Tum kisi dictionary mein ek word dhoondh rahe ho, aur beech-beech ke pages chhodte ja rahe ho. ๐Ÿ“– ๐Ÿ‘‰  Efficient hai, bade inputs ke liye bhi manageable. 3. O(n) - Linear Time (Purple Line) Seedhi simple line!  ๐Ÿ’œ Agar n double hota hai, toh kaam bhi double hoga. Example: Tum ek list ke sare items ek-ek karke dekh rahe ho. ๐Ÿ—’️ ๐Ÿ‘‰  Decent, par bade inputs ke liye...

Space Complexity

Space Complexity ka matlab hai: Tumhare program ko kitni memory (ya jagah) chahiye jab wo chal raha ho. ๐Ÿ’พ๐Ÿง  Socho ki tum ek almirah mein kapde rakh rahe ho. ๐Ÿชž๐Ÿ‘• Agar kapde kam hain, to chhoti almirah chalegi. Agar kapde zyada hain, to badi almirah chahiye! Yehi hota hai space complexity . ๐Ÿ˜„ Example se samjho: Koi Extra Memory Nahi: Tum ek simple number add kar rahe ho: 2 + 3 = 5 2 + 3 = 5 . Isme bas ek chhoti si memory chahiye answer store karne ke liye. ๐Ÿงฎ ๐Ÿ‘‰ Space Complexity = O(1) (constant space, ekdum kam memory). ๐ŸŸข Memory for List: Tumhe 10 logon ke naam yaad karne hain, to tumhe unke liye memory chahiye. List mein 10 names rakho! Agar log 20 ho gaye, to memory double ho jayegi. ๐Ÿ“ ๐Ÿ‘‰ Space Complexity = O(n) (jitne log, utni memory). ๐Ÿ”ต Nested Boxes Wali Example: Socho tum ek matryoshka doll (nested dolls) bana rahe ho, jisme har doll ke andar ek aur doll hai. Har new doll ke liye extra memory lagti hai. ๐Ÿช† ๐Ÿ‘‰ Ye hoti hai recursive function ki spa...

What is Big O(2^n) Exponential

  Big O(2^n) ko samajhne ke liye socho ki tumhare paas ek aisi problem hai jo   exponentially grow   karti hai, jaise ek pedh ki branches. ๐ŸŒณ Samajhne ka Easy Tareeka: Kya hota hai O(2^n)? Jab tumhari problem ki size nn  1 step badhta hai , toh tumhari calculations  double  ho jaati hain. ๐Ÿ˜ฒ Matlab agar n=1n = 1, toh 2 calculations, n=2n = 2, toh 4 calculations, n=3n = 3, toh 8 calculations, aur n=10n = 10, toh 1024 calculations! ๐Ÿ˜ต‍๐Ÿ’ซ Yeh  exponential growth  ka example hai. Example se samjho: Socho tumhare paas ek  binary tree  hai, jisme har node ke 2 child nodes hain. Agar tumhare nn levels hain, toh total nodes honge 2n−12^n - 1. ๐ŸŒฒ Level 0: 1 node Level 1: 2 nodes Level 2: 4 nodes Level 3: 8 nodes Tum dekh rahe ho na, har level pe nodes  double  ho jaati hain? Drawback: Bahut slow ho jata hai  agar nn bada ho jaaye. For example, agar n=50n = 50, toh 2502^{50} itna bada number hai ki tumhare computer ka dimaag ghoom ...

Big O(log n) Notation ๐Ÿง๐Ÿ“‰

Big O(log n)   ka matlab hai ki   input size jitna badhta hai, kaam ka time utna dheere dheere badhta hai . Matlab agar tumhare paas bohot zyada data ho, tab bhi tumhara algorithm kaam jaldi karega. ๐Ÿ’จ Simple Example ๐Ÿ“š Socho tumhare paas ek  sorted list  hai, jisme tumhe kisi specific item ko dhoondhna hai. Tum ek  magic trick  use karte ho: Pehle pura list ko do parts mein divide karte ho . Phir tum check karte ho ki tumhara item kis part mein ho sakta hai (left ya right). Fir se jo half ka part mila, usko do parts mein divide karte ho. Yeh step baar-baar repeat karte ho. Is tarah se tum jaldi se apne item tak pohanch jaoge. Example: Guess the Number Game ๐ŸŽฎ Socho tumhe ek number guess karna hai, jo 1 se 1000 ke beech ho. Tumhare paas ek  hint system  hai jo tumhe batata hai: "Higher"  ya  “Lower” . Agar tum 1000 numbers mein se kisi ko guess kar rahe ho, toh tum pehli baar  500 number ke aas-paas guess karoge . Agar number 500 se...