int[] metroCard(int lastNumberOfDays) { return lastNumberOfDays == 28 || lastNumberOfDays == 30 ? new [] { 31 } : new [] { 28, 30, 31 }; }
Previous Next
int[] metroCard(int lastNumberOfDays) { return lastNumberOfDays == 28 || lastNumberOfDays == 30 ? new [] { 31 } : new [] { 28, 30, 31 }; }
bool willYou(bool young, bool beautiful, bool loved) { return (young && beautiful && !loved) || ((!young || !beautiful) && loved); }
bool tennisSet(int score1, int score2) { return (Math.Min(score1, score2) < 5 && Math.Max(score1, score2) == 6) || ((Math.Min(score1, score2) == 5 || Math.Min(score1, score2) == 6) && Math.Max(score1, score2) == 7); }
bool arithmeticExpression(int A, int B, int C) { return A + B == C || A - B == C || A * B == C || A / B == C && A % B == 0; }
bool isInfiniteProcess(int a, int b) { return a > b || Math.Abs(a - b) % 2 == 1; }
int extraNumber(int a, int b, int c) { return (a == b) ? c : (a == c) ? b : a; }
int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { if (weight1 + weight2 <= maxW) return value1 + value2; else if (weight1 > maxW && weight2 > maxW) return 0; else if (weight1 > maxW) return value2; else if (weight2 > maxW) return value1; else return value1 > value2 ? value1 : value2; }
bool reachNextLevel(int experience, int threshold, int reward) { return experience + reward >= threshold; }
int phoneCall(int min1, int min2_10, int min11, int s) { var min = 0; while (s > 0) { if (min == 0) { if (s >= min1) { s -= min1; min = 1; } else s = 0; } else if (min == 1) { if (s >= min2_10 * 9) { min = 10; s -= min2_10 * 9; } else if (s < min2_10) { min = 2; s = 0; } else { min += s / min2_10; s = 0; } } else if (min == 10) { if (s < min11) min++; else min += s / min11; s = 0; } } return min; }
int lateRide(int n) { return (n / 60) / 10 + (n / 60) % 10 + (n % 60) / 10 + (n % 60) % 10; }
int circleOfNumbers(int n, int firstNumber) { return (firstNumber + n / 2) % n; }
int maxMultiple(int divisor, int bound) { return (bound / divisor) * divisor; }
int seatsInTheater(int nCols, int nRows, int col, int row) { return (nCols - col + 1) * (nRows - row); }
int candies(int n, int m) { return m / n * n; }
bool sudoku(int[][] grid) { var horizontalLists = new List<List<int>>(); var verticalLists = new List<List<int>>(); var clustersLists = new List<List<int>>(); for (var y = 0; y < 9; y++) verticalLists.Add(grid[y].ToList()); for (var x = 0; x < 9; x++) { var horizontalList = new List<int>(); for (var y = 0; y < 9; y++) horizontalList.Add(grid[y][x]); horizontalLists.Add(horizontalList); } for (var x = 0; x < 9; x += 3) for (var y = 0; y < 9; y += 3) { var clustersList = new List<int>(); clustersList.Add(grid[y][x]); clustersList.Add(grid[y + 1][x]); clustersList.Add(grid[y + 2][x]); clustersList.Add(grid[y][x + 1]); clustersList.Add(grid[y + 1][x + 1]); clustersList.Add(grid[y + 2][x + 1]); clustersList.Add(grid[y][x + 2]); clustersList.Add(grid[y + 1][x + 2]); clustersList.Add(grid[y + 2][x + 2]); clustersLists.Add(clustersList); } return !horizontalLists.Any( horizontalList => horizontalList.Any(i => horizontalList.Count(i1 => i1 == i) != 1)) && !verticalLists.Any( verticalList => verticalList.Any(i => verticalList.Count(i1 => i1 == i) != 1)) && !clustersLists.Any( clustersList => clustersList.Any(i => clustersList.Count(i1 => i1 == i) != 1)); }
int[][] spiralNumbers(int n) { var answer = new int[n][]; for (var i = 0; i < n; i++) answer[i] = new int[n]; var j = 1; var start = 0; var row = start; var col = start; var end = n - 1; while (j <= n * n) { // Horizontal to the right while (true) { if (answer[row][col] == 0) { answer[row][col] = j; j++; } if (col == end) break; col++; } // Vertically down while (true) { if (answer[row][col] == 0) { answer[row][col] = j; j++; } if (row == end) break; row++; } // Horizontal to the left while (true) { if (answer[row][col] == 0) { answer[row][col] = j; j++; } if (col == start) break; col--; } // Vertically up while (true) { if (answer[row][col] == 0) { answer[row][col] = j; j++; } if (row == start) break; row--; } row++; start++; end--; } return answer; }
string messageFromBinaryCode(string code) { var answer = string.Empty; for (var i = 0; i < code.Length / 8; i++) answer += (char) (128 * int.Parse(code[i * 8].ToString()) + 64 * int.Parse(code[i * 8 + 1].ToString()) + 32 * int.Parse(code[i * 8 + 2].ToString()) + 16 * int.Parse(code[i * 8 + 3].ToString()) + 8 * int.Parse(code[i * 8 + 4].ToString()) + 4 * int.Parse(code[i * 8 + 5].ToString()) + 2 * int.Parse(code[i * 8 + 6].ToString()) + int.Parse(code[i * 8 + 7].ToString())); return answer; }
string[] fileNaming(string[] names) { var list = new List<string>(); foreach (var name in names) if (!list.Contains(name)) list.Add(name); else for (var i = 1; i < 16; i++) { if (list.Contains(name + "(" + i + ")")) continue; list.Add(name + "(" + i + ")"); break; } return list.ToArray(); }
int digitsProduct(int product) { switch (product) { case 0: return 10; case 1: return 1; default: var i = 9; var answer = string.Empty; var copyOfProduct = product; while (product != 1 && i != 1) { if (product % i == 0) { product /= i; answer += i.ToString(); } else i--; } if (i == 1 && copyOfProduct == product) return -1; return i == 1 && copyOfProduct == product ? -1 : int.Parse(new string(answer.Reverse().ToArray())); } }
int differentSquares(int[][] matrix) { var two_x_two_s = new HashSet<Tuple<int, int, int, int>>(); for (var row = 0; row < matrix.Length - 1; row++) for (var column = 0; column < matrix[0].Length - 1; column++) two_x_two_s.Add(new Tuple<int, int, int, int>(matrix[row][column], matrix[row][column + 1], matrix[row + 1][column], matrix[row + 1][column + 1])); return two_x_two_s.Count; }
int deleteDigit(int n) { return int.Parse(n.ToString().Select((c, i) => n.ToString().Remove(i, 1)).Max());; }
int chessKnight(string cell) { var possible_moves = new List<string>(); // Upward if (int.Parse(cell[1].ToString()) + 2 <= 8) { if (cell[0] >= 'b') possible_moves.Add((char)(cell[0] - 1) + ((char) (cell[1] + 2)).ToString()); if (cell[0] <= 'g') possible_moves.Add((char)(cell[0] + 1) + ((char)(cell[1] + 2)).ToString()); } // Rightward if (cell[0] <= 'f') { if (int.Parse(cell[1].ToString()) <= 7) possible_moves.Add((char)(cell[0] + 2) + ((char)(cell[1] + 1)).ToString()); if (int.Parse(cell[1].ToString()) >= 2) possible_moves.Add((char)(cell[0] + 2) + ((char)(cell[1] - 1)).ToString()); } // Downward if (int.Parse(cell[1].ToString()) - 2 >= 1) { if (cell[0] >= 'b') possible_moves.Add((char)(cell[0] - 1) + ((char)(cell[1] - 2)).ToString()); if (cell[0] <= 'g') possible_moves.Add((char)(cell[0] + 1) + ((char)(cell[1] - 2)).ToString()); } // Leftward if (cell[0] >= 'c') { if (int.Parse(cell[1].ToString()) <= 7) possible_moves.Add((char)(cell[0] - 2) + ((char)(cell[1] + 1)).ToString()); if (int.Parse(cell[1].ToString()) >= 2) possible_moves.Add((char)(cell[0] - 2) + ((char)(cell[1] - 1)).ToString()); } return possible_moves.Count; }
string lineEncoding(string s) { var c = new char(); var answer = string.Empty; var counter = 0; for (var i = 0; i < s.Length; i++) { if (c == '\0') { c = s[i]; counter++; } else if (c == s[i]) { counter++; if (i == s.Length - 1) answer += counter.ToString() + c; } else { if (counter > 1) answer += counter.ToString() + c; else answer += c; c = s[i]; counter = 1; if (i == s.Length - 1) answer += c; } } return answer; }
bool isMAC48Address(string inputString) { return Regex.IsMatch(inputString, "^([0-9A-F]{2}-){5}[0-9A-F]{2}$"); }
int electionsWinners(int[] votes, int k) { var max = votes.Max(); return k == 0 && votes.Where(d => d == votes.Max()).Count() == 1 ? 1 : votes.Count(t => t + k > max); }
string buildPalindrome(string st) { var i = 0; while (!st.Substring(i).ToArray().SequenceEqual(st.Substring(i).Reverse().ToArray()) && i < st.Length) i++; return st + new string(st.Substring(0, i).Reverse().ToArray()); }
string findEmailDomain(string address) { return Regex.Match(address, "@[A-z0-9\\.-]+$").Value.Substring(1); }
bool isBeautifulString(string inputString) { var sorted = Enumerable.Range('a', inputString.Max(c => c) - 'a' + 1).Select(i => (char)i).ToArray(); for (var i = 1; i < sorted.Length; i++) if (inputString.Count(c => c == sorted[i]) > inputString.Count(c => c == sorted[i - 1])) return false; return true; }
bool bishopAndPawn(string bishop, string pawn) { return Math.Abs(bishop[1] - pawn[1]).Equals(Math.Abs(bishop[0] - pawn[0])); }
int digitDegree(int n) { if (n < 10) return 0; var times = 0; var sum = n.ToString().Select(x => int.Parse(x.ToString())).Sum(); while (sum >= 10 || n >= 10) { times++; n = sum; sum = n.ToString().Select(x => int.Parse(x.ToString())).Sum(); } return times; }
string longestDigitsPrefix(string inputString) { return Regex.Match(inputString, "^\\d+").Value; }
int knapsackLight(int value1, int weight1, int value2, int weight2, int maxW) { if (weight1 + weight2 <= maxW) return value1 + value2; else if (weight1 > maxW && weight2 > maxW) return 0; else if (weight1 <= maxW && weight2 > maxW) return value1; else if (weight2 <= maxW && weight1 > maxW) return value2; else if (value1 >= value2) return value1; else if (value1 < value2) return value2; return 0; }
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) { var days = 0; // Day = true, Night = false. var isday = true; while (desiredHeight > 0) if (isday) { desiredHeight -= upSpeed; days++; isday = false; } else { desiredHeight += downSpeed; isday = true; } return days; }
int arrayMaxConsecutiveSum(int[] inputArray, int k) { var max = 0; var rem = 0; for (var i = 0; i <= inputArray.Length - k; i++) { if (i == 0) rem = inputArray.Where((i1, i2) => i2 >= i && i2 < i + k).Sum(); else rem = rem + inputArray[i + k - 1] - inputArray[i - 1]; max = Math.Max(rem, max); } return max; }
bool stringsRearrangement(string[] inputArray) { IEnumerable<IEnumerable<int>> permutations = GetPermutations(Enumerable.Range(1, inputArray.Length), inputArray.Length); Dictionary<int, bool> dictionary = new Dictionary<int, bool>(); int key = 0; foreach (IEnumerable<int> intArray in permutations) { key++; List<int> intList = intArray.ToList(); for (int i = 0; i < intList.Count - 1; i++) { int diffcount = 0; for (int j = 0; j < inputArray[intList[i] - 1].Length; j++) if (inputArray[intList[i] - 1][j] != inputArray[intList[i + 1] - 1][j]) diffcount++; if (diffcount != 1) { dictionary.Add(key, false); break; } } if (!dictionary.ContainsKey(key)) dictionary.Add(key, true); } return dictionary.Any(x => x.Value); } IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length) { if (length == 1) return list.Select(t => (IEnumerable<T>)new T[] { t }); return GetPermutations(list, length - 1) .SelectMany(t => list.Where(e => !t.Contains(e)), (t1, t2) => t1.Concat(new T[] { t2 })); }
int absoluteValuesSumMinimization(int[] A) { int smallest = int.MaxValue; int x = 0; foreach (int i in A) { if (A.Sum(t => Math.Abs(t - i)) >= smallest) continue; smallest = A.Sum(t => Math.Abs(t - i)); x = i; } return x; }
int depositProfit(int deposit, int rate, int threshold) { return Convert.ToInt32(Math.Ceiling(Math.Log(Convert.ToDouble(threshold) / Convert.ToDouble(deposit), 1 + Convert.ToDouble(rate) / 100))); }
int circleOfNumbers(int n, int firstNumber) { return (firstNumber + n / 2) % n; }
bool chessBoardCellColor(string cell1, string cell2) { return (cell1[0] - 'A' % 2 + cell1[1] - '1' % 2) % 2 == (cell2[0] - 'A' % 2 + cell2[1] - '1' % 2) % 2; }
string alphabeticShift(string inputString) { return new string(inputString.ToCharArray().Select(x => x == 'z' ? 'a' : (char)(x + 1)).ToArray()); }
bool variableName(string name) { if (Regex.IsMatch(name, "^\\d")) return false; if (name.All(c => ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) || char.IsDigit(c) || c == '_')) return true; return false; }
bool evenDigitsOnly(int n) { return n.ToString().All(c => int.Parse(c.ToString()) % 2 == 0); }
int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) { for (int i = 0; i < inputArray.Length; i++) if (inputArray[i] == elemToReplace) inputArray[i] = substitutionElem; return inputArray; }
int[][] minesweeper(bool[][] matrix) { int rows = matrix.Length; int columns = matrix[0].Length; int[][] solution = new int[rows][]; for (var index = 0; index < solution.Length; index++) solution[index] = new int[columns]; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) { int count = 0; if (i - 1 >= 0) { if (j - 1 >= 0) if (matrix[i - 1][j - 1]) count++; if (j + 1 >= 0 & j + 1 < columns) if (matrix[i - 1][j + 1]) count++; if (matrix[i - 1][j]) count++; } if (i + 1 >= 0 && i + 1 < rows) { if (j - 1 >= 0) if (matrix[i + 1][j - 1]) count++; if (j + 1 >= 0 & j + 1 < columns) if (matrix[i + 1][j + 1]) count++; if (matrix[i + 1][j]) count++; } if (j - 1 >= 0) if (matrix[i][j - 1]) count++; if (j + 1 >= 0 & j + 1 < columns) if (matrix[i][j + 1]) count++; solution[i][j] = count; } return solution; }
int[][] boxBlur(int[][] image) { int rows = image.Length; int columns = image[0].Length; int[][] solution = new int[rows - 2][]; for (int index = 0; index < solution.Length; index++) solution[index] = new int[columns - 2]; for (int i = 0; i < rows - 2; i++) for (int j = 0; j < columns - 2; j++) solution[i][j] = (image[i][j] + image[i][j + 1] + image[i][j + 2] + image[i + 1][j] + image[i + 1][j + 1] + image[i + 1][j + 2] + image[i + 2][j] + image[i + 2][j + 1] + image[i + 2][j + 2]) / 9; return solution; }
int avoidObstacles(int[] inputArray) { for (int i = 2; ; i++) if (!inputArray.Any(x => x % i == 0)) return i; }
bool isIPv4Address(string inputString) { return Regex.IsMatch(inputString, @"^([1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])\.([1][0-9][0-9]?|[2][0-4][0-9]|[2][5][0-5]|[1-9][0-9]|[0-9])$"); }
int arrayMaximalAdjacentDifference(int[] inputArray) { return inputArray.Take(inputArray.Length - 1).Select((a, i) => Math.Abs(inputArray[i + 1] - a)).Max(); }
bool areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) { return Math.Max(yourLeft, yourRight) == Math.Max(friendsLeft, friendsRight) && Math.Min(yourLeft, yourRight) == Math.Min(friendsLeft, friendsRight); }
bool palindromeRearranging(string inputString) { if (inputString.Length % 2 == 0 && inputString.Distinct().Any(c => inputString.Count(x => x == c) % 2 != 0)) return false; if (inputString.Length % 2 == 1) { int count = 0; foreach (var c in inputString.Distinct()) { if (inputString.Count(x => x == c) % 2 != 0) count++; if (count > 1) return false; } } return true; }
int arrayChange(int[] inputArray) { int change = 0; for (int i = 0; i < inputArray.Length - 1; i++) while (inputArray[i] >= inputArray[i + 1]) { inputArray[i + 1]++; change++; } return change; }
bool areSimilar(int[] A, int[] B) { if (A.Where((t, i) => t != B[i]).Count() > 2) return false; return A.Distinct().OrderBy(x => x).SequenceEqual(B.Distinct().OrderBy(x => x).ToArray()); }
string[] addBorder(string[] picture) { return new[] { new string('*', picture.Max(x => x.Length) + 2) }.Concat(picture.Select(x => "*" + x + "*")).Concat(new[] { new string('*', picture.Max(x => x.Length) + 2) }).ToArray(); }
int[] alternatingSums(int[] a) { return new[] { a.Where((b, c) => c % 2 == 0).Sum(), a.Where((b, c) => c % 2 == 1).Sum() }; }
string reverseParentheses(string s) { string cs = s; while (cs.Contains("(")) { string r = Regex.Match(cs, "\\([^\\(\\)]+\\)").Value; string r2 = r.Remove(r.Length - 1, 1); r2 = r2.Remove(0, 1); string r3 = string.Concat(r2.Reverse()); cs = cs.Replace(r, r3); } return cs; }
int[] sortByHeight(int[] a) { int b = 0; return a.Select(c => c == -1 ? c : a.Where(d => d != -1).OrderBy(d => d).ToArray()[b++]).ToArray(); }
bool isLucky(int n) { return Equals(n.ToString().Substring(0, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString())), n.ToString().Substring(n.ToString().Length / 2, n.ToString().Length / 2).Sum(x => int.Parse(x.ToString()))); }
int commonCharacterCount(string s1, string s2) { return s1.ToCharArray().Distinct().Sum(c => Math.Min(s1.Count(x => x == c), s2.Count(x => x == c))); }
string[] allLongestStrings(string[] inputArray) { return inputArray.Where(x => x.Length == inputArray.Max(y => y.Length)).ToArray(); }
int matrixElementsSum(int[][] matrix) { int sum = 0; int rows = matrix.GetLength(0); int columns = matrix[0].Length; for (int x = 0; x < columns; x++) for (int y = 0; y < rows; y++) { if (matrix[y][x] == 0) break; sum += matrix[y][x]; } return sum; }
bool almostIncreasingSequence(int[] sequence) { bool erased = false; for (int i = sequence.Length - 1; i > 0; i--) if (sequence[i] <= sequence[i - 1]) if (erased != true) erased = true; else return false; return true; }
int makeArrayConsecutive2(int[] statues) { int min = statues.Min(); int max = statues.Max(); int st = 0; for (int i = min; i <= max; i++) if (!statues.Contains(i)) st++; return st; }
bool checkPalindrome(string inputString) { char[] arr = inputString.ToCharArray(); Array.Reverse(arr); return string.Equals(new string(arr), inputString); }