diff --git a/day20/README.md b/day20/README.md index a17c4535..9c479831 100644 --- a/day20/README.md +++ b/day20/README.md @@ -9,7 +9,7 @@ Write a program to divide the given array into sub arrays where each sub array i ``` input: partition([1,2,3,4,5,6,7,8], 2) output: [[1, 2], [3, 4], [5, 6], [7, 8]] - + input: partition([1,2,3,4,5,6,7], 2) output: [[1, 2], [3, 4], [5, 6], [7]] ``` @@ -25,18 +25,18 @@ A very simple solution, all we have to is loop through i=0 to array length. In e ```js function partition (array, size) { - let partitionedArray = [], - toBeAdded = []; - - for (let i=0; i 0) partitionedArray.push (toBeAdded); - toBeAdded = []; - } - toBeAdded.push (array[i]); - } - if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); - return partitionedArray; +let partitionedArray = [], +toBeAdded = []; + +for (let i=0; i 0) partitionedArray.push (toBeAdded); +toBeAdded = []; +} +toBeAdded.push (array[i]); +} +if (toBeAdded.length > 0) partitionedArray.push (toBeAdded); +return partitionedArray; } ``` @@ -46,18 +46,18 @@ The second solution is also similar to the first one. Here, we create an empty a ```js function partition (array, size) { - let partitionedArray = []; - - for (let element of array) { - let partition = partitionedArray[partitionedArray.length - 1]; - if (!partition || partition.length === size) { - partitionedArray.push ([element]); - } else { - lastEle.push (element); - } - } +let partitionedArray = []; + +for (let element of array) { +let partition = partitionedArray[partitionedArray.length - 1]; +if (!partition || partition.length === size) { +partitionedArray.push ([element]); +} else { +lastEle.push (element); +} +} - return partitionedArray; +return partitionedArray; } ``` @@ -78,12 +78,12 @@ array.sllice (startIndex, endIndex); will return a subarray of array including e ```js function partition (array, size) { - let partitionedArray = [], i=0; - while (i using namespace std; int main(){ - cout << "Enter length of 1st and 2nd array"; - int n1, n2; - cin >> n1 >> n2; - int a[n1]; - int b[n2]; - for(int i = 0; i < n1; i++){ - cin >> a[i]; - - } - - for(int i = 0; i < n2; i++){ - cin >> b[i]; - } - - for(int i = 0; i < n1; i++){ - for(int j = 0; j < n2; j++){ - cout << "[ " << a[i] << " " << b[j] << " ] " << endl; - } - } - return 0; -} -``` \ No newline at end of file +cout << "Enter length of 1st and 2nd array"; +int n1, n2; +cin >> n1 >> n2; +int a[n1]; +int b[n2]; +for(int i = 0; i < n1; i++){ +cin >> a[i]; + +} + +for(int i = 0; i < n2; i++){ +cin >> b[i]; +} + +for(int i = 0; i < n1; i++){ +for(int j = 0; j < n2; j++){ +cout << "[ " << a[i] << " " << b[j] << " ] " << endl; +} +} +return 0; +} +``` diff --git a/day28/C++/Aadit/sample001.ans b/day28/C++/Aadit/sample001.ans new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/day28/C++/Aadit/sample001.ans @@ -0,0 +1 @@ +1 diff --git a/day28/C++/Aadit/sample001.in b/day28/C++/Aadit/sample001.in new file mode 100644 index 00000000..9d2b8984 --- /dev/null +++ b/day28/C++/Aadit/sample001.in @@ -0,0 +1,2 @@ +2 +1 2 3 4 5 diff --git a/day28/C++/Aadit/sample002.ans b/day28/C++/Aadit/sample002.ans new file mode 100644 index 00000000..417b7b53 --- /dev/null +++ b/day28/C++/Aadit/sample002.ans @@ -0,0 +1 @@ +undefined diff --git a/day28/C++/Aadit/sample002.in b/day28/C++/Aadit/sample002.in new file mode 100644 index 00000000..eaeda59b --- /dev/null +++ b/day28/C++/Aadit/sample002.in @@ -0,0 +1,2 @@ +7 +1 2 3 4 5 diff --git a/day28/C++/Aadit/solution.cpp b/day28/C++/Aadit/solution.cpp new file mode 100644 index 00000000..88e0b39f --- /dev/null +++ b/day28/C++/Aadit/solution.cpp @@ -0,0 +1,31 @@ +#include + +/** + * @author: aaditkamat + * @date: 28/1/2019 + */ + +/* + * The input is assumed to be of the following form: + * The first line contains the number to be searched + * The second line contains the numbers to be searched seperated by spaces. + */ +int main() { + int num; + std::cin >> num; + int ctr = 0; + bool flag = false; + while (!std::cin.eof()) { + int value; + std::cin >> value; + if (value == num) { + std::cout << ctr << std::endl; + flag = true; + } + ctr++; + } + if (!flag) { + std::cout << "undefined" << std::endl; + } + return 0; +} diff --git a/day28/C++/Aadit/test.sh b/day28/C++/Aadit/test.sh new file mode 100755 index 00000000..a6dc723d --- /dev/null +++ b/day28/C++/Aadit/test.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +clear +g++ solution.cpp -std=gnu++17 + +if [ $? -eq 0 ]; then + DIR=rationalsequence2 + + for i in {1..2} + do + ./a.out < ${DIR}/sample00${i}.in > ${DIR}/sample00${i}.o && diff ${DIR}/sample00${i}.o ${DIR}/sample00${i}.ans + open ${DIR}/sample00${i}.o + done +fi + + + + diff --git a/day29/C++/Binary_Search.cpp b/day29/C++/Binary_Search.cpp index abc0d302..6feeed3b 100644 --- a/day29/C++/Binary_Search.cpp +++ b/day29/C++/Binary_Search.cpp @@ -1,59 +1,55 @@ -/** - * @author: Karthick < karthikn2099@gmail.com > - * @github: https://github.com/karthikn2098 - * @date: 31/01/2018 - */ - -#include -using namespace std; - -/** @desc: splits the array into two sections at each step, then checks the element in the desired one. - * @param: array , key (to be found), size of the array. - * @return: index of key if present , -1 otherwise. - * @TimeComplexity: O( logn ) < n = size of the array > - */ -int Binary_search(int arr[] , int key, int N) { +/* +* @author : imkaka +* @date : 31/1/2019 +*/ - int low = 0 , high = N-1 , mid; +#include +#include - while(low <= high) { +using namespace std; - mid = low + (high - low) / 2; //like (low + high)/2 but efficient. +int binary_search_itr(int arr[], int size, int val){ + int l = 0, r = size-1; + int mid = (l + r) / 2; - if(key == arr[mid]) { - return mid; - } - else if( key > arr[mid] ) { - low = mid + 1; + while(arr[mid] != val && l <= r){ + if(val < arr[mid]){ + r = mid - 1; } - else { - high = mid - 1; + else{ + l = mid +1; } + + mid = (l + r) / 2; } + if(arr[mid] == val) + return mid; + return -1; } -int main(void) { - - int arr[] = { 1, 3, 5, 7, 8, 9, 100, 234, 788, 899 }; +int binary_search_rec(int arr[], int left, int right, int val){ + if(right >= left){ - int sizeOfArray = sizeof(arr) / sizeof(arr[0]); + int mid = left + (right - left) / 2; - cout << "\nBinary_search(arr , 100) = " << Binary_search(arr, 100, sizeOfArray ) << endl; + if(arr[mid] == val) return mid; - cout << "\nBinary_search(arr , 1000) = " << Binary_search(arr, 1000, sizeOfArray ) << endl; + if(arr[mid] > val) + return binary_search_rec(arr, left, mid-1, val); + return binary_search_rec(arr, mid+1, right, val); + } - return 0; + return -1; } -/* ---------------- - Sample Output ---------------- - -Binary_search(arr , 100) = 6 +int main(){ -Binary_search(arr , 1000) = -1 - -*/ + int arr[] = {1, 2, 3, 6, 9, 15, 16, 14}; + int val = 9; + int size = sizeof(arr)/sizeof(arr[0]); + cout << "Iterative: " << binary_search_itr(arr, size, val) << endl; + cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl; + return 0; +} diff --git a/day36/C++/radix_sort.cpp b/day36/C++/radix_sort.cpp new file mode 100644 index 00000000..855e8add --- /dev/null +++ b/day36/C++/radix_sort.cpp @@ -0,0 +1,69 @@ +/* + * @author: aaditkamat + * @date: 12/02/2019 + */ +#include +#include +#include +#include +#include +#define SIZE 10 + +/** + * The radix_sort works by processing decimal digits from right to left + * and sorting the numbers into buckets before combining them into a single + * array (mutating the original input array) at each step. The number of steps + * is equal to the number of decimal digits in the maximum element of the + * input array. + * + * e.g. input = [546, 37, 2132, 422] max_element = 2132, steps = digits(2132) = 4 + * Step 1: [[2132, 422], [546], [37]] input = [2132, 422, 546, 37] + * Step 2: [[422], [2132, 37], [546]] input = [422, 2132, 37, 546] + * Step 3: [[37], [2132], [422], [546]] input = [37, 2132, 422, 546] + * Step 4: [[37, 422, 546], [2132]] input = [37, 422, 546, 2132] + * + * The idea is that the smaller numbers (with lesser digits) will have 0s for the + * digits in the leftmost positions. Example can be written as 37 can be written + * as 0037 (0 * 10^3 + 0 * 10^2 + 3 * 10^1 + 7 * 10^0). If all numbers have them + * same number of digits then it will just sort according to 1st digit, breaking + * ties using the other digits. + */ +void printArray(std::vector output) { + for (int i = 0; i < output.size(); i++) { + std::cout << output[i] << " "; + } + std::cout << "\n"; +} + +int digits(int num) { + int ctr = 0; + while(num > 0) { + num /= 10; + ctr++; + } + return ctr; +} + +std::vector sort(std::vector input) { + int max = *max_element(input.begin(), input.end()); + for (int j = 0; j < digits(max); j++) { + std::array, SIZE> buckets; + for (int i = 0; i < input.size(); i++) { + int jth_digit = input[i] % static_cast(std::pow(10, j + 1)) / static_cast(std::pow(10, j)); + buckets[jth_digit].push_back(input[i]); + } + input = std::vector(); + for (int i = 0; i < SIZE; i++) { + for (int k = 0; k < buckets[i].size(); k++) { + input.push_back(buckets[i][k]); + } + } + } + return input; +} + +int main() { + std::vector input{39, 72, 64, 538, 2334, 39524, 1521, 2106, 3765}; + std::vector output = sort(input); + printArray(output); +} diff --git a/day36/README.md b/day36/README.md index 27069dbb..728fc9cf 100644 --- a/day36/README.md +++ b/day36/README.md @@ -21,4 +21,78 @@ output: [1, 2, 3, 4, 5, 6, 7, 8, 9] ```js to be added -``` \ No newline at end of file +``` + +### [C++ Implementation](./C++/radix_sort.cpp) + +```c++ +/* + * @author: aaditkamat + * @date: 12/02/2019 + */ +#include +#include +#include +#include +#include +#define SIZE 10 + +/** + * The radix_sort works by processing decimal digits from right to left + * and sorting the numbers into buckets before combining them into a single + * array (mutating the original input array) at each step. The number of steps + * is equal to the number of decimal digits in the maximum element of the + * input array. + * + * e.g. input = [546, 37, 2132, 422] max_element = 2132, steps = digits(2132) = 4 + * Step 1: [[2132, 422], [546], [37]] input = [2132, 422, 546, 37] + * Step 2: [[422], [2132, 37], [546]] input = [422, 2132, 37, 546] + * Step 3: [[37], [2132], [422], [546]] input = [37, 2132, 422, 546] + * Step 4: [[37, 422, 546], [2132]] input = [37, 422, 546, 2132] + * + * The idea is that the smaller numbers (with lesser digits) will have 0s for the + * digits in the leftmost positions. Example can be written as 37 can be written + * as 0037 (0 * 10^3 + 0 * 10^2 + 3 * 10^1 + 7 * 10^0). If all numbers have them + * same number of digits then it will just sort according to 1st digit, breaking + * ties using the other digits. + */ +void printArray(std::vector output) { + for (int i = 0; i < output.size(); i++) { + std::cout << output[i] << " "; + } + std::cout << "\n"; +} + +int digits(int num) { + int ctr = 0; + while(num > 0) { + num /= 10; + ctr++; + } + return ctr; +} + +std::vector sort(std::vector input) { + int max = *max_element(input.begin(), input.end()); + for (int j = 0; j < digits(max); j++) { + std::array, SIZE> buckets; + for (int i = 0; i < input.size(); i++) { + int jth_digit = input[i] % static_cast(std::pow(10, j + 1)) / static_cast(std::pow(10, j)); + buckets[jth_digit].push_back(input[i]); + } + input = std::vector(); + for (int i = 0; i < SIZE; i++) { + for (int k = 0; k < buckets[i].size(); k++) { + input.push_back(buckets[i][k]); + } + } + } + return input; +} + +int main() { + std::vector input{39, 72, 64, 538, 2334, 39524, 1521, 2106, 3765}; + std::vector output = sort(input); + printArray(output); +} +```