Daily Coding Problem #4

cwetanow

Ivan

Posted on January 2, 2020

Daily Coding Problem #4

How it works?

The folks at Daily Coding Problem send you a problem that was asked at a top company every day for you to solve. The premium membership also offers the opportunity to verify your solution.

Problem #4

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.
Enter fullscreen mode Exit fullscreen mode

My solution

using System;
using System.Collections.Generic;
using System.Linq;

namespace DailyCodingProblem.Solutions.Problem04
{
    public class Solution
    {
        public static void Test()
        {
            var input = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse)
                .ToHashSet();

            var minPositiveNumber = GetMinimalPositiveNumber(input);

            Console.WriteLine(minPositiveNumber);
        }

        public static int GetMinimalPositiveNumber(HashSet<int> input)
        {
            var minPositiveNumber = 1;
            while (true)
            {
                if (!input.Contains(minPositiveNumber))
                {
                    break;
                }

                minPositiveNumber++;
            }

            return minPositiveNumber;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Solutions are posted in my Github account






.

Feel free to leave a like and post a comment.

If you also have any better solution in mind, by all means, share it, so we can learn from each other.

💖 💪 🙅 🚩
cwetanow
Ivan

Posted on January 2, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Daily Coding Problem #4
dev Daily Coding Problem #4

January 2, 2020

Daily Coding Problem #1
dev Daily Coding Problem #1

November 24, 2018

Daily Coding Problem #2
dev Daily Coding Problem #2

November 25, 2018

Daily Coding Problem #3
dev Daily Coding Problem #3

November 26, 2018