├── Triangle.cs ├── MinPerimeterRectangle.cs ├── MissingInteger-Sort.cs ├── FrogJmp.cs ├── PassingCars.cs ├── MissingInteger.cs ├── TapeEquilibrium.cs ├── PermCheck.cs ├── MaxProductOfThree.cs ├── TieRopes.cs ├── BinaryGap-v2.cs ├── Equi.cs ├── Fish.cs ├── Ladder.cs ├── StrSymmetryPoint.cs ├── MaxCounters.cs ├── NumberOfDiscIntersections.cs ├── Distinct.cs ├── FrogRiverOne.cs ├── PermMissingElem.cs ├── EquiLeader.cs ├── Peaks.cs ├── Ladder.wrong.cs ├── MinMaxDivision.cs ├── BinaryGap.cs ├── Flags.cs ├── MaxDoubleSliceSum.cs ├── CountSemiprimes.cs ├── BreakTheRope.cs └── README.md /Triangle.cs: -------------------------------------------------------------------------------- 1 | public static int solution(int[] A) 2 | { 3 | Array.Sort(A); 4 | 5 | for (int i = 2; i < A.Length; i++) 6 | { 7 | long sum = (long)A[i - 1] + (long)A[i - 2]; 8 | if (sum > A[i]) 9 | { 10 | return 1; 11 | } 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /MinPerimeterRectangle.cs: -------------------------------------------------------------------------------- 1 | public static int solution(int N) 2 | { 3 | var sqrt = (long)Math.Floor(Math.Sqrt(N)); 4 | 5 | for (long i = sqrt; i > 0; i--) 6 | { 7 | if (N % i == 0) // is divisible by i 8 | { 9 | return (int)(2 * ((i) + (N / i))); 10 | } 11 | } 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /MissingInteger-Sort.cs: -------------------------------------------------------------------------------- 1 | // Find the smallest positive integer that does not occur in a given sequence. 2 | class Solution { 3 | public int solution(int[] A) { 4 | 5 | Array.Sort(A); 6 | int result = 0; 7 | for (int i = 0; i < A.Length; i++) 8 | { 9 | if (A[i] > result + 1) 10 | { 11 | return result + 1; 12 | } 13 | result = A[i] > result ? A[i] : result; 14 | } 15 | 16 | return result + 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /FrogJmp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | 12 | } 13 | 14 | public static int solution(int X, int Y, int D) 15 | { 16 | if (D == 0) 17 | { 18 | return 0; 19 | } 20 | 21 | int jumps = (int)Math.Ceiling((double)(Y - X) / D); 22 | 23 | return jumps; 24 | } 25 | } -------------------------------------------------------------------------------- /PassingCars.cs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | const int maxSize = 1000000000; 3 | public int solution(int[] A) 4 | { 5 | int total = 0; 6 | int preSum = 0; 7 | 8 | foreach (int car in A) 9 | { 10 | if (car == 0) 11 | { 12 | preSum++; 13 | } 14 | else 15 | { 16 | total += preSum; 17 | } 18 | 19 | if (total > maxSize) 20 | { 21 | return -1; 22 | } 23 | } 24 | 25 | return total; 26 | } 27 | } -------------------------------------------------------------------------------- /MissingInteger.cs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | const int maxSize = 100000; 3 | 4 | public int solution(int[] A) 5 | { 6 | int[] counter = new int[maxSize]; 7 | 8 | foreach (int number in A) 9 | { 10 | if (number > 0 && number <= maxSize) 11 | { 12 | counter[number - 1] = 1; 13 | } 14 | } 15 | 16 | for (int i = 0; i < maxSize; i++) 17 | { 18 | if (counter[i] == 0) 19 | { 20 | return i + 1; 21 | } 22 | } 23 | 24 | return maxSize + 1; 25 | } 26 | } -------------------------------------------------------------------------------- /TapeEquilibrium.cs: -------------------------------------------------------------------------------- 1 | private const int maxLenght = 100000; 2 | 3 | public static int Solution(int[] A) 4 | { 5 | int length = A.Length; 6 | 7 | int[] partialSums = new int[maxLenght]; 8 | 9 | // calculate partial sums 10 | for (int i = 0; i < length; i++) 11 | { 12 | partialSums[i] = i > 0 ? A[i] + partialSums[i - 1] : A[i]; 13 | } 14 | 15 | int minimalValue = int.MaxValue; 16 | 17 | // get minimal value 18 | for (int i = 0; i < length -1 ; i++) 19 | { 20 | int difference = Math.Abs(partialSums[i] - (partialSums[length - 1] - partialSums[i])); 21 | 22 | minimalValue = difference < minimalValue ? difference : minimalValue; 23 | } 24 | 25 | return minimalValue; 26 | } -------------------------------------------------------------------------------- /PermCheck.cs: -------------------------------------------------------------------------------- 1 | private const int maxLength = 100000; 2 | private const int isPermutation = 1; 3 | private const int isNotPermutation = 0; 4 | 5 | public int solution(int[] A) 6 | { 7 | int length = A.Length; 8 | 9 | int[] countingArray = new int[maxLength]; // initialized to 0 by default 10 | 11 | for (int i = 0; i < length; i++) 12 | { 13 | if (A[i] > maxLength) 14 | { 15 | return isNotPermutation; 16 | } 17 | else 18 | { 19 | countingArray[A[i] - 1] = 1; 20 | } 21 | } 22 | 23 | for (int i = 0; i < length; i++) 24 | { 25 | if (countingArray[i] == 0) 26 | { 27 | return isNotPermutation; 28 | } 29 | } 30 | 31 | return isPermutation; 32 | } -------------------------------------------------------------------------------- /MaxProductOfThree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | 12 | } 13 | 14 | public static int solution(int[] A) 15 | { 16 | Array.Sort(A); 17 | 18 | var topMaximum = A[A.Length - 1] * A[A.Length - 2] * A[A.Length - 3]; 19 | var bottomMaxium = A[0] * A[1] * A[A.Length - 1]; 20 | 21 | return Math.Max(topMaximum, bottomMaxium); 22 | } 23 | } 24 | 25 | // Tests 26 | [TestMethod] 27 | public void TestSolution() 28 | { 29 | Assert.AreEqual(Solution.solution(new[] { -3, 1, 2, -2, 5, 6 }), 60); 30 | 31 | Assert.AreEqual(Solution.solution(new[] { 1, 1, 2 }), 2); 32 | 33 | Assert.AreEqual(Solution.solution(new[] { -5, 5, -5, 4 }), 125); 34 | } -------------------------------------------------------------------------------- /TieRopes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Solution 4 | { 5 | public static void Main(string[] args) 6 | { 7 | var response = solution(4, new int[] { 1, 2, 3, 4, 1, 1, 3 }); 8 | 9 | Console.WriteLine(string.Join(",", response)); 10 | } 11 | 12 | public static int solution(int k, int[] A) 13 | { 14 | int numberOfRopes = 0; 15 | int currentRopeLenght = 0; 16 | for (int i = 0; i < A.Length; i++) 17 | { 18 | if (A[i] >= k) 19 | { 20 | numberOfRopes++; 21 | currentRopeLenght = 0; 22 | continue; 23 | } 24 | 25 | currentRopeLenght += A[i]; 26 | if (currentRopeLenght >= k) 27 | { 28 | numberOfRopes++; 29 | currentRopeLenght = 0; 30 | } 31 | } 32 | 33 | return numberOfRopes; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /BinaryGap-v2.cs: -------------------------------------------------------------------------------- 1 | public static int Solution(int N) 2 | { 3 | var sequenceCounter = 0; 4 | var iterations = Math.Floor(Math.Log(N, 2) + 1); 5 | var maxSequence = 0; 6 | var startingOne = false; 7 | 8 | for (int i = 0; i < iterations; i++) 9 | { 10 | // check if binary digit is 0 11 | var isCero = (N & (1 << i)) == 0; 12 | Console.Write(isCero ? "0" : "1"); 13 | if (isCero && startingOne) 14 | { 15 | // we are in a 0, only count if there was a 1 before 16 | sequenceCounter++; 17 | } 18 | else if (!isCero) 19 | { 20 | // we are in a 1 and the sequence had already started 21 | if (startingOne && sequenceCounter > maxSequence) 22 | { 23 | maxSequence = sequenceCounter; 24 | } 25 | // restart the sequence 26 | startingOne = true; 27 | sequenceCounter = 0; 28 | } 29 | } 30 | 31 | return maxSequence; 32 | } 33 | -------------------------------------------------------------------------------- /Equi.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Solution 4 | { 5 | static void Main(string[] args) 6 | { 7 | int sol = solution(new int[] { -1, 3, -4, 5, 1, -6, 2, 1 }); 8 | 9 | Console.WriteLine(sol); 10 | } 11 | 12 | static int solution(int[] A) 13 | { 14 | if (A.Length == 0) 15 | { 16 | return -1; 17 | } 18 | 19 | if (A.Length == 1) 20 | { 21 | return 0; 22 | } 23 | 24 | // calculate prefix sums 25 | long prefixSum = 0; 26 | for (int i = 0; i < A.Length; i++) 27 | { 28 | prefixSum += (long)A[i]; 29 | } 30 | 31 | // find equi 32 | long leftSum = 0; 33 | for (int i = 0; i < A.Length; i++) 34 | { 35 | long rightSum = prefixSum - (leftSum + A[i]); 36 | 37 | if (leftSum == rightSum) 38 | { 39 | return i; 40 | } 41 | 42 | leftSum += (long)A[i]; 43 | } 44 | 45 | return -1; 46 | } 47 | } -------------------------------------------------------------------------------- /Fish.cs: -------------------------------------------------------------------------------- 1 | public static int solution(int[] A, int[] B) 2 | { 3 | Stack upstreamQueue = new Stack(); // 0's 4 | Stack downstreamQueue = new Stack(); // 1's 5 | 6 | for (int i = 0; i < A.Length; i++) 7 | { 8 | if (B[i] == 0) // is upstream 9 | { 10 | if (downstreamQueue.Count > 0) // there is fish to eat 11 | { 12 | while (downstreamQueue.Count > 0 && downstreamQueue.Peek() < A[i]) 13 | { 14 | downstreamQueue.Pop(); 15 | } 16 | 17 | if (downstreamQueue.Count == 0) 18 | { 19 | upstreamQueue.Push(A[i]); 20 | } 21 | } 22 | else 23 | { 24 | upstreamQueue.Push(A[i]); 25 | } 26 | } 27 | else // is downstream 28 | { 29 | downstreamQueue.Push(A[i]); 30 | } 31 | } 32 | 33 | return upstreamQueue.Count + downstreamQueue.Count; 34 | } -------------------------------------------------------------------------------- /Ladder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | class Solution 6 | { 7 | public void Main(string[] args) 8 | { 9 | var response = solution(new int[] { 4, 4, 5, 5, 1 }, new int[] { 3, 2, 4, 3, 1 }); 10 | 11 | Console.WriteLine(string.Join(",", response)); 12 | } 13 | 14 | public int[] solution(int[] A, int[] B) 15 | { 16 | int maxLadderSize = A.Max(); 17 | 18 | ulong[] fibo = BuildFiboSecuence(maxLadderSize +2); 19 | int[] result = new int[A.Length]; 20 | 21 | for (int i = 0; i < A.Length; i++) 22 | { 23 | ulong positionResult = fibo[A[i]]; 24 | 25 | result[i] = (int)(positionResult & (ulong)((1 << B[i]) - 1)); 26 | } 27 | 28 | return result; 29 | } 30 | 31 | public ulong[] BuildFiboSecuence(int ladderSize) 32 | { 33 | ulong[] fibo = new ulong[ladderSize]; 34 | 35 | fibo[0] = 1; 36 | fibo[1] = 1; 37 | 38 | for (int i = 2; i < ladderSize; i++) 39 | { 40 | fibo[i] = fibo[i - 1] + fibo[i - 2]; 41 | } 42 | 43 | return fibo; 44 | } 45 | } -------------------------------------------------------------------------------- /StrSymmetryPoint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | Console.WriteLine(solution("racecar")); 12 | } 13 | 14 | public static int solution(string word) 15 | { 16 | if (word.Length % 2 != 1) 17 | { 18 | return -1; 19 | } 20 | 21 | int midPoint = word.Length / 2; 22 | 23 | for (int i = 0; i < midPoint; i++) 24 | { 25 | if (word[i] != word[word.Length - 1 - i]) 26 | { 27 | return -1; 28 | } 29 | } 30 | 31 | return midPoint; 32 | } 33 | } 34 | 35 | // tests 36 | [TestMethod] 37 | public void TestSolution() 38 | { 39 | Assert.AreEqual(Solution.solution("aabaac"), -1); 40 | 41 | Assert.AreEqual(Solution.solution("racecar"), 3); 42 | 43 | Assert.AreEqual(Solution.solution("aabbcc"), -1); 44 | 45 | Assert.AreEqual(Solution.solution("aabbgbbaa"), 4); 46 | 47 | Assert.AreEqual(Solution.solution("atoyotasatoyota"), 7); 48 | } -------------------------------------------------------------------------------- /MaxCounters.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | // you can also use other imports, for example: 4 | // using System.Collections.Generic; 5 | 6 | // you can use Console.WriteLine for debugging purposes, e.g. 7 | // Console.WriteLine("this is a debug message"); 8 | 9 | class Solution { 10 | const int maxSize = 100000; 11 | 12 | public int[] solution(int N, int[] A) 13 | { 14 | int[] counters = new int[maxSize]; 15 | int maxCounterValue = 0; 16 | int maxCounterOverride = 0; 17 | 18 | foreach (int number in A) 19 | { 20 | if (number == N + 1) 21 | { 22 | maxCounterOverride = maxCounterValue; 23 | } 24 | else 25 | { 26 | counters[number - 1] = counters[number - 1] < maxCounterOverride ? maxCounterOverride + 1 : counters[number - 1] + 1; 27 | maxCounterValue = counters[number - 1] > maxCounterValue ? counters[number - 1] : maxCounterValue; 28 | } 29 | } 30 | 31 | for (int i = 0; i < N; i++) 32 | { 33 | counters[i] = counters[i] < maxCounterOverride ? maxCounterOverride : counters[i]; 34 | } 35 | 36 | return counters.Take(N).ToArray(); 37 | } 38 | } -------------------------------------------------------------------------------- /NumberOfDiscIntersections.cs: -------------------------------------------------------------------------------- 1 | public static int solution(int[] A) 2 | { 3 | // obtain ordered starts array 4 | long[] starts = new long[A.Length]; 5 | for (int i = 0; i < A.Length; i++) 6 | { 7 | starts[i] = i - A[i]; 8 | } 9 | Array.Sort(starts); 10 | 11 | // obtain ordered ends array 12 | long[] ends = new long[A.Length]; 13 | for (int i = 0; i < A.Length; i++) 14 | { 15 | ends[i] = i + A[i]; 16 | } 17 | Array.Sort(ends); 18 | 19 | // find intersections 20 | int intersections = 0; 21 | int counter = 0; 22 | int startsIndex = 0; 23 | int endsIndex = 0; 24 | 25 | while (startsIndex < starts.Length || endsIndex < ends.Length) 26 | { 27 | if (startsIndex < starts.Length && starts[startsIndex] <= ends[endsIndex]) 28 | { 29 | counter++; 30 | startsIndex++; 31 | } 32 | else 33 | { 34 | counter--; 35 | intersections = intersections + counter; 36 | endsIndex = endsIndex <= (ends.Length - 1) ? endsIndex + 1 : endsIndex; 37 | } 38 | 39 | if (intersections > 10000000) 40 | { 41 | return -1; 42 | } 43 | } 44 | 45 | return intersections; 46 | } -------------------------------------------------------------------------------- /Distinct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | 12 | } 13 | 14 | public static int solution(int[] A) 15 | { 16 | if (A.Length == 0) 17 | { 18 | return 0; 19 | } 20 | 21 | Array.Sort(A); 22 | 23 | int distinctCount = 1; 24 | for (int i = 1; i < A.Length; i++) 25 | { 26 | if (A[i] != A[i - 1]) 27 | { 28 | distinctCount++; 29 | } 30 | } 31 | 32 | return distinctCount; 33 | } 34 | } 35 | 36 | // tests 37 | [TestMethod] 38 | public void TestSolution() 39 | { 40 | Assert.AreEqual(Solution.solution(new[] { 2, 1, 1, 2, 3, 1 }), 3); 41 | 42 | Assert.AreEqual(Solution.solution(new[] { -3, 1, 2, -2, 5, 6 }), 6); 43 | 44 | Assert.AreEqual(Solution.solution(new[] { 1, 1, 2 }), 2); 45 | 46 | Assert.AreEqual(Solution.solution(new[] { 1, 2, 2, 2 }), 2); 47 | 48 | Assert.AreEqual(Solution.solution(new[] { -5, 5, -5, 4 }), 3); 49 | 50 | Assert.AreEqual(Solution.solution(new[] { 0, 0, 0, 0 }), 1); 51 | } 52 | -------------------------------------------------------------------------------- /FrogRiverOne.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | public class Solution 7 | { 8 | public static void Main(string[] args) 9 | { 10 | solution(5, new[] { 1, 3, 1, 4, 2, 3, 5, 4 }); 11 | } 12 | 13 | public static int solution(int X, int[] A) 14 | { 15 | bool[] leavesInPosition = new bool[X + 1]; 16 | 17 | int occupiedPositions = 0; 18 | for (int i = 0; i < A.Length; i++) 19 | { 20 | int position = A[i]; 21 | if (position <= X && !leavesInPosition[position]) 22 | { 23 | leavesInPosition[position] = true; 24 | occupiedPositions++; 25 | } 26 | 27 | if (occupiedPositions == X) 28 | { 29 | return i; 30 | } 31 | } 32 | 33 | return -1; 34 | } 35 | } 36 | 37 | // Separate test project 38 | [TestMethod] 39 | public void TestSolution() 40 | { 41 | Assert.AreEqual(Solution.solution(5, new[] { 1, 3, 1, 4, 2, 3, 5, 4 }), 6); 42 | 43 | var bigRange = Enumerable.Range(1, 100000).ToArray(); 44 | 45 | Assert.AreEqual(Solution.solution(3, bigRange), 2); 46 | 47 | Assert.AreEqual(Solution.solution(1, new int[0]), -1); 48 | 49 | Assert.AreEqual(Solution.solution(1, new[] { 1 }), 0); 50 | } -------------------------------------------------------------------------------- /PermMissingElem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | public class Solution 7 | { 8 | public static void Main(string[] args) 9 | { 10 | solution(new[] { 1, 4, 16 }); 11 | } 12 | 13 | public static int solution(int[] A) 14 | { 15 | // calculate prefix sum 16 | long arraySum = 0; 17 | foreach (int i in A) // A.Sum() breaks as it uses Int32 18 | { 19 | arraySum += i; 20 | } 21 | 22 | // calculate theorical sum 23 | long n = A.Length + 1; 24 | long theoricalSum = (n * (n + 1)) / 2; // notice long as return type to avoid overflow 25 | 26 | // return difference 27 | return (int)(theoricalSum - arraySum); 28 | } 29 | } 30 | 31 | 32 | // Separate test project 33 | [TestMethod] 34 | public void TestSolution() 35 | { 36 | var bigRange = Enumerable.Range(2, 100000).ToArray(); 37 | 38 | Assert.AreEqual(Solution.solution(bigRange), 1); 39 | 40 | Assert.AreEqual(Solution.solution(new[] { 1, 2, 3, 4 }), 5); 41 | 42 | Assert.AreEqual(Solution.solution(new[] { 2, 3, 1, 5 }), 4); 43 | 44 | Assert.AreEqual(Solution.solution(new[] { 2, 3, 4 }), 1); 45 | 46 | Assert.AreEqual(Solution.solution(new[] { 1, 2, 3, 4, 6 }), 5); 47 | 48 | Assert.AreEqual(Solution.solution(new int[0]), 1); 49 | 50 | Assert.AreEqual(Solution.solution(new[] { 2 }), 1); 51 | } -------------------------------------------------------------------------------- /EquiLeader.cs: -------------------------------------------------------------------------------- 1 | public static int solution(int[] A) 2 | { 3 | int leader = 0; 4 | int size = 0; 5 | int leaderCount = 0; 6 | 7 | // find the leader (this algorithm to find the leader in O(N) is obtained from the Leader lesson) 8 | for (int i = 0; i < A.Length; i++) 9 | { 10 | if (size == 0) 11 | { 12 | size++; 13 | leader = A[i]; 14 | } 15 | else 16 | { 17 | if (A[i] == leader) 18 | { 19 | size++; 20 | } 21 | else 22 | { 23 | size--; 24 | } 25 | } 26 | } 27 | 28 | if (size == 0) 29 | { 30 | return 0; 31 | } 32 | 33 | leaderCount = A.Count(i => i == leader); 34 | 35 | if (leaderCount <= A.Length / 2) 36 | { 37 | return 0; 38 | } 39 | 40 | int equiCount = 0; 41 | 42 | int leftSideLeaders = 0; 43 | int rightSideLeaders = 0; 44 | for (int i = 0; i < A.Length; i++) 45 | { 46 | if (A[i] == leader) 47 | { 48 | leftSideLeaders++; 49 | } 50 | 51 | rightSideLeaders = leaderCount - leftSideLeaders; 52 | 53 | bool leftSideHasLeader = leftSideLeaders > (i + 1) / 2; 54 | bool rightSideHasLeader = rightSideLeaders > (A.Length - (i + 1)) / 2; 55 | 56 | if (leftSideHasLeader && rightSideHasLeader) 57 | { 58 | equiCount++; 59 | } 60 | } 61 | 62 | return equiCount; 63 | } -------------------------------------------------------------------------------- /Peaks.cs: -------------------------------------------------------------------------------- 1 | // see http://rafal.io/posts/codility-peaks.html for a better solution 2 | public static int solution(int[] A) 3 | { 4 | if (A.Length < 3) 5 | { 6 | return 0; 7 | } 8 | 9 | // get a new array with peaks 10 | bool[] peaks = new bool[A.Length]; 11 | int peaksCounter = 0; 12 | for (int i = 1; i < A.Length - 1; i++) 13 | { 14 | if (A[i] > A[i - 1] && A[i] > A[i + 1]) 15 | { 16 | peaks[i] = true; 17 | peaksCounter++; 18 | } 19 | } 20 | 21 | // start testing each possible division 22 | for (int groupSize = 1; groupSize <= A.Length; groupSize++) 23 | { 24 | if (A.Length % groupSize == 0) // is divisible 25 | { 26 | // test actual divisor 27 | if (PeaksCorrect(peaks, peaksCounter, groupSize)) 28 | { 29 | return A.Length / groupSize; 30 | } 31 | } 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | private static bool PeaksCorrect(bool[] peaks, int peaksCounter, int groupSize) 38 | { 39 | int groupNumber = peaks.Length / groupSize; 40 | 41 | // should be at least one peak per group 42 | if (peaksCounter < groupNumber) 43 | { 44 | return false; 45 | } 46 | 47 | // this is what should be improved (should only store the peaks indexes, not the whole array) 48 | for (int i = 0; i < groupNumber; i++) 49 | { 50 | bool hasPeak = peaks.Skip(i * groupSize).Take(groupSize).Any(t => t == true); 51 | 52 | if (!hasPeak) 53 | { 54 | return false; 55 | } 56 | } 57 | 58 | return true; 59 | } -------------------------------------------------------------------------------- /Ladder.wrong.cs: -------------------------------------------------------------------------------- 1 | // This is a Naive solution not using Fibonnacci, it uses the Combinatorial principle behind where k = (positions)! / (1's!)*(2's!) 2 | public static int[] solution(int[] A, int[] B) 3 | { 4 | ulong[] cache = new ulong[30001]; 5 | int[] result = new int[A.Length]; 6 | 7 | for (int i = 0; i < A.Length; i++) 8 | { 9 | ulong positionResult; 10 | if (cache[A[i]] > 0) 11 | { 12 | positionResult = cache[A[i]]; 13 | } 14 | else 15 | { 16 | positionResult = CalculatePermutations(A[i]); 17 | cache[A[i]] = positionResult; 18 | } 19 | 20 | result[i] = (int)(positionResult % Math.Pow(2, int.MaxValue)); 21 | } 22 | 23 | return result; 24 | } 25 | 26 | public static ulong CalculatePermutations(int ladderSize) 27 | { 28 | if (ladderSize == 1) 29 | { 30 | return 1; 31 | } 32 | 33 | ulong combinationsResult = 0; 34 | int maxIterations = ladderSize / 2 + ladderSize % 2; 35 | for (int i = ladderSize; i >= maxIterations; i--) 36 | { 37 | int twosAmount = ladderSize - i; 38 | int onesAmount = i - twosAmount; 39 | ulong combinations = factorial(i) / (factorial(onesAmount) * factorial(twosAmount)); 40 | 41 | combinationsResult += combinations; 42 | } 43 | 44 | return combinationsResult > 0 ? combinationsResult : 1; 45 | } 46 | 47 | public static ulong factorial(int number) 48 | { 49 | ulong result = (ulong)number; 50 | 51 | for (int i = 1; i < number; i++) 52 | { 53 | result = result * (ulong)i; 54 | } 55 | 56 | return result > 0 ? result : 1; 57 | } 58 | } -------------------------------------------------------------------------------- /MinMaxDivision.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | class Solution 6 | { 7 | public static void Main(string[] args) 8 | { 9 | var response = solution(2, 10, new int[] { 4, 4 }); 10 | 11 | Console.WriteLine(response); 12 | } 13 | 14 | public static int solution(int K, int M, int[] A) 15 | { 16 | long sumLowerBound = A.Max(); 17 | long sumUpperBound = A.Sum(); 18 | 19 | long result = 0; 20 | // binary search in the large sums testing for the validity of the solution 21 | while (sumLowerBound <= sumUpperBound) 22 | { 23 | long midVal = (sumLowerBound + sumUpperBound) / 2; 24 | bool isValid = isValidSolution(A, midVal, K); 25 | 26 | if (isValid) 27 | { 28 | sumUpperBound = midVal - 1; 29 | result = midVal; 30 | } 31 | else 32 | { 33 | sumLowerBound = midVal + 1; 34 | } 35 | } 36 | 37 | return (int)result; 38 | } 39 | 40 | public static bool isValidSolution(int[] A, long M, int K) 41 | { 42 | int partialSum = A[0]; 43 | int intervals = 0; 44 | for (int i = 1; i < A.Length; i++) 45 | { 46 | if (partialSum + A[i] > M) 47 | { 48 | partialSum = A[i]; 49 | intervals++; 50 | } 51 | else 52 | { 53 | partialSum += A[i]; 54 | } 55 | 56 | if (intervals >= K) 57 | { 58 | return false; 59 | } 60 | } 61 | 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /BinaryGap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | 12 | } 13 | 14 | public static int solution(int N) 15 | { 16 | string binaryRepresentation = Convert.ToString(N, 2); 17 | 18 | // search max binary gap 19 | int longestBinaryGap = 0; 20 | int binaryGapLenght = 0; 21 | for (int i = 1; i < binaryRepresentation.Length; i++) 22 | { 23 | if (binaryRepresentation[i-1] == '1' && binaryRepresentation[i] == '0') 24 | { 25 | binaryGapLenght = 1; 26 | } 27 | else if (binaryRepresentation[i - 1] == '0' && binaryRepresentation[i] == '0') 28 | { 29 | binaryGapLenght++; 30 | } 31 | else if (binaryRepresentation[i - 1] == '0' && binaryRepresentation[i] == '1') 32 | { 33 | longestBinaryGap = Math.Max(longestBinaryGap, binaryGapLenght); 34 | } 35 | } 36 | 37 | return longestBinaryGap; 38 | } 39 | } 40 | 41 | // tests 42 | [TestMethod] 43 | public void TestSolution() 44 | { 45 | Assert.AreEqual(Solution.solution(9), 2); 46 | 47 | Assert.AreEqual(Solution.solution(2), 0); 48 | 49 | Assert.AreEqual(Solution.solution(5), 1); 50 | 51 | Assert.AreEqual(Solution.solution(1041), 5); 52 | 53 | Assert.AreEqual(Solution.solution(0), 0); 54 | 55 | Assert.AreEqual(Solution.solution(511), 0); 56 | 57 | Assert.AreEqual(Solution.solution(126), 0); 58 | 59 | Assert.AreEqual(Solution.solution(12320), 6); 60 | } -------------------------------------------------------------------------------- /Flags.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | // using simple binary search similar to JS solution here: http://stackoverflow.com/questions/19466115/peak-and-flag-codility-latest-chellange 6 | public static int solution(int[] A) 7 | { 8 | if (A.Length < 3) 9 | { 10 | return 0; 11 | } 12 | 13 | // get a new array with peaks 14 | List peaks = new List(); 15 | for (int i = 1; i < A.Length - 1; i++) 16 | { 17 | if (A[i] > A[i - 1] && A[i] > A[i + 1]) 18 | { 19 | peaks.Add(i); 20 | } 21 | } 22 | 23 | // if a 1-2 peaks, we can return them directly 24 | if (peaks.Count <= 2) 25 | { 26 | return peaks.Count; 27 | } 28 | 29 | // start binary search 30 | int maxFlags = peaks.Count; 31 | int minFlags = 1; 32 | int result = 0; 33 | while (maxFlags >= minFlags) 34 | { 35 | int flags = (minFlags + maxFlags) / 2; // get middle of interval (binary search) 36 | if (CheckFlags(flags, peaks)) 37 | { 38 | result = flags; 39 | minFlags = flags + 1; 40 | } 41 | else 42 | { 43 | maxFlags = flags - 1; 44 | } 45 | } 46 | 47 | return result; 48 | } 49 | 50 | private static bool CheckFlags(int flagsCount, List flags) 51 | { 52 | int i = 1; 53 | // assume first flag in first position 54 | int lastFlag = flags[0]; 55 | int actualFlags = flagsCount - 1; 56 | while (actualFlags > 0 && i < flags.Count) 57 | { 58 | if (flags[i] - lastFlag >= flagsCount) 59 | { 60 | actualFlags--; // can put one flag 61 | lastFlag = flags[i]; 62 | } 63 | 64 | i++; 65 | } 66 | 67 | return actualFlags == 0; // all flags were placed 68 | } -------------------------------------------------------------------------------- /MaxDoubleSliceSum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | public class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | 12 | } 13 | 14 | public static int solution(int[] A) 15 | { 16 | // see http://codesays.com/2014/solution-to-max-double-slice-sum-by-codility/ 17 | if (A.Length < 4) 18 | { 19 | return 0; 20 | } 21 | 22 | // get starting max slice 23 | int[] endingMaxSum = new int[A.Length]; 24 | for (int i = 1; i < A.Length; i++) 25 | { 26 | endingMaxSum[i] = Math.Max(0, endingMaxSum[i - 1] + A[i]); 27 | } 28 | 29 | // get ending max slice 30 | int[] startingMaxSum = new int[A.Length]; 31 | for (int i = A.Length - 2; i >= 0; i--) 32 | { 33 | startingMaxSum[i] = Math.Max(0, startingMaxSum[i + 1] + A[i]); 34 | } 35 | 36 | // find max 37 | int maxDoubleSlice = 0; 38 | for (int i = 0; i < A.Length - 2; i++) 39 | { 40 | maxDoubleSlice = Math.Max(maxDoubleSlice, endingMaxSum[i] + startingMaxSum[i + 2]); 41 | } 42 | 43 | return maxDoubleSlice; 44 | } 45 | } 46 | 47 | // tests 48 | [TestMethod] 49 | public void TestSolution() 50 | { 51 | Assert.AreEqual(Solution.solution(new[] { 6, 1, 5, 6, 4, 2, 9, 4 }), 26); 52 | 53 | Assert.AreEqual(Solution.solution(new[] { 3, 2, 6, -1, 4, 5, -1, 2 }), 17); 54 | 55 | Assert.AreEqual(Solution.solution(new[] { 2, 1, 1, 2, 3, 1 }), 6); 56 | 57 | Assert.AreEqual(Solution.solution(new[] { -3, 1, 2, -2, 5, 6 }), 8); 58 | 59 | Assert.AreEqual(Solution.solution(new[] { 1, 1, 2 }), 0); 60 | 61 | Assert.AreEqual(Solution.solution(new[] { 1, 2, 2, 2 }), 2); 62 | 63 | Assert.AreEqual(Solution.solution(new[] { -5, 5, -5, 4 }), 5); 64 | 65 | Assert.AreEqual(Solution.solution(new[] { 0, 0, 0, 0 }), 0); 66 | 67 | Assert.AreEqual(Solution.solution(new[] { -1, -1, -1, -1 }), 0); 68 | 69 | Assert.AreEqual(Solution.solution(new[] { 1, 1, 0, 10, -100, 10, 0 }), 21); 70 | } 71 | -------------------------------------------------------------------------------- /CountSemiprimes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | public class Solution 7 | { 8 | public static void Main(string[] args) 9 | { 10 | solution(26, new[] { 1, 4, 16 }, new[] { 26, 10, 20 }); 11 | } 12 | 13 | public static int[] solution(int N, int[] P, int[] Q) 14 | { 15 | var sieveInput = Enumerable.Repeat(true, N + 1).ToArray(); 16 | var nSqrt = Math.Sqrt(N); 17 | 18 | // perform sieve 19 | sieveInput[0] = false; sieveInput[1] = false; 20 | for (int i = 2; i < nSqrt; i++) 21 | { 22 | if (sieveInput[i]) 23 | { 24 | int j = i * i; 25 | while (j <= N) 26 | { 27 | sieveInput[j] = false; 28 | j += i; 29 | } 30 | } 31 | } 32 | 33 | // get array of primes 34 | var primes = new List(); 35 | for (int i = 0; i < sieveInput.Length; i++) 36 | { 37 | if (sieveInput[i]) 38 | { 39 | primes.Add(i); 40 | } 41 | } 42 | 43 | // calculate semiprimes 44 | var semiPrimes = new int[N + 1]; 45 | for (int i = 0; i <= nSqrt; i++) 46 | { 47 | for (int j = i; j < primes.Count(); j++) 48 | { 49 | var semiPrime = primes[i] * primes[j]; 50 | if (semiPrime > N) 51 | { 52 | break; 53 | } 54 | 55 | semiPrimes[semiPrime] = 1; 56 | } 57 | } 58 | 59 | // calculate prefix sums 60 | var semiPrimesCount = new int[N + 1]; 61 | for (int i = 1; i < semiPrimesCount.Length; i++) 62 | { 63 | semiPrimesCount[i] = semiPrimesCount[i - 1] + semiPrimes[i]; 64 | } 65 | 66 | //obtain result 67 | var result = new List(); 68 | for (int i = 0; i < P.Length; i++) 69 | { 70 | result.Add(semiPrimesCount[Q[i]] - semiPrimesCount[P[i] - 1]); 71 | } 72 | 73 | return result.ToArray(); 74 | } 75 | } -------------------------------------------------------------------------------- /BreakTheRope.cs: -------------------------------------------------------------------------------- 1 | // Got 87% without binary search https://codility.com/demo/results/trainingWNABKT-ATU/ 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | class Solution 8 | { 9 | public static void Main(string[] args) 10 | { 11 | // var response = solution(new int[] { 5, 3, 6, 3, 3 }, new int[] { 2, 3, 1, 1, 2 }, new int[] { -1, 0, -1, 0, 3 }); 12 | // var response = solution(new int[] { 5 }, new int[] { 2 }, new int[] { -1 }); 13 | var response = solution(new int[] { 5 }, new int[] { 6 }, new int[] { -1 }); 14 | 15 | Console.WriteLine(response); 16 | } 17 | 18 | public static int solution(int[] A, int[] B, int[] C) 19 | { 20 | Node roof = new Node() { Id = -1, Durability = int.MaxValue, Weight = 0 }; 21 | 22 | Node[] nodes = new Node[A.Length]; // node array for O(1) searching 23 | 24 | for (int i = 0; i < A.Length; i++) 25 | { 26 | Node newNode = new Node() { Id = i, Weight = B[i], Durability = A[i] - B[i] }; 27 | nodes[i] = newNode; 28 | 29 | if (newNode.Durability < 0) 30 | { 31 | return i - 1; 32 | } 33 | 34 | if (C[i] == -1) 35 | { 36 | roof.Children.Add(newNode); 37 | newNode.Parent = roof; 38 | continue; // if hung from the roof, no need to check 39 | } 40 | else 41 | { 42 | nodes[C[i]].Children.Add(newNode); 43 | newNode.Parent = nodes[C[i]]; 44 | } 45 | 46 | Node searchNode = nodes[C[i]]; 47 | while (searchNode.Id != -1) // go from leaf to roof 48 | { 49 | searchNode.Durability = searchNode.Durability - newNode.Weight; 50 | 51 | if (searchNode.Durability < 0) // rope was broken 52 | { 53 | return i; 54 | } 55 | 56 | searchNode = searchNode.Parent; 57 | } 58 | } 59 | 60 | return A.Length; 61 | } 62 | 63 | public class Node 64 | { 65 | public int Weight { get; set; } 66 | 67 | public int Id { get; set; } 68 | 69 | public int Durability { get; set; } 70 | 71 | public Node Parent { get; set; } 72 | 73 | public List Children { get; set; } 74 | 75 | public Node() 76 | { 77 | this.Children = new List(); 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codility Exercises in C# # 2 | 3 | from https://codility.com/programmers/ 4 | 5 | ### Exercises 6 | 7 | **Iterations** 8 | 1. BinaryGap.cs (two versions): https://app.codility.com/programmers/lessons/1-iterations/binary_gap/ 9 | 10 | **Time Complexity** 11 | 12 | 1. FrogJmp.cs: https://codility.com/programmers/task/frog_jmp/ 13 | 1. TapeEquilibrium.cs: https://codility.com/programmers/task/tape_equilibrium 14 | 1. PermMissingElem.cs: https://codility.com/programmers/task/perm_missing_elem 15 | 16 | **Counting Elements** 17 | 18 | 1. FrogRiverOne.cs: https://codility.com/programmers/task/frog_river_one 19 | 1. PermCheck.cs: https://codility.com/programmers/task/perm_check 20 | 1. MissingInteger.cs: https://codility.com/programmers/task/missing_integer 21 | 1. MaxCounters.cs: https://codility.com/programmers/task/max_counters 22 | 23 | **Prefix Sums** 24 | 25 | 1. PassingCars.cs: https://codility.com/programmers/task/passing_cars 26 | 1. CountDiv --> beware of this as it's only applying a math formula 27 | 28 | **Sorting** 29 | 30 | 1. MaxProductOfThree.cs: https://codility.com/programmers/task/max_product_of_three/ 31 | 1. Distinct.cs: https://codility.com/programmers/task/distinct/ 32 | 1. Triangle.cs: https://codility.com/programmers/task/triangle 33 | 1. NumberOfDiscIntersections.cs: https://codility.com/programmers/task/number_of_disc_intersections 34 | 35 | **Stacks and Queues** 36 | 37 | 1. Fish.cs: https://codility.com/programmers/task/fish 38 | 39 | **Leader** 40 | 41 | 1. EquiLeader.cs: https://codility.com/programmers/task/equi_leader 42 | 1. Dominator.cs: Finding the leader in O(N) 43 | 44 | **Maximum Slice Problem** 45 | 46 | 1. MaxDoubleSliceSum.cs: https://codility.com/programmers/task/max_double_slice_sum/ 47 | 48 | **Prime and Composite Numbers** 49 | 50 | 1. MinPerimeterRectangle.cs: https://codility.com/programmers/task/min_perimeter_rectangle/ 51 | 1. Flags.cs: https://codility.com/programmers/task/flags 52 | 1. Peaks.cs: https://codility.com/programmers/task/peaks/ 53 | 54 | **Sieve of Eratosthenes** 55 | 56 | 1. CountSemiprimes.cs: https://codility.com/programmers/task/count_semiprimes 57 | 58 | **Fibonnacci numbers** 59 | 60 | 1. Ladder.cs (and a wrong version): https://codility.com/programmers/task/ladder 61 | 62 | **Binary Search Algorithm** 63 | 64 | 1. MinMaxDivision.cs: https://codility.com/programmers/task/min_max_division/ 65 | 66 | **Greedy Algorithm** 67 | 68 | 1. TieRopes.cs: https://codility.com/programmers/task/tie_ropes/ 69 | 70 | **Challenges/Future Training** 71 | 72 | 1. BreakTheRope.cs: https://codility.com/programmers/task/break_the_rope 73 | 1. Equi.cs: https://codility.com/programmers/task/equi 74 | 1. BinaryGap.cs: https://codility.com/programmers/task/binary_gap/ 75 | 1. StrSymmetryPoint.cs: https://codility.com/programmers/task/str_symmetry_point/ 76 | 77 | --------------------------------------------------------------------------------