• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Advent Of Code 2020 – Day 1 – Report Repair

December 7, 2020 by varunshrivastava Leave a Comment

In this article, I’ll discuss on the Day – 1 problem of “Advent of Code”. And from today onwards, I will try to discuss everyday’s problem. Make sure you subscribe to the blog to stay in touch.

Feel free to share your ideas or even post your solutions in the comment section below.

Let’s start…

Table of Contents

  • PART – 1 of DAY – 1
    • Bruteforce Approach
    • Two Pointer Approach
    • Approach 3 – Maintain A Compliment Hash Set Of The Numbers
  • PART – 2 of DAY – 1
    • Approach 1 – Bruteforce
    • Approach 2 – Three Pointer

PART – 1 of DAY – 1

The problem is simple – Given a list of years, you have to find the two entries that sum to 2020; And the output will be retrieved after multiplying those two years.

Test Input

1721
979
366
299
675
1456

The very first thing that comes to the mind is Bruteforce approach.

Bruteforce Approach

In this approach, we loop over each and every combination of year in the list and check if their sum equals 2020. The solution is perfect and always works. But it is not so efficient.

The time complexity of this approach is O(n2).

Code for the same will be like this:

    // APPROACH 1 - BRUTE FORCE
    // Complexity - O(n2)
    int bruteforce(int target) {
        int  len = input.size();
        for (int i=0; i < len - 2; i++) 
            for (int j = i + 1; j < len - 1; j++) 
                    if (input[i] + input[j] == target) 
                        return input[i]  * input[j];

        return 0;
    }

This code will compare year in the list with every other year to check if their sum equals 2020. And if that condition is satisfied then multiplies both the numbers and returns the result.

Another approach to solve this is 2 pointer approach.

Two Pointer Approach

This approach is pretty awesome. This approach has a complexity of O(nlogn) which is a great improvement from the previous approach.

The nlogn comes from the fact that the array should be sorted. That is the prerequisite. This approach only works on the sorted array.

In this we start searching from the front and back simultaneously. We start by initializing the left pointer to the start of the array and right pointer to the end of the array. The with each iteration we compare the values at left pointer and right pointer. And if the sum equals 2020 then we return the multiplied result.

Let’s see the code:

    // APPROACH 2 - PART 1
    int two_pointer(int target) {
        vector<int> input = {1721,979,366,299,675,1456};
        sort(input.begin(), input.end());
        int j = input.size() - 1;
        for (int i = 0; i < input.size();) 
            if (input[i] + input[j] > target) j--;
            else if (input[i] + input[j] < target) i++;
            else return input[i] * input[j];
        
        return 0;
    }

This is a pretty optimized solution and guarantees that the result will be retrieved in the O(nlogn) time.

Approach 3 – Maintain A Compliment Hash Set Of The Numbers

There is one more approach that makes use of the HashSet to store the compliment of the visited numbers. The Time Complexity of this approach is as follows:

  • Time Complexity – O(n)
  • Space Complexity – O(n)

Basically we trade space for time. Here’s the idea behind this approach.

Let’s say you have the following list.

vector<int> arr = {1721,979,366,299,675,1456};

Here we will iterate over each and every item in the list and check for its existence in the set. If it is not present in the set then we insert the compliment of that number to the set. So that the next time if we encounter any number which is already present in the set, we would immediately know that its compliments exist.

Let’s take a look at the code to understand the logic better.

    // Approach 3 - Part - 1 | HashSet approach
    int hash_set_approach(int target) {
        unordered_set<int> complements;
        vector<int> input = {1721,979,366,299,675,1456};
        for (auto year: input) {
            int complement = target - year;
            if (complements.count(year) != 0) 
                return year * complement;
            else 
                complements.insert(complement);
            
        }

        return 0;
    }

As you can see in the above code. We go over each and every integer in the array and check its presence in the set. If the number is present we simply multiply the number with its complement to get the result.

But if the number is not present in the set then we take its complement and add to the set.

That way we keep a track of the numbers that we have seen before.

Great! you have seen all the three approaches with which the part one of the questions can be solved. Let’s take a look at part 2, shall we?

PART – 2 of DAY – 1

The problem is extended by adding one more requirement. That is, instead of finding the product from two numbers that sum up to 2020, you now have to find the product from three numbers that sums up to 2020.

Again, for solving the problem, you can expand the approach 1 – BruteForce approach which is the easiest but the time complexity now will be O(n^3). Or you can use approach 2- Three Pointer approach for which the time complexity will be O(n^2).

Approach 1 – Bruteforce

This is the fastest to impliment. You just have to insert one more loop for the third number.

    // APPROACH 1 - BRUTE FORCE
    // Complexity - O(n3)
    int bruteforce(int target) {
        int  len = input.size();
        for (int i=0; i < len - 2; i++) 
            for (int j = i + 1; j < len - 1; j++) 
                for (int k = j + 1; k < len; k++) 
                    if (input[i] + input[j] + input[k] == target) 
                        return input[i]  * input[j] * input[k];

        return 0;
    }

Approach 2 – Three Pointer

This is the extension of the two pointer approach. We will be doing the same thing but with 3 pointers.

Here, the first step is to sort the given array O(nlogn) we will take 3 variables that will be used for finding the sum (let’s say i,j & k).

The variables j and k will behave just the same way they did in the two pointer approach. The new variable i will be responsible for the third number in the sum.

Since, the code will contain a nested loop, the overall complexity of this algorithm will be O(nlogn + n2) => O(n^2).

Let’s directly look at the code to understand how it works.

    // APPROACH 2 - TWO POINTER
    // Complexity - O(nlogn) + O(n2) = O(n2)
    int three_pointer(int target) {
        sort(input.begin(), input.end());
        int len = input.size();
        for(int i = 0; i < len - 2; i++) 
            for (int j = i + 1, k = len - 1; j < k;) 
                if (input[i] + input[j] + input[k] > target) k--;
                else if (input[i] + input[j] + input[k] < target) j++;
                else return input[i]  * input[j] * input[k];
            
        return 0;
    }

These are the best ways I could come up with to solve the day one problem. Do share your comments and approach in the comments below.

Related

Filed Under: Programming Tagged With: aoc-2020, day-1

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version