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
Array of length n having integers 1 to n with some elements being repeated. | |
Count frequencies of all elements from 1 to n in Time Complexity O(n) and Space Complexity O(1) | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var matrix = new int[] { 5, 2, 7, 7, 5, 5, 2 }; | |
var someUniqueNumberGreaterThan = matrix.Max() + 1; |
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
using System; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var matrix = new int[,] { | |
{ 1, 2, 3, 4,5}, |
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
There are N children standing in a line with some rating value. You want to distribute a minimum number of candies to these children such that: | |
Each child must have at least one candy. | |
The children with higher ratings will have more candies than their neighbors. | |
You need to write a program to calculate the minimum candies you must give. | |
class Program | |
{ | |
public static void Main(string[] args) |
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
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var arrival = new int[] { 900, 940, 950, 1100, 1500, 1800 }; | |
var departure = new int[] { 910, 1200, 1120, 1130, 1900, 2000 }; | |
Array.Sort(arrival); | |
Array.Sort(departure); |
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
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var matrix = new int[] { 2, 5, 8, 7, 6, 1 }; | |
var loopLength = matrix.Length - 1; | |
var firstSmallElementIndex = -1; | |
//1. Find the index where there's small element on left hand side |
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
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var array = new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 }; // 1, 1, 2 | |
var LEFT = 0; | |
var RIGHT = 1; | |
var newIndex = 1; |
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
//Learned theory from https://www.youtube.com/watch?v=HkvChUv9dDg | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var array = new int[] { 19, 10, 12, 10, 24, 25, 22 }; | |
//generate empty array to add values on index | |
// keeping +1 in max to avoid index out error can be handle by other condition though | |
var arrayForOutPut = Enumerable.Repeat(0, array.Max() + 1).ToList(); |
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 Solution { | |
public int Trap(int[] height) { | |
return GetTotal(height); | |
} | |
private static int GetTotal(int[] height) | |
{ | |
var totalLength = height.Length; | |
var total = 0; | |
for (int i = 0; i < totalLength; 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 void solve(double meal_cost, int tip_percent, int tax_percent) | |
{ | |
double tip = (meal_cost /100) * tip_percent; | |
double tax = (meal_cost / 100) * tax_percent; | |
double total_cost = meal_cost + tip + tax; | |
Console.WriteLine(Math.Round(total_cost)); | |
} |
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
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
int[] userInputArray = { 104, 5, 99, 100, 45, 102, 67, 1, 3, 2, 9, 105 }; | |
var (minimum, secondMinimum) = MinimumValue(userInputArray); | |
var (maximum, secondMaximum) = MaximumValue(userInputArray); | |
Console.WriteLine($"Minimum: {minimum}, Second Minimum {secondMinimum}"); |
NewerOlder