Skip to content

Instantly share code, notes, and snippets.

@schauhan232
schauhan232 / C# Count frequncies of elements in array
Last active July 15, 2021 09:45
C# Count frequncies of elements in array
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;
@schauhan232
schauhan232 / Print diagonal matrix
Last active July 15, 2021 07:20
C# Print Diagonal matrix
using System;
namespace ConsoleApp1
{
class Program
{
public static void Main(string[] args)
{
var matrix = new int[,] {
{ 1, 2, 3, 4,5},
@schauhan232
schauhan232 / Distribute Candy Problem
Last active July 15, 2021 07:19
Distribute Candy Problem
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)
@schauhan232
schauhan232 / Minimum Number of platform needed
Created July 12, 2021 12:34
Minimum Number of platform needed
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);
@schauhan232
schauhan232 / Create next smallest number from the same array element
Created July 12, 2021 10:56
Create next smallest number from the same array element
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
@schauhan232
schauhan232 / Remove Duplicate Element from Sorted Array c#
Created July 10, 2021 05:34
Remove duplicate element from Sorted Array C#
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;
@schauhan232
schauhan232 / Counting Sort Hackerrank
Created July 9, 2021 14:10
Hackerrank: Counting Sort 2 C#
//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();
@schauhan232
schauhan232 / Trapping Rain Water
Created July 7, 2021 08:34
GeeksForGeeks: Trapping Rain Water
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++)
@schauhan232
schauhan232 / Day 2 : Operators
Created July 7, 2021 04:05
Hackerrank : Day 2 : Operators
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));
}
@schauhan232
schauhan232 / Find Maximum and Second Maximum with Minimum and Second Minimum value from array
Created July 7, 2021 03:28
GeeksForGeeks: Find Maximum and Second Maximum with Minimum and Second Minimum value from array
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}");