Cracking the Code: The Foodie Puzzle and the Art of Time Complexity

Akhil TiwariAkhil Tiwari
3 min read

Introduction

One day, my friend and i were arguing about something called “Time Complexity” in coding.He said,”100 lines of code way better than just 2 lines of code”, i disagreed and told him “no way 2 lines of code way better than 100 lines of code “ I explained if we can do same thing by writing just 2 lines of code then why we need 100 lines of code make sense??, To prove our point, we decided to make it fun and came up with a challenge called the Foodie Puzzle.

Find Your Paneer Package🤤🤤 – A Tasty Analogy to Understand Time Complexity

Scenario:

Maano tum ek ultimate foodie ho. Paneer tumhari jaan hai. Tumhare saamne 9 khane ke packages rakhe hain, neatly arranged in an array.

  • 8 packages ka weight hai exactly 500 grams – yeh sab normal wale snacks hain, kuch chips, kuch biscuits, etc.

  • Lekin ek package hai 550 grams ka – yeh hai VIP Paneer package, full loaded with extra paneer cubes 🧀.

Ab tumhara mission simple hai:
Paneer wala packet jaldi se dhoondh ke nikalo

My Approach:

In my approach, I simply traverse the array of packages and check each one to see if it weighs 550g (the paneer package). This is a straightforward linear search. It works perfectly and has a time complexity of O(n) since it may have to check all elements in the worst case.

#include <iostream>
using namespace std;

int main() {
    int Packages[9]={500,500,500,550,500,500,500,500,500};
    for(int i=0;i<=9;i++){
        if (Packages[i]==550){
            cout<<"Paneer Ka package mil gya:"<<i;
        }
    }
    return 0;
}

MY Friend’s Approach:

In my friend’s approach what he did he divides 9 packages into 3 equal groups and start comparing the total weight of each group :

  • if pack1==pack2 then paneer package found in package 3

  • if pack1>pack2 then paneer package found in package 1

  • if pack1<pack2 then paneer package found in package 2

Since we are always dealing with a fixed number of operations (just 9 elements, always divided into 3 groups), this approach has constant time complexity, O(1) regardless of how large n would be in another case.

#include <iostream>
using namespace std;

int main() {
    int Packages[9]={500,500,500,550,500,500,500,500,500};
    int pack1=Packages[0]+Packages[1]+Packages[2];
    int pack2=Packages[3]+Packages[4]+Packages[5];
    int pack3=Packages[6]+Packages[7]+Packages[8];

    if(pack1==pack2){
        for(int i=6;i<=8;i++){
            if(Packages[i]==550){
                cout<<"paneer ka package mil gya:"<<i;
            }
        }
    }
    else if(pack1>pack2){
        for(int i=0;i<=2;i++){
            if(Packages[i]==550){
                cout<<"paneer ka package mil gya:"<<i+1;
            }
        }
    }
    else if(pack1<pack2){
        for(int i=3;i<=5;i++){
            if(Packages[i]==550){
                cout<<"paneer ka package mil gya:"<<i;
            }
        }
    }
    return 0;
}

Conclusion:

In the end, its not about how long your code is -its all about how smartly you solve the problem .

  • mera 2 line wala code galata nahi hai bass thoda slow O(n) ,short and sweet tha

  • mere friend ka code optimized hai smartly likha gya hai jiski wajh se uski speed acchi hai O(1)

  • so the lesson is: code chae 2 line ka ho ya 50 line ka ,code ki readabilty , understanding toh important hai hi sath sath hume code ki execution speed per bhi dhyan dena chaiye

  • jaruri nahi har situation main 2 line ka code hi best ho aur 50 line ka code bekar ho yeh bhi jaruri nhi

DSA Shirf coding hi nhi, real world problems ko logically ,efficiently kaise theek karna hai ye bhi seekhata hai

Aur haan agar tum bhi coding ke sath sath khane ke dewaane ho toh comment main 🍕 ya 🧀 drop kar do

Phir milunga aise hi kisi doosre concept ke sath tab tak apna khyal rkhein 😊

0
Subscribe to my newsletter

Read articles from Akhil Tiwari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akhil Tiwari
Akhil Tiwari