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
| public class InversionCounter | |
| { | |
| List<int> array; | |
| public InversionCounter (IEnumerable<int> arr) | |
| { | |
| array = arr.ToList(); | |
| } | |
| private Tuple<int, List<int>> Merge(List<int> leftList, List<int> rightList) |
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
| public static IList<T> ReservoirSampler<T>(IEnumerable<T> arr, int k) | |
| { | |
| if(arr == null) | |
| { | |
| throw new ArgumentNullException(); | |
| } | |
| if( k <= 1 || k > arr.Count()) | |
| { | |
| throw new ArgumentOutOfRangeException(); | |
| } |
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
| public static IList<int> SieveOfEratosthenes(int n) | |
| { | |
| if(n < 2) | |
| { | |
| throw new ArgumentOutOfRangeException(); | |
| } | |
| var arr = Enumerable.Repeat(true, n+1).ToArray(); | |
| for (int i = 2; i <= n; i++) |
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
| public static int PowerRemainder(int n, int divisor) | |
| { | |
| while((n%divisor) == 0) | |
| { | |
| n /= divisor; | |
| } | |
| return n; | |
| } | |
| public static int UglyNumberNth(int n) |
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
| public static int FindMissingNumber(IEnumerable<int> numbers) | |
| { | |
| int expectedSum = (numbers.Count() + 1)*(numbers.Count()+2)/2; | |
| int realSum = 0; | |
| foreach (var number in numbers) | |
| { | |
| realSum += number; | |
| } |
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
| public class Tree<K, V> | |
| where K : class, IComparable<K> | |
| where V : class | |
| { | |
| private Node<K, V> root; | |
| /// <summary> | |
| /// Searches tree using BFS but the depth of the search | |
| /// is limited | |
| /// </summary> |
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
| public class Tree<K, V> | |
| where K : class, IComparable<K> | |
| where V : class | |
| { | |
| private Node<K, V> root; | |
| /// <summary> | |
| /// Searches tree using BFS but the depth of the search | |
| /// is limited. Level of root is 0. | |
| /// </summary> |
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
| public class Tree<K, V> | |
| where K : class, IComparable<K> | |
| where V : class | |
| { | |
| private Node<K, V> root; | |
| public V BFS(K key) | |
| { | |
| Queue<Node<K, V>> queue = new Queue<Node<K, V>>(); | |
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
| public class Tree<K, V> | |
| where K : class, IComparable<K> | |
| where V : class | |
| { | |
| private Node<K, V> root; | |
| public V DFS(K key) | |
| { | |
| Stack<Node<K, V>> stack = new Stack<Node<K, V>>(); | |
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
| public static int PowerBySquaring(int baseNumber, int exponent) | |
| { | |
| int result = 1; | |
| while (exponent != 0) | |
| { | |
| if ((exponent & 1)==1) | |
| { | |
| result *= baseNumber; | |
| } | |
| exponent >>= 1; |