Code With Coffie
  • HOME
  • ABOUT US
  • PORTFOLIO
  • JAVASCRIPT
    • Vue.js
  • PHP
    • PHP OOPS
    • LARAVEL
    • WORDPRESS
  • MYSQL
    • DATETIME
  • DSA
    • LEETCODE
  • Home
  • Blog
  • LeetCode
  • LeetCode #1: Two Sum Problem Solution in PHP
LeetCode #1

LeetCode #1: Two Sum Problem Solution in PHP

Mar 29, 2026 by codewithhemu

šŸ“Œ Introduction

The Two Sum problem is one of the most popular coding interview questions and is often asked in companies like Google, Amazon, and Microsoft.

In this blog, we will solve the problem using PHP, starting from a basic approach to an optimized solution.


🧠 Problem Statement

Given an array of integers nums and an integer target, return the indices of two numbers such that they add up to the target.

šŸ‘‰ Conditions:

  • Each input has exactly one solution
  • You cannot use the same element twice
  • Return answer in any order

šŸ“„ Example

Input: nums = [2,7,11,15], target = 9  
Output: [0,1]

šŸ‘‰ Explanation:
nums[0] + nums[1] = 2 + 7 = 9

🧩 Approach 1: Brute Force (Simple Method)

šŸ” Logic

Check every pair of elements and see if they add up to the target.


šŸ’» PHP Code

function twoSum($nums, $target) {
    $n = count($nums);    
    for ($i = 0; $i < $n; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($nums[$i] + $nums[$j] == $target) {
                return [$i, $j];
            }
        }
    }
    return [];
}

ā± Time Complexity

  • O(n²) āŒ (Slow for large input)

⚔ Approach 2: Optimized (Hash Map Method)

šŸ” Logic

  • Store numbers in an array (map)
  • Check if target - current already exists

šŸ‘‰ This avoids nested loops!


šŸ’» PHP Code

function twoSum($nums, $target) {
    $map = [];    
    foreach ($nums as $index => $num) {
        $diff = $target - $num;        
        if (isset($map[$diff])) {
            return [$map[$diff], $index];
        }        $map[$num] = $index;
    }    
    return [];
}

ā± Time Complexity

  • O(n) āœ… (Efficient)

šŸ”„ Dry Run

Input: [2,7,11,15], target = 9

StepNumDiffMapResult
127{2:0}–
272Found āœ…[0,1]

šŸš€ Why This Question is Important

  • 🧠 Tests problem-solving skills
  • šŸ”‘ Introduces hash maps
  • ⚔ Teaches optimization
  • šŸ’¼ Common in interviews

āš ļø Common Mistakes

  • āŒ Using same index twice
  • āŒ Not returning indices
  • āŒ Forgetting optimized approach

šŸ›  Best Practices

  • Always think of optimization
  • Use associative arrays (maps) in PHP
  • Practice dry runs

šŸŽÆ Conclusion

The Two Sum problem is a must-know for every developer.
Start with brute force, then move to optimized solutions using hash maps.

  • Share:
Previous Article What is dependency injection container?
No comments yet! You be the first to comment.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

category

  • DATETIME (6)
  • DJANGO (1)
  • Docker (1)
  • DSA (21)
  • DSA PRACTICE (4)
  • ENGLISH READING (1)
  • JAVASCRIPT (69)
  • LARAVEL (40)
  • LeetCode (1)
  • MYSQL (45)
  • PHP (21)
  • PHP OOPS (16)
  • PROGRAMME (1)
  • PYTHON (7)
  • REACT JS (6)
  • STAR PATTERN PROGRAMME (7)
  • Uncategorized (20)
  • Vue.js (5)
  • WORDPRESS (15)

Archives

  • March 2026
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • January 2023

Tags

Certificates Education Instructor Languages School Member

Building reliable software solutions for modern businesses. Sharing practical tutorials and real-world project insights to help developers grow with confidence.

GET HELP

  • Home
  • Portfolio
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer
  • Contact Us

PROGRAMS

  • Software Development
  • Performance Optimization
  • System Architecture
  • Project Consultation
  • Technical Mentorship

CONTACT US

  • Netaji Subhash Place (NSP) Delhi
  • Tel: + (91) 8287315524
  • Email: contact@codewithcoffie.com

Copyright Ā© 2026 LearnPress LMS | Powered by LearnPress LMS