Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete CC2_CSharp #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "e:/Learning/S30/Competitive-Coding-1",
"program": "e:/Learning/S30/Competitive-Coding-1/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
32 changes: 32 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity: O(log N)
// Space Complexity: O(1)

using System;

public class Solution {
public static int Search(int[] nums, int size) {
int low = 0, high = size - 1;

while (low <= high) {
int mid = low + (high - low) / 2;

// Check if the missing number is on the left side
if (nums[mid] != mid + 1) {
if (mid == 0 || nums[mid - 1] == mid) {
return mid + 1;
}
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1; // No missing number found
}

static public void Main(string[] args) {
int[] ar = { 1, 2, 3, 4, 5, 6, 8 };
int size = ar.Length;
Console.WriteLine("Missing number: " + Search(ar, size));
}
}
112 changes: 112 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;

class MinHeap
{
// Time Complexity:
// - Insert: O(log n)
// - Remove: O(log n)
// - MinHeapify: O(log n)
// - Print: O(n)
// Space Complexity: O(n)

private int[] heap;
private int size;
private int maxsize;

private static readonly int FRONT = 1;

public MinHeap(int maxsize)
{
this.maxsize = maxsize;
this.size = 0;
heap = new int[this.maxsize + 1];
heap[0] = int.MinValue;
}

private int Parent(int pos) { return pos / 2; }

private int LeftChild(int pos) { return 2 * pos; }

private int RightChild(int pos) { return (2 * pos) + 1; }

private bool IsLeaf(int pos)
{
return pos > (size / 2);
}

private void Swap(int fpos, int spos)
{
int temp = heap[fpos];
heap[fpos] = heap[spos];
heap[spos] = temp;
}

private void MinHeapify(int pos)
{
if (!IsLeaf(pos))
{
int swapPos = pos;
if (RightChild(pos) <= size)
swapPos = heap[LeftChild(pos)] < heap[RightChild(pos)] ? LeftChild(pos) : RightChild(pos);
else
swapPos = LeftChild(pos);

if (heap[pos] > heap[swapPos])
{
Swap(pos, swapPos);
MinHeapify(swapPos);
}
}
}

public void Insert(int element)
{
if (size >= maxsize)
return;

heap[++size] = element;
int current = size;

while (heap[current] < heap[Parent(current)])
{
Swap(current, Parent(current));
current = Parent(current);
}
}

public void Print()
{
for (int i = 1; i <= size / 2; i++)
{
Console.WriteLine($"PARENT: {heap[i]}, LEFT CHILD: {heap[2 * i]}, RIGHT CHILD: {heap[2 * i + 1]}");
}
}

public int Remove()
{
int popped = heap[FRONT];
heap[FRONT] = heap[size--];
MinHeapify(FRONT);
return popped;
}

public static void Main(string[] args)
{
MinHeap minHeap = new MinHeap(15);

minHeap.Insert(5);
minHeap.Insert(3);
minHeap.Insert(17);
minHeap.Insert(10);
minHeap.Insert(84);
minHeap.Insert(19);
minHeap.Insert(6);
minHeap.Insert(22);
minHeap.Insert(9);

Console.WriteLine("The Min Heap is:");
minHeap.Print();

Console.WriteLine("The Min value is " + minHeap.Remove());
}
}