Created
July 12, 2017 14:56
-
-
Save MichaelaIvanova/8ba98b6d1f9ab6101738d186d96c9775 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace algos | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[] input = new int[] { 1, 3, 6, 4, 1, 2, -11 }; | |
solution(input); | |
} | |
public static int solution(int[] A) | |
{ | |
var onlyPositiveValues = A.Where(x => x > 0).ToList(); | |
var candidate = 1; | |
if (onlyPositiveValues.Any()) | |
{ | |
onlyPositiveValues.Sort(); | |
for (int i = 1; i < onlyPositiveValues.Count; i++) | |
{ | |
var previous = onlyPositiveValues[i-1]; | |
var curr = onlyPositiveValues[i]; | |
var diff = curr - previous; | |
if(diff > 1) // example 2 4 | |
{ | |
candidate = onlyPositiveValues[i - 1] + 1; | |
break; | |
} | |
} | |
return candidate; | |
} | |
else | |
{ | |
return candidate; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment