diff --git a/Contributors/Nitin-200104042.md b/Contributors/Nitin-200104042.md new file mode 100644 index 0000000..a20e0f0 --- /dev/null +++ b/Contributors/Nitin-200104042.md @@ -0,0 +1,86 @@ +## NITIN KUMAR +- Batch: 2020-2024 {CSE} +- Github Profile: {https://github.com/Nitin73290} +- Linkedin Profile: {https://www.linkedin.com/in/nitin-kumar-77067321a/} + + + POLICE CATCHES THIEVES + +// C++ program to find maximum number of thieves caught +#include +using namespace std; +int policeThief(char arr[], int n, int k) +{ +// Initialize the current lowest indices of +// policeman in pol and thief in thi variable as -1 +int pol = -1, thi = -1, res = 0; +// Find the lowest index of policemen +for (int i = 0; i < n; i++) { + if (arr[i] == 'P') { + pol = i; + break; + } +} + +// Find the lowest index of thief +for (int i = 0; i < n; i++) { + if (arr[i] == 'T') { + thi = i; + break; + } +} + +// If lowest index of either policemen or thief remain +// -1 then return 0 +if (thi == -1 || pol == -1) + return 0; +while (pol < n && thi < n) { + // can be caught + if (abs(pol - thi) <= k) { + + pol = pol + 1; + while (pol < n && arr[pol] != 'P') + pol = pol + 1; + + thi = thi + 1; + while (thi < n && arr[thi] != 'T') + thi = thi + 1; + + res++; + } + else if (thi < pol) { + thi = thi + 1; + while (thi < n && arr[thi] != 'T') + thi = thi + 1; + } + else { + pol = pol + 1; + while (pol < n && arr[pol] != 'P') + pol = pol + 1; + } +} +return res; +} + +int main() +{ +int k, n; +char arr1[] = { 'P', 'T', 'T', 'P', 'T' }; +k = 2; +n = sizeof(arr1) / sizeof(arr1[0]); +cout << "Maximum thieves caught: " << policeThief(arr1, n, k) << endl; + +char arr2[] = { 'T', 'T', 'P', 'P', 'T', 'P' }; +k = 2; +n = sizeof(arr2) / sizeof(arr2[0]); +cout << "Maximum thieves caught: " << policeThief(arr2, n, k) << endl; + +char arr3[] = { 'P', 'T', 'P', 'T', 'T', 'P' }; +k = 3; +n = sizeof(arr3) / sizeof(arr3[0]); +cout << "Maximum thieves caught: " << policeThief(arr3, n, k) << endl; + +return 0; +} + +// This code is contributed by Aditya Kumar (adityakumar129) diff --git a/Contributors/NitinKumar-200104042.md b/Contributors/NitinKumar-200104042.md new file mode 100644 index 0000000..8ea4fbd --- /dev/null +++ b/Contributors/NitinKumar-200104042.md @@ -0,0 +1,47 @@ +## NITIN KUMAR +- Batch: 2020-2024 {CSE} +- Github Profile: https://github.com/Nitin73290 +- Linkedin Profile: {https://www.linkedin.com/in/nitin-kumar-77067321a/} + + PEGION HOLE ALGORITHM + +/* C program to implement Pigeonhole Sort */ +#include +using namespace std; + +/* Sorts the array using pigeonhole algorithm */ +void pigeonholeSort(int arr[], int n) +{ + // Find minimum and maximum values in arr[] + int min = arr[0], max = arr[0]; + for (int i = 1; i < n; i++) + { + if (arr[i] < min) + min = arr[i]; + if (arr[i] > max) + max = arr[i]; + } + int range = max - min + 1; + + int index = 0; + for (int i = 0; i < range; i++) + { + vector::iterator it; + for (it = holes[i].begin(); it != holes[i].end(); ++it) + arr[index++] = *it; + } +} + +int main() +{ + int arr[] = {8, 3, 2, 7, 4, 6, 8}; + int n = sizeof(arr)/sizeof(arr[0]); + + pigeonholeSort(arr, n); + + printf("Sorted order is : "); + for (int i = 0; i < n; i++) + printf("%d ", arr[i]); + + return 0; +} diff --git a/Nitin Kumar-200104042.md b/Nitin Kumar-200104042.md new file mode 100644 index 0000000..0db2b42 --- /dev/null +++ b/Nitin Kumar-200104042.md @@ -0,0 +1,45 @@ +## Nitin Kumar +- Batch: 2020-2024 {CSE} +- Github Profile: {https://github.com/Nitin73290} +- Linkedin Profile: {https://www.linkedin.com/in/nitin-kumar-77067321a/} + + Fitting Shelves Problem + + +#include +using namespace std; + +void minSpacePreferLarge(int wall, int m, int n) +{ + int num_m = 0, num_n = 0, min_empty = wall; + int p = wall/m, q = 0, rem=wall%m; + num_m=p; + num_n=q; + min_empty=rem; + while (wall >= n) { + // place one more shelf of length n + q += 1; + wall = wall - n; + // place as many shelves of length m + // in the remaining part + p = wall / m; + rem = wall % m; + if (rem <= min_empty) { + num_m = p; + num_n = q; + min_empty = rem; + } + } + cout << num_m << " " << num_n << " " + << min_empty << endl; +} + +int main() +{ + int wall = 29, m = 3, n = 9; + minSpacePreferLarge(wall, m, n); + + wall = 76, m = 1, n = 10; + minSpacePreferLarge(wall, m, n); + return 0; +} diff --git a/NitinKumar-200104042.md b/NitinKumar-200104042.md new file mode 100644 index 0000000..bf2f8b2 --- /dev/null +++ b/NitinKumar-200104042.md @@ -0,0 +1,258 @@ +## Nitin kumar +- Batch: 2020-2024 {CSE} +- Github Profile: {https://github.com/Nitin73290} +- Linkedin Profile:{https://www.linkedin.com/in/nitin-kumar-77067321a/} + + HUFFMAN CODING + +#include +#include +using namespace std; +#define MAX_TREE_HT 100 + +// A Huffman tree node +struct MinHeapNode { + + // One of the input characters + char data; + + // Frequency of the character + unsigned freq; + + // Left and right child of this node + struct MinHeapNode *left, *right; +}; + +// A Min Heap: Collection of +// min-heap (or Huffman tree) nodes +struct MinHeap { + + // Current size of min heap + unsigned size; + + // capacity of min heap + unsigned capacity; + + // Array of minheap node pointers + struct MinHeapNode** array; +}; + +// A utility function allocate a new +// min heap node with given character +// and frequency of the character +struct MinHeapNode* newNode(char data, unsigned freq) +{ + struct MinHeapNode* temp + = (struct MinHeapNode*)malloc +(sizeof(struct MinHeapNode)); + + temp->left = temp->right = NULL; + temp->data = data; + temp->freq = freq; + + return temp; +} + +// A utility function to create +// a min heap of given capacity +struct MinHeap* createMinHeap(unsigned capacity) + +{ + + struct MinHeap* minHeap + = (struct MinHeap*)malloc(sizeof(struct MinHeap)); + + // current size is 0 + minHeap->size = 0; + + minHeap->capacity = capacity; + + minHeap->array + = (struct MinHeapNode**)malloc(minHeap-> +capacity * sizeof(struct MinHeapNode*)); + return minHeap; +} + +// A utility function to +// swap two min heap nodes +void swapMinHeapNode(struct MinHeapNode** a, + struct MinHeapNode** b) + +{ + + struct MinHeapNode* t = *a; + *a = *b; + *b = t; +} + +// The standard minHeapify function. +void minHeapify(struct MinHeap* minHeap, int idx) + +{ + + int smallest = idx; + int left = 2 * idx + 1; + int right = 2 * idx + 2; + + if (left < minHeap->size && minHeap->array[left]-> +freq < minHeap->array[smallest]->freq) + smallest = left; + + if (right < minHeap->size && minHeap->array[right]-> +freq < minHeap->array[smallest]->freq) + smallest = right; + + if (smallest != idx) { + swapMinHeapNode(&minHeap->array[smallest], + &minHeap->array[idx]); + minHeapify(minHeap, smallest); + } +} +int isSizeOne(struct MinHeap* minHeap) +{ + + return (minHeap->size == 1); +} + +struct MinHeapNode* extractMin(struct MinHeap* minHeap) + +{ + + struct MinHeapNode* temp = minHeap->array[0]; + minHeap->array[0] + = minHeap->array[minHeap->size - 1]; + + --minHeap->size; + minHeapify(minHeap, 0); + + return temp; +} + +void insertMinHeap(struct MinHeap* minHeap, + struct MinHeapNode* minHeapNode) + +{ + + ++minHeap->size; + int i = minHeap->size - 1; + + while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) { + + minHeap->array[i] = minHeap->array[(i - 1) / 2]; + i = (i - 1) / 2; + } + + minHeap->array[i] = minHeapNode; +} + +// A standard function to build min heap +void buildMinHeap(struct MinHeap* minHeap) + +{ + + int n = minHeap->size - 1; + int i; + + for (i = (n - 1) / 2; i >= 0; --i) + minHeapify(minHeap, i); +} + +// A utility function to print an array of size n +void printArr(int arr[], int n) +{ + int i; + for (i = 0; i < n; ++i) + cout<< arr[i]; + + cout<<"\n"; +} +int isLeaf(struct MinHeapNode* root) + +{ + + return !(root->left) && !(root->right); +} +struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) + +{ + + struct MinHeap* minHeap = createMinHeap(size); + + for (int i = 0; i < size; ++i) + minHeap->array[i] = newNode(data[i], freq[i]); + + minHeap->size = size; + buildMinHeap(minHeap); + + return minHeap; +} + +// The main function that builds Huffman tree +struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size) + +{ + struct MinHeapNode *left, *right, *top; + + struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size); 1 + while (!isSizeOne(minHeap)) { + + // Step 2: Extract the two minimum + // freq items from min heap + left = extractMin(minHeap); + right = extractMin(minHeap); + top = newNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + insertMinHeap(minHeap, top); + } + + return extractMin(minHeap); +} + +void printCodes(struct MinHeapNode* root, int arr[], int top) + +{ + + if (root->left) { + + arr[top] = 0; + printCodes(root->left, arr, top + 1); + } + if (root->right) { + + arr[top] = 1; + printCodes(root->right, arr, top + 1); + } + + if (isLeaf(root)) { + + cout<< root->data <<": "; + printArr(arr, top); + } +} + +void HuffmanCodes(char data[], int freq[], int size) + +{ + struct MinHeapNode* root + = buildHuffmanTree(data, freq, size); + int arr[MAX_TREE_HT], top = 0; + + printCodes(root, arr, top); +} + +// Driver code +int main() +{ + + char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; + int freq[] = { 5, 9, 12, 13, 16, 45 }; + + int size = sizeof(arr) / sizeof(arr[0]); + + HuffmanCodes(arr, freq, size); + + return 0; +} diff --git a/NitinKumar200104042.md b/NitinKumar200104042.md new file mode 100644 index 0000000..8f1229c --- /dev/null +++ b/NitinKumar200104042.md @@ -0,0 +1,79 @@ +## Nitin kumar +- Batch: 2020-2024 {CSE} +- Github Profile: {https://github.com/Nitin73290} +- Linkedin Profile: {https://www.linkedin.com/in/nitin-kumar-77067321a/} + + RAT IN MAZE + +#include +using namespace std; +#define N 4 +bool solveMazeUtil(int maze[N][N], int x, int y,int sol[N][N]); +void printSolution(int sol[N][N]) +{ + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) + cout<<" "<= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1) + return true; + return false; +} +bool solveMaze(int maze[N][N]) +{ + int sol[N][N] = { { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 } }; + if (solveMazeUtil(maze, 0, 0, sol) == false) { + cout<<"Solution doesn't exist"; + return false; + } + printSolution(sol); + return true; +} + + +bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]) +{ + // if (x, y is goal) return true + if (x == N - 1 && y == N - 1 && maze[x][y] == 1) { + sol[x][y] = 1; + return true; + } + Check if maze[x][y] is valid + if (isSafe(maze, x, y) == true) { + + if (sol[x][y] == 1) + return false; + mark x, y as part of solution path + sol[x][y] = 1; + + if (solveMazeUtil(maze, x + 1, y, sol) == true) + return true; + + if (solveMazeUtil(maze, x, y + 1, sol) == true) + return true; + + sol[x][y] = 0; + return false; + } + return false; +} + +// driver program to test above function +int main() +{ + int maze[N][N] = { { 1, 0, 0, 0 }, + { 1, 1, 0, 1 }, + { 0, 1, 0, 0 }, + { 1, 1, 1, 1 } }; + solveMaze(maze); + return 0; +} + diff --git a/Nitin_Kumar-200104042.md b/Nitin_Kumar-200104042.md new file mode 100644 index 0000000..c0da277 --- /dev/null +++ b/Nitin_Kumar-200104042.md @@ -0,0 +1,31 @@ +## Nitin kumar +- Batch: 2020-2024 {CSE} +- Github Profile: https://github.com/Nitin73290 +- Linkedin Profile: {https://www.linkedin.com/in/nitin-kumar-77067321a/} + + + KADANE'S ALGOTHIM + +#include +using namespace std; +int kadanes(int array[],int length) { + int highestMax = 0; + int currentElementMax = 0; + for(int i = 0; i < length; i++){ + currentElementMax =max(array[i],currentElementMax + array[i]) ; + highestMax = max(highestMax,currentElementMax); + } + return highestMax; +} +int main() { + cout << "Enter the array length: "; + int l; + cin >> l; + int arr[l]; + cout << "Enter the elements of array: "; + for (int i = 0; i < l; i++) { + cin >> arr[i]; + } + cout << "The Maximum Sum is: "<