static void Main(string[] args) { // Node n = new Node(50); //n.left = new Node(30); //n.right = new Node(70); //n.left.left = new Node(20); //n.left.right = new Node(40); // n.right.left = new Node(60); // n.right.right = new Node(80); // Problem2 p = new Problem2(); // Sum s = new Sum(); // p.ModifyBST(n,s); // p.Inorder(n); //DutchNationalFlagProblem d = new DutchNationalFlagProblem(); //int[] arr = { 1, 0, 0, 1, 2, 1, 1, 2, 1 }; // d.Sort(arr); //for (int i = 0; i < arr.Length - 1; i++) //{ // Console.WriteLine(arr[i]); //} //Console.ReadKey(); //int[] arr = { 1, 4, 45, // 6, 10, 8 }; //Problem6 p = new Problem6(); //p.GetPair(arr, 16); // int NA = -1; // int[] arr1 = { 1, 3, 4, 5,NA, // NA, NA, NA }; // int[] arr2 = { 2, 4, 6, 8 }; // Problem8 p = new Problem8(); //int[]arr= p.MergeSortedArrays(arr1, arr2); //Problem11 p1 = new Problem11(); //int[] arr2 = { -2, -4, -6, -8 }; //p1.LessThan(arr2, -5); //NodeLL n = new NodeLL(3); //n.next = new NodeLL(1); //n.next.next = new NodeLL(5); //n.next.next.next = new NodeLL(9); //n.next.next.next.next = new NodeLL(7); //n.next.next.next.next.next = new NodeLL(2); //n.next.next.next.next.next.next = new NodeLL(1); //NodeLL n1 = new NodeLL(4); //n1.next = new NodeLL(6); //n1.next.next = new NodeLL(7); //n1.next.next.next = new NodeLL(2); //n1.next.next.next.next = new NodeLL(1); //PointofIntersection p = new PointofIntersection(); //p.GetDifferece(n, n1); //NodeLL n = new NodeLL(12); //n.next = new NodeLL(11); //n.next.next = new NodeLL(12); //n.next.next.next = new NodeLL(21); //n.next.next.next.next = new NodeLL(41); //n.next.next.next.next.next = new NodeLL(41); //RemoveDuplicates d = new RemoveDuplicates(); //d.Remove(n); //DetectCycle d = new DetectCycle(); //NodeLL n1 = d.GetMiddle(n); //QueueUsingStack q = new QueueUsingStack(); //q.Queueusingstack(n); //FindMissingElements f = new FindMissingElements(); // int [] arr1 = { 1, 2, 3, 4, 5, 10 }; //int[] arr2 = { 2, 3, 1, 0, 5 }; //List<int> res=f.FindMissingElement(arr1, arr2); //string str1 = "babad"; //LongestPalindromeSubstring p = new LongestPalindromeSubstring(); //p.LongestPalindrome(str1); //string str = "paypalishiring"; //printZig p = new printZig(); //p.myAtoi("1234"); //int[] arr = { 7, 3, 2, 4, 9, 12, 56 }; //ChocolateDistributionProblem c = new ChocolateDistributionProblem(); //int res=c.DistributionProblem(arr, 3); //int[] arr = { 1, 0, 2 }; //Problem1 p = new Problem1(); //int res=p.GetRatings(arr); //Problem5 p = new Problem5(); //int []arr= { 100, 180, 260, 310, 40, 535, 695 }; //p.stockBuySell(arr, arr.Length); //SumofPairs p = new SumofPairs(); //int[] arr = {1, 4, 45, // 6, 10, 8}; //p.Pairs(arr, 16); //Problem13 p = new Problem13(); //string start = "TOON"; //string target = "PLEA"; //string[] dic = { "POON", "PLEE", "SAME", "POIE", "PLEA", "PLIE", "POIN" }; //p.ShortesChain(start, target, dic); //Node n = new Node(1); //n.left = new Node(2); //n.right = new Node(3); //n.left.left = new Node(4); //n.left.right = new Node(5); //Problem17 p = new Problem17(); //p.BFS(n); int[] arr = { 6, 7, 1, 2, 3, 4, 5 }; Problem20 p = new Problem20(); int res = p.FindPivot(arr); }
static void Main(string[] args) { // Problem 1 int[] arr = new int[4] { 10, 15, 3, 7 }; int k = 17; var result = new Problem1().problem1(arr, k); System.Console.WriteLine("Problem 1: " + result); // Expected output 10,7 or 7, 10 Debug.Assert(Tuple.Equals(result, Tuple.Create(10, 7)) || Tuple.Equals(result, Tuple.Create(7, 10))); // Problem 2 arr = new int[3] { 3, 2, 1 }; System.Console.Write("Problem 2: "); int[] res = new Problem2().problem2(arr); res.ToList().ForEach(x => System.Console.Write((x) + " ")); Debug.Assert(res.SequenceEqual(new int[] { 2, 3, 6 })); System.Console.Write("\nProblem 2_1: "); res = new Problem2().problem2_1(arr); res.ToList().ForEach(x => System.Console.Write((x) + " ")); // Expected output == [2, 3, 6] Debug.Assert(res.SequenceEqual(new int[] { 2, 3, 6 })); // Problem 3 var root = new Problem3().populateNode(); var nodes = new Problem3().serialize(root); System.Console.Write("\nProblem 3: "); System.Console.WriteLine(nodes); // Expected output == left.left Debug.Assert(new Problem3().deserialize(nodes).Left.Left.Val == "left.left"); // Problem 4 arr = new int[] { 3, 4, -1, 1 }; System.Console.Write("Problem 4: "); int res4 = new Problem4().problem4(arr); System.Console.WriteLine(res4); // Expected output == 2 Debug.Assert(res4 == 2); // Problem 6 var prob6 = new Problem6().problem6(); System.Console.WriteLine($"Problem 6: {prob6}"); Debug.Assert(prob6 == 2); // Problem 7 var prob7 = new Problem7().problem7("111"); System.Console.WriteLine($"Problem 7: {prob7}"); // Problem simple ecryption var encr = new SimpleEncryptedString().encrypt("This is a test!", 1); System.Console.WriteLine($"Problem encrypted string: {encr}"); Debug.Assert(encr == "hsi etTi sats!"); var decrypt = new SimpleEncryptedString().decrypt(encr, 1); System.Console.WriteLine($"Problem decrypted string: {decrypt}"); Debug.Assert(decrypt == "This is a test!"); encr = new SimpleEncryptedString().encrypt("This is a test!", 2); System.Console.WriteLine($"Problem encrypted string: {encr}"); Debug.Assert(encr == "s eT ashi tist!"); decrypt = new SimpleEncryptedString().decrypt(encr, 2); System.Console.WriteLine($"Problem decrypted string: {decrypt}"); Debug.Assert(decrypt == "This is a test!"); // Sort odd in-place var arrVal = new int[6] { 5, 3, 2, 8, 1, 4 }; var output = new Kata().SortArray(arrVal); System.Console.Write("Sort Odd Values in Array: "); output.ToList().ForEach(x => System.Console.Write((x) + " ")); Debug.Assert(output.SequenceEqual(new int[] { 1, 3, 2, 8, 5, 4 })); // Unival tree var prob8 = new Problem8(); prob8.hydrateTree(); int unival = prob8.problem8(); System.Console.WriteLine($"\nProblem 8: {unival}"); Debug.Assert(unival == 5, "Unival count should be 5"); // Find all anagrams in a string var anagram = new AllAnagramsInString(); IList <int> expectedOutput = anagram.FindAnagrams("cbaebabacd", "abc"); System.Console.Write("Problem: All anagrams index in String: "); expectedOutput.ToList().ForEach(x => System.Console.Write((x) + " ")); Debug.Assert(expectedOutput.SequenceEqual(new int[2] { 0, 6 })); expectedOutput = anagram.FindAnagrams("abab", "ab"); System.Console.Write("\nProblem: All anagrams index in String: "); expectedOutput.ToList().ForEach(x => System.Console.Write((x) + " ")); Debug.Assert(expectedOutput.SequenceEqual(new int[3] { 0, 1, 2 })); // Problem 9 int prob9 = new Problem9().problem9(new int[5] { 2, 4, 6, 2, 5 }); System.Console.WriteLine($"\nProblem 9: {prob9}"); Debug.Assert(prob9 == 13); prob9 = new Problem9().problem9(new int[4] { 5, 1, 1, 5 }); System.Console.WriteLine($"Problem 9: {prob9}"); Debug.Assert(prob9 == 10); // Problem 11 var problem11 = new Problem11(); problem11.Add("dog"); problem11.Add("deer"); problem11.Add("deal"); var results = problem11.Search("de"); System.Console.Write("Problem 11: "); results.ToList().ForEach(x => System.Console.Write(x + " ")); Debug.Assert(results.SequenceEqual(new string[] { "deer", "deal" })); System.Console.Write($"\nProblem 11 - Delete key `deal`: {problem11.deleteNode("deal")}"); System.Console.Write("\nProblem 11- Search deleted key(`deal`): "); problem11.Search("deal").ToList().ForEach(_ => System.Console.Write(_)); // Problem 12 var problem12 = new Problem12(); var output12 = problem12.problem12(5, new int[] { 1, 3, 5 }); System.Console.Write($"\nProblem 12: {output12}"); Debug.Assert(output12 == 5); // Problem 13 var prob13 = new Problem13(); string output13 = prob13.problem13(text: "abcba", k: 2); System.Console.WriteLine($"\nProblem 13: {output13}"); Debug.Assert(output13 == "bcb"); output13 = prob13.problem13("aabbcc", 2); System.Console.Write($"Problem 13: {output13}"); Debug.Assert(output13 == "aabb" || output13 == "bbcc"); output13 = prob13.problem13Another("aabbcc", 3); System.Console.Write($"\nProblem 13: {output13}"); Debug.Assert(output13 == "aabbcc"); output13 = prob13.problem13("aabbcc", 4); System.Console.Write($"\nProblem 13: {output13}"); Debug.Assert(output13 == String.Empty); // Problem Delete Multiple Items var delItem = new DeleteMultipleItems(); int[] actual = delItem.DeleteNth(new int[] { 1, 1, 3, 3, 7, 2, 2, 2, 2 }, 3); var expected = new int[] { 1, 1, 3, 3, 7, 2, 2, 2 }; Debug.Assert(expected.SequenceEqual(actual)); // Problem 15 var prob15 = new Problem15(); IEnumerable <int> prob15Output = prob15.problem15(10); System.Console.Write("\nProblem 15: "); prob15Output.ToList().ForEach(_ => System.Console.Write(_ + " ")); // Problem 16 // input = 100 var prob16 = new Problem16(); var prob16Expected = prob16.problem16(); System.Console.WriteLine($"\nProblem 16: {prob16Expected}"); Debug.Assert(prob16Expected == 95); // Problem 17 var prob17 = new Problem17(); int prob17Output = prob17.problem17("dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"); System.Console.WriteLine($"Problem 17: {prob17Output}"); Debug.Assert(prob17Output == 20); prob17Output = prob17.problem17("dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"); System.Console.WriteLine($"Problem 17: {prob17Output}"); Debug.Assert(prob17Output == 32); // Problem 18 var prob18 = new Problem18(); IEnumerable <int> prob18Output = prob18.problem18(array: new int[] { 10, 5, 2, 7, 8, 7 }, k: 3); System.Console.Write("Problem 18: "); prob18Output.ToList().ForEach(_ => System.Console.Write(_ + " ")); prob18Output = prob18.problem18Optimized(array: new int[] { 11, 12, 13, 12, 14, 11, 10, 9 }, k: 3); System.Console.Write("\nProblem 18: "); prob18Output.ToList().ForEach(_ => System.Console.Write(_ + " ")); // Problem 22 var prob22 = new Problem22(); var text = "thequickbrownfox"; var dic = new string[] { "quick", "brown", "the", "fox" }; var prob22Output = prob22.problem22(text, dic); System.Console.Write("\nProblem 22: "); prob22Output.ToList().ForEach(_ => System.Console.Write(_ + " ")); Debug.Assert(prob22Output.SequenceEqual(new string[] { "the", "quick", "brown", "fox" })); var prob22Other = prob22.prob22Another("bedbathandbeyond", new string[] { "bed", "bath", "bedbath", "and", "beyond" }); System.Console.Write("\nProblem 22: "); prob22Other.ToList().ForEach(_ => System.Console.Write(_ + " ")); // Problem 26 var prob26 = new Problem26(); var prob26Output = prob26.problem26(2); System.Console.WriteLine($"\nProblem 26: {prob26Output}"); // Problem min window var minWindow = new MinWindowProblem(); IEnumerable <String> minOutput = minWindow.minWindowOther("this is a test string", "tist"); System.Console.Write("Min Window: "); minOutput.ToList().ForEach(_ => System.Console.WriteLine(_)); // Debug.Assert(minOutput == "t stri"); minOutput = minWindow.minWindowOther("a", "b"); System.Console.Write("\nMin Window: "); minOutput.ToList().ForEach(_ => System.Console.WriteLine(_)); // Debug.Assert(minOutput == ""); // Remove duplicates recursively var remDups = new RemoveDuplicates(); string dupOutput = remDups.removeDuplicates("careermonk"); System.Console.WriteLine($"\nRemove duplicates: {dupOutput}"); Debug.Assert(dupOutput == "camonk"); dupOutput = remDups.removeDuplicates("geeksforgeeg"); System.Console.WriteLine($"Remove duplicates: {dupOutput}"); Debug.Assert(dupOutput == "gksfor"); // word boggle char[,] matrix = { { 'o', 'a', 'a', 'n' }, { 'e', 't', 'a', 'e' }, { 'i', 'h', 'k', 'r' }, { 'i', 'f', 'l', 'v' } }; string[] words = { "oath", "pea", "eat", "rain" }; System.Console.Write("Word boggle: "); var boggle = new Boggle().boggle(matrix, words); System.Console.WriteLine(string.Join(", ", boggle)); Debug.Assert(boggle.OrderBy(x => x).ToList().SequenceEqual(new string[] { "eat", "oath" })); // Candy problem var candy = new Candy().candy(new int[] { 1, 0, 2 }); var candyOutput = 5; System.Console.WriteLine($"Minimum Candies: {candy}"); Debug.Assert(candy == candyOutput); candy = new Candy().candy(new int[] { 1, 2, 2 }); candyOutput = 4; System.Console.WriteLine($"Minimum Candies: {candy}"); Debug.Assert(candy == candyOutput); // Lowest Common Ancestor - BST var lcaBst = new LowestCommonAncestorBST(); var lcabstOutput = lcaBst.lca(); System.Console.WriteLine($"LCA BST: {lcabstOutput}"); Debug.Assert(lcabstOutput == 12); // Lowest Common Ancestor - BT var lcaBt = new LowestCommonAncestorBT(); var lcabtOutput = lcaBt.lca(); System.Console.WriteLine($"LCA BT: {lcabtOutput}"); Debug.Assert(lcabtOutput == 2); // Topology Sort var topo = new TopologySort(); var topoOutput = topo.topologySort(); System.Console.WriteLine($"Topology sort: {string.Join(',', topoOutput.ToList())}"); Debug.Assert(topoOutput.SequenceEqual(new int[] { 5, 4, 2, 3, 1, 0 })); // LRU var lru = new LRU(4); lru.put(1, 1); lru.put(10, 15); lru.put(15, 10); lru.put(10, 16); System.Console.WriteLine($"LRU cache size: {lru.getCacheSize()}"); // 3 System.Console.WriteLine($"Get LRU key-10: {lru.get(10)}"); // 16 lru.put(12, 15); lru.put(18, 10); System.Console.WriteLine($"LRU cache size: {lru.getCacheSize()}"); // 4 lru.put(13, 16); System.Console.WriteLine($"LRU cache size: {lru.getCacheSize()}"); // 4 System.Console.WriteLine($"Get LRU key-1: {lru.get(1)}"); // -1 System.Console.WriteLine($"Get LRU key-15: {lru.get(15)}"); // -1 System.Console.WriteLine($"Get LRU key-18: {lru.get(18)}"); // 10 System.Console.WriteLine($"Get LRU key-13: {lru.get(13)}"); // 16 // Number Of Islands int[,] islands = { { 0, 1 }, { 1, 0 }, { 1, 1 }, { 1, 0 } }; int connected = new NumberOfIslands().numerOfIslands(islands); System.Console.WriteLine($"Number of Connected islands is: {connected}"); Debug.Assert(connected == 1); islands = new int[, ] { { 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 1, 0, 1, 0 } }; connected = new NumberOfIslands().numerOfIslands(islands); System.Console.WriteLine($"Number of Connected islands is: {connected}"); Debug.Assert(connected == 2); // Minimum number of swaps int[] array = { 4, 3, 2, 1 }; int minswaps = new MinimumSwapsToSort().minimumSwaps(array); System.Console.WriteLine($"Minimum swap for array -> {string.Join(",", array)} : {minswaps}"); Debug.Assert(minswaps == 2); array = new int[] { 1, 5, 4, 3, 2 }; minswaps = new MinimumSwapsToSort().minimumSwaps(array); System.Console.WriteLine($"Minimum swap for array -> {string.Join(",", array)} : {minswaps}"); Debug.Assert(minswaps == 2); // Snakes and Ladder // int[] positions = {3, 22, 5, 8, 11, 26, 20, 29, // 17, 4, 19, 7, 27, 1, 21, 9}; (int rows, int cols) = (5, 6); int steps = rows * cols; int[] moves = new int[steps]; Enumerable.Range(0, steps).ToList().ForEach(x => { moves[x] = -1; }); // Ladders moves[2] = 21; moves[4] = 7; moves[10] = 25; moves[19] = 28; // Snakes moves[26] = 0; moves[20] = 8; moves[16] = 3; moves[18] = 6; int minDistance = new SnakesNLadders().snakesnLadder(moves, steps); System.Console.WriteLine($"Snakes n Ladder: Minimum dice roll needed: {minDistance}"); Debug.Assert(minDistance == 3); // Alternating array var altArray = new int[] { 1, 2, 3, 4, 5, 6 }; System.Console.Write($"Alternating array output for array - {String.Join(", ", altArray)}"); new AlternateArray().alternateArray(altArray); System.Console.WriteLine(" is: " + String.Join(", ", altArray)); // Minimum platforms int[] arrivals = new int[] { 0900, 0940, 0950, 1100, 1500, 1800 }; int[] departures = new int[] { 0910, 1200, 1120, 1130, 1900, 2000 }; int minPlatforms = new MinimumPlatforms().findPlatforms(arrivals, departures, arrivals.Length); System.Console.WriteLine($"Mininum platforms needed: {minPlatforms}"); Debug.Assert(minPlatforms == 3); arrivals = new int[] { 0900, 1100, 1235 }; departures = new int[] { 1000, 1200, 1240 }; minPlatforms = new MinimumPlatforms().findPlatforms(arrivals, departures, arrivals.Length); System.Console.WriteLine($"Mininum platforms needed: {minPlatforms}"); Debug.Assert(minPlatforms == 1); // Reverse array in groups of size K int[] revArray = new int[] { 1, 2, 3, 4, 5 }; k = 3; System.Console.WriteLine($"Reverse array: {String.Join(", ", revArray)} in group of \"{k}\""); new ReverseArrays().reverArrays(revArray, k); System.Console.WriteLine($"Reversed array: {String.Join(", ", revArray)}"); // Cycle in undirected graph var hasCycle = new CyclesInUndirectedGraph(5).DetectCycle(); System.Console.WriteLine($"Is the Graph cyclic: {hasCycle}"); Debug.Assert(hasCycle == true); // Cycle in directed graph var dgCycle = new CycleInDirectedGraph(4).detectCycle(); System.Console.WriteLine($"Is the Graph cyclic: {dgCycle}"); Debug.Assert(dgCycle == false); // Typeahead System.Console.WriteLine("\nTypeahead:"); System.Console.WriteLine("=========="); var typeAhead = new Typeahead(); words = new string[] { "table", "tablet", "mobile", "mob", "mobbed" }; var ranks = new int[] { 9, 4, 8, 3, 1 }; foreach (var(word, rank) in Enumerable.Zip(words, ranks)) { typeAhead.AddNode(word, rank); } typeAhead.BuildMostSearchWords(wordLimit: 3); System.Console.Write("Most searched words by Rank:"); typeAhead.GetMostSearchedWordsByRank().ToList().ForEach(_ => System.Console.Write(_.ToString() + ", ")); System.Console.WriteLine("\nStrings starting with mob: " + string.Join(", ", typeAhead.SearchAllChildren("mob"))); System.Console.WriteLine("Strings starting with tab: " + string.Join(", ", typeAhead.PrefixSearch("tab"))); System.Console.WriteLine("Strings starting with let: " + string.Join(", ", typeAhead.SearchAllChildren("let"))); // Problem 20 var prob20 = new Problem20().problem20(new int[] { 3, 7, 8, 10 }, new int[] { 99, 1, 8, 101 }); System.Console.WriteLine($"Problem 20: {prob20}"); Debug.Assert(prob20 == 8); // Problem 21 int prob21 = new Problem21(new int[] { 30, 75, 0, 50, 60, 150 }).problem21(); System.Console.WriteLine($"Problem 21: {prob21}, for sequence - [30, 75, 0, 50, 60, 150]"); Debug.Assert(prob21 == 2); prob21 = new Problem21(new int[] { 30, 75, 0, 50, 60, 150, 25, 45 }).problem21(); System.Console.WriteLine($"Problem 21: {prob21}, for sequence - [30, 75, 0, 50, 60, 150, 25, 45]"); Debug.Assert(prob21 == 3); // Problem 28 string[] sequence = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" }; IEnumerable <string> prob28 = new Problem28().problem28(sequence, k: 16); System.Console.WriteLine($"Problem 28: for sequence - [{string.Join(", ", sequence)}] - {string.Join(", ", prob28)}"); sequence = new string[] { "This", "is", "an", "example", "of", "text", "justification" }; prob28 = new Problem28().problem28(sequence, k: 16); System.Console.WriteLine($"Problem 28: for sequence - [{string.Join(", ", sequence)}] - {string.Join(", ", prob28)}"); // Make maximum sum arr = new int[] { 1, 1, 3 }; int probMaxSum = new RemoveMinSum().removeMinSum(arr); System.Console.WriteLine($"Remove Min Sum for sequence - [{string.Join(", ", arr)}] is: {probMaxSum}"); Debug.Assert(probMaxSum == 3); arr = new int[] { 1, 1, 3, 3, 2, 2, 1, 1, 1 }; probMaxSum = new RemoveMinSum().removeMinSum(arr); System.Console.WriteLine($"Remove Min Sum for sequence - [{string.Join(", ", arr)}] is: {probMaxSum}"); Debug.Assert(probMaxSum == 11); // string permutation text = "abca"; IEnumerable <string> stringPerm = new StringPermutation().permute(text, ""); System.Console.WriteLine($"String permutation - {string.Join(", ", stringPerm)}"); // linked list grouping var llGroup = new GroupLinkedListsByIntersectionPoint().getLinkedListsByGroup().ToList(); System.Console.WriteLine("Group LinkedList by intersection nodes:"); for (int i = 0; i < llGroup.Count; i++) { System.Console.WriteLine($"Group {i + 1}: [{string.Join(", ", llGroup[i])}]"); } // minimum repetitive window arr = new int[] { 1, 2, 1, 2, 3 }; int minimumRepetitionWindow = new MinimumRepetitionWindow().minimumRepetitionWindow(arr); System.Console.WriteLine($"Minimum repetitive window for sequence - [{string.Join(", ", arr)}] is: {minimumRepetitionWindow}"); Debug.Assert(minimumRepetitionWindow == 3); arr = new int[] { 1, 2, 1, 2, 3, 2, 1 }; minimumRepetitionWindow = new MinimumRepetitionWindow().minimumRepetitionWindow(arr); System.Console.WriteLine($"Minimum repetitive window for sequence - [{string.Join(", ", arr)}] is: {minimumRepetitionWindow}"); Debug.Assert(minimumRepetitionWindow == 5); // problem 24 var prob24 = new Problem24(); System.Console.WriteLine($"Problem 24: [{string.Join(", ", prob24.problem24())}]"); }