💻
Gürkan Fikret Günak - Personal
  • 👨‍💻About me
    • 🌊Journey
  • 🎯Dart
    • 🔬What's Dart Algorithms?
    • 🔬What's Dart Structures?
    • 🧮#01 Algorithm Guidance: Implementing Calculation Algorithms
    • 🧮#02 Algorithm Guidance: Two Sum
  • 📄Guidances
    • Flutter MVVM Guidance
    • Dart Programming Guidance
    • E-Commerce Use Cases
    • E-Commerce Applications
    • Flutter App Color Palette Usage Guidance
    • Flutter Custom AppBar Usage Guidance
    • Flutter Network Image Cache Usage Guidance
    • Flutter Project Bitbucket SSH Guidance
    • Flutter Project GitHub SSH Guidance
    • Flutter SliverAppBar Usage Guidance
    • The Importance of BuildContext in Flutter Tests Guidance
    • Internship Basic Guidance v0.1.0
    • The Importance of Type Casting in Flutter
    • Effective and Detailed Pull Request Guide
    • Flutter Naming Conventions Guidance
    • Flutter Widget Guidance
    • Semantic Commit Guidance
    • Being Part of a Mobile Software Team and Working on a Shared Architecture
    • Understanding Deep Links for Any Development Platform
    • The Journey of a Developer: Stories of Becoming Junior, Middle, and Senior Developer
    • Becoming a Team Leader: Growing in Sync with Your Team
    • Why IP Changes Are Important for Mobile Applications in Flutter
    • Why Your Growing Mobile Team Needs CI/CD and How to Build a Winning Strategy
    • Dart in 2024: 20 Features You Need to Know With Code Examples and Scenarios
    • Remote Theme Management with API (JSON): Implementing a Helper in Flutter SDK
    • Understanding and Implementing Force Upgrade in Your Flutter Project
    • Life Lessons from the Bald Eagle: A Metaphor for Growth, Change, and Leadership
    • The Beauty of Imperfection: Why Today Doesn’t Need to Be Perfect
    • # The Reverse Curve of Productivity: When Social Cohesion in Software Teams Starts to Hurt **How str
    • 📱 Mobil Uygulamalarda GraphQL Tercihi: Bakım ve Maliyet Etkiler
    • 📉 Türkiye’de Yazılım Projelerinde Süreç Yönetimi ve Ekonomik Kayıp: Bir Bekâ Sorunu mu?
  • 📹VIDEOS
    • Introduction to Flutter Boilerplate! ( Turkish )
    • Flutter APIs effective using ( English )
    • Understand to SDK ( English )
  • Links
    • 💼 | Linkedin
    • 🆇 | x.com
    • 📧 | Mail me
Powered by GitBook
On this page
  • 📝 Introduction
  • ⚙️ Algorithm Overview
  • Algorithm Steps
  • Dart Implementation
  • Example Scenarios
  • 1. Online Shopping Platform - Cart Total Calculation
  • 2. Inventory Management System - Product Bundling
  • 3. Ride-Sharing Application - Fare Calculation
  • 🎉 Conclusion
  • 📚 References
  1. Dart

#02 Algorithm Guidance: Two Sum

📝 Introduction

This guidance explains how to find two numbers in an array that add up to a specific target value. It provides step-by-step instructions, sample code snippets, and real-world project examples.

⚙️ Algorithm Overview

The "Two Sum" algorithm aims to find two numbers in an array whose sum equals a given target value. The algorithm returns the indices of these two numbers.

Algorithm Steps

  1. Create a Map: Initialize an empty map to store the indices of array elements.

  2. Iterate Through Array:

    • Iterate through the array elements.

    • For each element, calculate its complement (target - current element).

    • Check if the complement exists in the map.

    • If found, return the indices of the current element and its complement.

    • If not found, add the current element and its index to the map.

  3. Handle Edge Cases: If no such pair is found, return an empty list.

Dart Implementation

class Solution {
  List<int> twoSum(List<int> nums, int target) {
    Map<int, int> numMap = {};

    for (int i = 0; i < nums.length; i++) {
      int complement = target - nums[i];
      if (numMap.containsKey(complement)) {
        return [numMap[complement], i];
      }
      numMap[nums[i]] = i;
    }

    return [];
  }
}

Example Scenarios

1. Online Shopping Platform - Cart Total Calculation

Dart Code:

void main() {
  Solution solution = Solution();
  List<int> nums = [10, 20, 30, 40];
  int target = 50;
  List<int> result = solution.twoSum(nums, target);
  print(result); // Output: [0, 2] (Indices of 10 and 30)
}

In an online shopping platform, the "Two Sum" algorithm can be used to find two items in the cart whose combined prices match a user's specified budget. This helps users manage their expenses and make informed purchasing decisions.

2. Inventory Management System - Product Bundling

Dart Code:

void main() {
  Solution solution = Solution();
  List<int> nums = [15, 25, 35, 45];
  int target = 60;
  List<int> result = solution.twoSum(nums, target);
  print(result); // Output: [0, 3] (Indices of 15 and 45)
}

In an inventory management system, the "Two Sum" algorithm can assist in identifying two products whose combined quantities meet a retailer's stocking requirements. This feature helps streamline inventory replenishment processes and optimize stock levels.

3. Ride-Sharing Application - Fare Calculation

Dart Code:

void main() {
  Solution solution = Solution();
  List<int> nums = [5, 10, 15, 20];
  int target = 25;
  List<int> result = solution.twoSum(nums, target);
  print(result); // Output: [1, 2] (Indices of 10 and 15)
}

In a ride-sharing application, the "Two Sum" algorithm can be applied to identify two ride options whose fares sum up to a user's specified budget. This functionality enables users to select the most cost-effective transportation options for their trips.

🎉 Conclusion

This guidance provided a step-by-step explanation of the "Two Sum" algorithm, accompanied by real-world project examples demonstrating its practical applications in various industries. The algorithm's versatility makes it valuable for solving a wide range of problems in software development.

📚 References


This guidance provides software developers with the step-by-step process of implementing the "Two Sum" algorithm, supported by real-world project examples and comprehensive explanations.

Previous#01 Algorithm Guidance: Implementing Calculation AlgorithmsNextFlutter MVVM Guidance

Last updated 6 months ago

Dart Programming Language Documentation:

Dart Language Tour:

Dart API Reference:

🎯
🧮
Dart.dev
Dart Language Tour
Dart API Reference