This repository contains solutions of the Daily Coding Problem tasks
Daily Coding Problem #4
Ivan
Posted on January 2, 2020
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.
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;
}
}
}
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.
💖 💪 🙅 🚩
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.