SShortSingh.
Back to feed

How a Hash Map Cuts the Classic Two Sum Problem from O(n²) to O(n)

0
·1 views

The Two Sum problem asks developers to find indices of two numbers in an array that add up to a given target. A naive brute-force approach uses nested loops, resulting in O(n²) time complexity that slows significantly as input size grows. The key insight is to use a hash map to store previously seen values, allowing each element's required complement to be looked up in constant time. This reduces the solution to a single pass through the array, achieving O(n) time at the cost of O(n) extra space. A common mistake is storing the complement rather than the actual value seen, which can cause incorrect lookups; the correct approach records each number and its index before checking for its partner.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Dev.to CLI Tool devpub Gains Image Upload Feature via Community Contributor

An open-source command-line tool called devpub, built to replace the Dev.to web editor, received its first community pull request three weeks after launch. Developer Harish independently forked the repository, tackled a long-pending issue, and delivered a complete image upload feature with tests and edge-case handling. The new devpub upload command lets users upload images directly from the terminal, bypassing the multi-step browser workflow previously required. Because the official Forem API lacks an image upload endpoint, Harish reverse-engineered the web editor's upload mechanism, which relies on browser session cookies and a CSRF token rather than an API key. The feature is available in devpub version 0.3.0 and includes a --markdown flag that outputs ready-to-paste image syntax for local article drafts.

0
ProgrammingDEV Community ·

How Fail2Ban Shields a Debian Server From Automated Internet Scanners

Internet-connected servers face relentless automated scanning almost immediately after going online, with some scanners firing hundreds of HTTP requests per second probing for exposed files like .env, wp-config.php, and Git repositories. Fail2Ban addresses this by monitoring system log files and automatically blocking IP addresses at the firewall level once suspicious activity crosses a defined threshold. The tool is lightweight and runs efficiently even on low-power hardware like a Raspberry Pi, making it practical for small setups. While Fail2Ban does not replace secure configuration, strong passwords, or regular updates, it effectively stops automated scanners from exhaustively testing a server over extended periods. The author recommends it as a standard addition to any publicly accessible Linux server, alongside the core principle of never exposing unnecessary services to the internet.

0
ProgrammingDEV Community ·

Dev Series Season 6 Tackles NIST 800-53 Change Management Compliance Head-On

A new season of the 'Adventures of Blink' video series on DEV Community has launched, focusing on bridging the gap between software developers and compliance teams. The season opens by examining NIST 800-53 CM-3, a control framework governing change management practices. The series argues that developers and auditors have long misunderstood each other, with each side holding only a partial view of the other's priorities. Rather than focusing solely on the specific control, the creator emphasizes teaching a broader approach to compliance that teams can adapt to their own frameworks. The overarching goal of the season is to encourage governance professionals and engineers to view compliance as a shared, sustainable responsibility.

0
ProgrammingDEV Community ·

Cloud Security Tools Miss Functioning vs. Configured Gap, Leaving Systems Exposed

A production incident revealed that a cloud secret appeared compliant on dashboards but had not actually rotated in 120 days because its rotation Lambda function had been deleted months earlier. The incident highlights a fundamental flaw in how cloud security tools work: they verify whether a setting is enabled, not whether it is actually functioning. Tools like Prowler, Checkov, and AWS Trusted Advisor are built reactively, adding checks only after researchers or bodies like CIS document a known issue. This approach leaves entire categories of misconfiguration — such as WAF rules that exist but contain no rules, or DMARC policies set to take no action — systematically undetected. Security experts argue tools need to shift from asking what issues have been reported to mapping every possible configuration state and identifying which ones cause harm.