<file_sep>﻿using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question1
    {
        public static int Answer(int[] portfolios)
        {
            var maxValue = 0;
            for (var i = 0; i < portfolios.Length; i++)
            {
                for (var j = 0; j < portfolios.Length; j++)
                {
                    int xor = portfolios[i] ^ portfolios[j];
                    if (xor > maxValue)
                    {
                        maxValue = xor;
                    }
                }
            }

            return maxValue;
        }
    }
}
<file_sep>﻿using System;
using System.Linq;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question5
    {
        public static int Answer(int[] numOfShares, int totalValueOfShares)
        {

            var numOfShares2 = numOfShares.ToList();
            int count = 1;
            while (calSets(totalValueOfShares, numOfShares2.ToArray()) == 0)
            {
                for (int i = 0; i < numOfShares.Length; i++)
                {
                    numOfShares2.Add(numOfShares[i]);
                }

                count++;
            }

            return count;
        }
        
        static int[,] costs;
    
        static int[,] minItems;
    
        public static int calSets(int target, int[] arr) {
            costs = new int[arr.Length, target + 1];
            minItems = new int[arr.Length, target + 1];
            for (int j = 0; (j <= target); j++) {
                if ((arr[0] <= j)) {
                    costs[0, j] = arr[0];
                    minItems[0, j] = 1;
                }
            
            }
        
            for (int i = 1; (i < arr.Length); i++) {
                for (int j = 0; (j <= target); j++) {
                    costs[i, j] = costs[(i - 1), j];
                    minItems[i, j] = minItems[(i - 1), j];
                    if ((arr[i] <= j)) {
                        costs[i, j] = Math.Max(costs[i, j], (costs[(i - 1), (j - arr[i])] + arr[i]));
                        if ((costs[(i - 1), j] 
                             == (costs[(i - 1), (j - arr[i])] + arr[i]))) {
                            minItems[i, j] = Math.Min(minItems[i, j], (minItems[(i - 1), (j - arr[i])] + 1));
                        }
                        else if ((costs[(i - 1), j] 
                                  < (costs[(i - 1), (j - arr[i])] + arr[i]))) {
                            minItems[i, j] = (minItems[(i - 1), (j - arr[i])] + 1);
                        }
                    
                    }
                
                }
            
            }
        
            if ((costs[(arr.Length - 1), target] == target))
            {
                return minItems[(arr.Length - 1), target];
            }
                return 0;
        
        }
    }
}<file_sep>﻿using System.Collections.Generic;
using System.Linq;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question4
    {
        public static int Answer(string[,] machineToBeFixed, int numOfConsecutiveMachines)
        {
            int dim = machineToBeFixed.GetLength(0);
            int max = 999;
            for (int i = 0; i < dim; i++)
            {
                var row = SliceRow(machineToBeFixed, i);
                if (dim - row.Count(x => x == "X") == numOfConsecutiveMachines)
                {
                    List<string> rowString = row.ToList();
                    rowString.RemoveAll(x => x == "X");
                    List<int> rowInt = rowString.Select(int.Parse).ToList();
                    if (rowInt.Sum() < max)
                    {
                        max = rowInt.Sum();
                    }
                }
            }

            if (max == 999) max = 0;
            return max;
        }
        
        public static IEnumerable<T> SliceRow<T>(T[,] array, int row)
        {
            for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
            {
                yield return array[row, i];
            }
        }
    }
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using C_Sharp_Challenge_Skeleton.Beans;
using QuickGraph;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question3
    {
        public static int Answer(int numOfNodes, Edge[] edgeLists)
                {
                    List<AdjacencyGraph<int, Edge<int>>> graphs = new List<AdjacencyGraph<int, Edge<int>>>();
                    List<List<int>> possibleSolutions = new List<List<int>>();

                    List<int> nodes = Enumerable.Range(1, numOfNodes).ToList();
                    
                    int comboCount = (int) Math.Pow(2, nodes.Count) - 1;
                    List<List<int>> combos = new List<List<int>>();
                    for (int i = 1; i < comboCount + 1; i++)
                    {
                        combos.Add(new List<int>());
                        for (int j = 0; j < nodes.Count; j++)
                        {
                            if ((i >> j) % 2 != 0)
                                combos.Last().Add(nodes[j]);
                        }
                    }
                    
                    
                    for (var index = 0; index < combos.Count; index++)
                    {
                        var combo = combos[index];
                        AdjacencyGraph<int, Edge<int>> graph = new AdjacencyGraph<int, Edge<int>>();
                        for (var index1 = 0; index1 < combo.Count; index1++)
                        {
                            var i = combo[index1];
                            graph.AddVertex(i);
                        }

                        bool dontAdd = false;
                        for (var i = 0; i < edgeLists.Length; i++)
                        {
                            var e = edgeLists[i];
                            if (graph.ContainsVertex(e.EdgeA) && graph.ContainsVertex(e.EdgeB))
                            {
                                dontAdd = true;
                            }
                        }

                        if (!dontAdd)
                        {
                            graphs.Add(graph);
                        }
                        
                    }

                    AdjacencyGraph<int, Edge<int>> selectedGraph = new AdjacencyGraph<int, Edge<int>>();
                    foreach (var graph in graphs.ToList())
                    {
                        if (graph.Edges.Any() || graph.VertexCount == 1)
                        {
                            graphs.Remove(graph);
                        }
                        else
                        {
                            if (graph.VertexCount > selectedGraph.VertexCount) selectedGraph = graph;
                        }
                    }

                    int X = selectedGraph.VertexCount;
                    int Y = numOfNodes - X;
                    
                    return X - Y;
                }
    }
}<file_sep>using System;
using System.Collections.Generic;
using System.Linq;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question2
    {
        public static int Answer(int[] cashflowIn, int[] cashflowOut)
        {
            List<IEnumerable<int>> inSets = Subsets(cashflowIn.ToList()).Where(x => x.Count() != 0).ToList();
            List<IEnumerable<int>> outSets = Subsets(cashflowOut.ToList()).Where(x => x.Count() != 0).ToList();
            
            int minDiff = Math.Abs(inSets.ElementAt(0).Sum() - outSets.ElementAt(0).Sum());
            
            foreach (var set in inSets)
            {
                foreach (var set2 in outSets)
                {
                    if (Math.Abs(set.Sum() - set2.Sum()) < minDiff)
                    {
                        minDiff = Math.Abs(set.Sum() - set2.Sum());
                        
                    }
                }
            }

            return minDiff;
        }
        
        public static IEnumerable<IEnumerable<T>> Subsets<T>(IEnumerable<T> source)
        {
            List<T> list = source.ToList();
            int length = list.Count;
            int max = (int)Math.Pow(2, list.Count);

            for (int count = 0; count < max; count++)
            {
                List<T> subset = new List<T>();
                uint rs = 0;
                while (rs < length)
                {
                    if ((count & (1u << (int)rs)) > 0)
                    {
                        subset.Add(list[(int)rs]);
                    }
                    rs++;
                }
                yield return subset;
            }
        }
    }
}
<file_sep>using System;
using System.Collections.Generic;
using System.Linq;
using QuickGraph;
using QuickGraph.Algorithms;

namespace C_Sharp_Challenge_Skeleton.Answers
{
    public class Question6
    {
        static AdjacencyGraph<int, Edge<int>> graph = new AdjacencyGraph<int, Edge<int>>();
        static Dictionary<Edge<int>, double> costs = new Dictionary<Edge<int>, double>();
        
        public static int Answer(int numOfServers, int targetServer, int[,] connectionTimeMatrix)
        {
            for (int i = 0; i < connectionTimeMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < numOfServers; j++)
                {
                    var edge = new Edge<int>(i, j);
                    graph.AddVerticesAndEdge(edge);
                    costs.Add(edge, connectionTimeMatrix[i,j]);
                }
            }
            
            var edgeCost = AlgorithmExtensions.GetIndexer(costs);
            var tryGetPath = graph.ShortestPathsDijkstra(edgeCost, 0);

            IEnumerable<Edge<int>> path;
            tryGetPath(targetServer, out path);

            double cost = 0;
            foreach (var edge in path)
            {
                cost += costs[edge];
            }

            return Convert.ToInt32(cost);
        }
    }
}
