├── .gitignore ├── .vscode ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── 1038. Binary Search Tree to Greater Sum Tree.cpp ├── Arrays ├── Count pairs in Array whose sum is k │ └── main.cpp ├── Count pairs whose LCM is greater that min of pair of numbers │ ├── Breif about problem.txt │ └── main.cpp ├── Count pairs whose sum is divisible by k │ ├── Breif about problem.txt │ └── main.cpp ├── FindMaxRegionLength - DFS Approach │ └── main.cpp ├── MaxSum SubArray │ ├── Breif about problem.txt │ └── main.cpp └── Sort arry by tracking indexes │ └── main.cpp ├── Companies ├── Goldmaan Sachs │ └── Previously asked programs │ │ ├── Codersbit1 │ │ └── main.cpp │ │ ├── CountPossibleStrings │ │ ├── Question.txt │ │ ├── main.cpp │ │ └── rec.cpp │ │ ├── DP │ │ └── Adds in movies │ │ │ ├── Question.txt │ │ │ └── main.cpp │ │ ├── Maximal Commanlity │ │ ├── Question.png │ │ └── main.cpp │ │ ├── OA 2019 - Find The Rank │ │ ├── Question.png │ │ └── main.cpp │ │ ├── OA 2019 - Spiral Matrix │ │ ├── Question.txt │ │ └── main.cpp │ │ ├── OA 2020 Strange Sorting Problem │ │ ├── Question.png │ │ └── main.cpp │ │ ├── Share Purchases │ │ ├── Input-output.txt │ │ ├── Shares_Purchase_Question.png │ │ └── main.cpp │ │ ├── String Recursively remove all adjacent duplicates │ │ ├── Question │ │ └── main.cpp │ │ └── count specific numbers in range │ │ ├── Question │ │ └── main.cpp ├── Most Common Questions │ └── Trapping Rain Water │ │ ├── Question.txt │ │ └── main.cpp └── Thought Works │ └── Online Hiring Test │ └── Ques1 │ ├── Question.txt │ └── main.cpp ├── LeetCode.cpp ├── Math └── Prime Check │ └── main.cpp ├── Search └── BinarySearch.cpp ├── Strings └── Reverse String │ └── main.cpp ├── Track └── MyTracker.xlsx ├── VizExperts ├── legacy.cpp ├── sphere.txt └── sphere2.txt ├── output.txt └── vizExperts ├── legacy.cpp └── sphere.txt /.gitignore: -------------------------------------------------------------------------------- 1 | a.exe -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "intelliSenseMode": "msvc-x64", 14 | "browse": { 15 | "path": [ 16 | "${workspaceRoot}", 17 | "C:\\MinGW\\lib\\gcc\\mingw32\\8.2.0\\include\\c++" 18 | ], 19 | "limitSymbolsToIncludedHeaders": true, 20 | "databaseFilename": "" 21 | } 22 | } 23 | ], 24 | "version": 4 25 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Launch", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "${workspaceFolder}/a.exe", 12 | "args": ["C:\\Users\\Hudwik\\Desktop\\Desktop\\My GIt\\Data_structures\\VizExperts\\sphere.txt"], 13 | "stopAtEntry": false, 14 | "cwd": "${workspaceFolder}", 15 | "environment": [], 16 | "externalConsole": false, 17 | "MIMode": "gdb", 18 | "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", 19 | "preLaunchTask": "myTask", 20 | "setupCommands": [ 21 | { 22 | "description": "Enable pretty-printing for gdb", 23 | "text": "-enable-pretty-printing", 24 | "ignoreFailures": true 25 | } 26 | ] 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "deque": "cpp", 4 | "list": "cpp", 5 | "string": "cpp", 6 | "vector": "cpp", 7 | "array": "cpp", 8 | "forward_list": "cpp", 9 | "unordered_map": "cpp", 10 | "unordered_set": "cpp", 11 | "string_view": "cpp", 12 | "initializer_list": "cpp", 13 | "regex": "cpp", 14 | "valarray": "cpp", 15 | "filesystem": "cpp", 16 | "xhash": "cpp", 17 | "xstring": "cpp", 18 | "xtree": "cpp", 19 | "xutility": "cpp", 20 | "bitset": "cpp", 21 | "utility": "cpp", 22 | "queue": "cpp", 23 | "random": "cpp", 24 | "stack": "cpp", 25 | "type_traits": "cpp", 26 | "ostream": "cpp", 27 | "algorithm": "cpp", 28 | "chrono": "cpp", 29 | "*.tcc": "cpp", 30 | "cctype": "cpp", 31 | "clocale": "cpp", 32 | "cmath": "cpp", 33 | "complex": "cpp", 34 | "csetjmp": "cpp", 35 | "csignal": "cpp", 36 | "cstdarg": "cpp", 37 | "cstddef": "cpp", 38 | "cstdio": "cpp", 39 | "cstdlib": "cpp", 40 | "cstring": "cpp", 41 | "ctime": "cpp", 42 | "cwchar": "cpp", 43 | "cwctype": "cpp", 44 | "exception": "cpp", 45 | "fstream": "cpp", 46 | "iomanip": "cpp", 47 | "iosfwd": "cpp", 48 | "iostream": "cpp", 49 | "istream": "cpp", 50 | "limits": "cpp", 51 | "memory": "cpp", 52 | "new": "cpp", 53 | "sstream": "cpp", 54 | "stdexcept": "cpp", 55 | "streambuf": "cpp", 56 | "typeinfo": "cpp", 57 | "atomic": "cpp", 58 | "cfenv": "cpp", 59 | "charconv": "cpp", 60 | "cinttypes": "cpp", 61 | "codecvt": "cpp", 62 | "condition_variable": "cpp", 63 | "cstdint": "cpp", 64 | "cuchar": "cpp", 65 | "functional": "cpp", 66 | "iterator": "cpp", 67 | "map": "cpp", 68 | "memory_resource": "cpp", 69 | "numeric": "cpp", 70 | "optional": "cpp", 71 | "ratio": "cpp", 72 | "set": "cpp", 73 | "system_error": "cpp", 74 | "tuple": "cpp", 75 | "future": "cpp", 76 | "mutex": "cpp", 77 | "scoped_allocator": "cpp", 78 | "shared_mutex": "cpp", 79 | "thread": "cpp", 80 | "typeindex": "cpp", 81 | "ios": "cpp", 82 | "locale": "cpp", 83 | "xfacet": "cpp", 84 | "xiosbase": "cpp", 85 | "xlocale": "cpp", 86 | "xlocbuf": "cpp", 87 | "xlocinfo": "cpp", 88 | "xlocmes": "cpp", 89 | "xlocmon": "cpp", 90 | "xlocnum": "cpp", 91 | "xloctime": "cpp", 92 | "xmemory": "cpp", 93 | "xstddef": "cpp", 94 | "xtr1common": "cpp", 95 | "variant": "cpp" 96 | } 97 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "label": "myTask", 8 | "type": "shell", 9 | "command": "g++", 10 | "args": [ 11 | "-g", "./vizExperts/legacy.cpp" 12 | ], 13 | "group": { 14 | "kind": "build", 15 | "isDefault": true 16 | } 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /1038. Binary Search Tree to Greater Sum Tree.cpp: -------------------------------------------------------------------------------- 1 | //QuestionLink: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/ 2 | 3 | /*The idea here is to store all the nodes and their values in sorted order using inorder traversal. 4 | Then we maintain a right Sum array and replace node's value by sum of nodes greater than equal to that node easily.*/ 5 | 6 | //Time Complexity (n), Space Complexity O(n) 7 | 8 | class Solution { 9 | public: 10 | void helper(vector& nodes, vector& sums, TreeNode* root){ 11 | if(!root) 12 | return; 13 | helper(nodes,sums,root->left); 14 | nodes.push_back(root); 15 | sums.push_back(root->val); 16 | helper(nodes,sums,root->right); 17 | } 18 | TreeNode* bstToGst(TreeNode* root) { 19 | vector nodes; 20 | vector sums; 21 | helper(nodes, sums, root); 22 | int n = sums.size(); 23 | for(int i=n-2;i>=0;i--){ 24 | sums.at(i) += sums.at(i+1); 25 | } 26 | for(int i=0;ival = sums.at(i); 28 | } 29 | return root; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Arrays/Count pairs in Array whose sum is k/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void findAndPrintSuchPairs(int arr[],int N,int k) 5 | { 6 | //stores the parsed records in hash table 7 | unordered_set s; 8 | 9 | for(int i=0;i min(arr[i],arr[j]) 2 | 3 | Given an array arr[], the task is to find the count of pairs from the array such that LCM(arr[i], arr[j]) > min(arr[i], arr[j]) 4 | Note: Pairs (arr[i], arr[j]) and (arr[j], arr[i]) are considered identical and will be counted only once. 5 | 6 | Examples: 7 | 8 | Input: arr[] = {1, 1, 4, 9} 9 | Output: 5 10 | All valid pairs are (1, 4), (1, 9), (1, 4), (1, 9) and (4, 9) 11 | 12 | Input: arr[] = {2, 4, 5, 2, 5, 7, 2, 8} 13 | Output: 24 -------------------------------------------------------------------------------- /Arrays/Count pairs whose LCM is greater that min of pair of numbers/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | int PossiblePairs(int n) 7 | { 8 | return n*(n-1)/2; 9 | } 10 | 11 | int CountSuchPairs(int arr[],int N) 12 | { 13 | int count = 0; 14 | count += PossiblePairs(N); 15 | 16 | unordered_map freq; 17 | for(int i=0; i 2 | using namespace std; 3 | 4 | int nc2(int n) 5 | { 6 | if(n<2) 7 | return 0; 8 | return n*(n-1)/2; 9 | } 10 | 11 | int CountPairsWhichAreDivisibleByK(int arr[],int N,int k) 12 | { 13 | int count = 0; 14 | int freq[k] = {0}; 15 | 16 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | int regionLength(vector> matrix, vector> visited, int n, int i, int j) 6 | { 7 | int len = 0; 8 | if(i>=0 && i<=n-1 && j>=0 && j<=n-1) 9 | { 10 | cout<<"a."<> matrix) 31 | { 32 | int n = matrix[0].size(), max_len = 0, len = 0; 33 | vector> visited(n); 34 | 35 | for (int i = 0; i < n; i++) 36 | { 37 | visited[i] = vector(n); 38 | for (int j = 0; j < n; j++) 39 | { 40 | visited[i][j] = 0; 41 | } 42 | } 43 | 44 | 45 | for (int i = 0; i < n; i++) 46 | { 47 | for (int j = 0; j < n; j++) 48 | { 49 | if(matrix[i][j]==1 && visited[i][j] == 0) 50 | { 51 | if(i==0 && j==0) 52 | { 53 | cout<<"1)"<max_len) 58 | {max_len = len;} 59 | len =0; 60 | } 61 | } 62 | } 63 | return max_len; 64 | } 65 | 66 | int main() 67 | { 68 | ofstream fout(getenv("OUTPUT_PATH")); 69 | 70 | int n; 71 | cin >> n; 72 | cin.ignore(numeric_limits::max(), '\n'); 73 | 74 | int m; 75 | cin >> m; 76 | cin.ignore(numeric_limits::max(), '\n'); 77 | 78 | vector> matrix(n); 79 | for (int i = 0; i < n; i++) { 80 | matrix[i].resize(m); 81 | 82 | for (int j = 0; j < m; j++) { 83 | cin >> matrix[i][j]; 84 | } 85 | 86 | cin.ignore(numeric_limits::max(), '\n'); 87 | } 88 | 89 | int result = connectedCell(matrix); 90 | 91 | fout << result << "\n"; 92 | 93 | fout.close(); 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Arrays/MaxSum SubArray/Breif about problem.txt: -------------------------------------------------------------------------------- 1 | Largest Sum Contiguous Subarray: 2 | 3 | Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. 4 | 5 | arr[] = {-2, -3, 4, -1, -2, 1, 5,-3} 6 | 7 | max sum = 7 // 4-1-2+1+5 8 | start index = 2 9 | end index = 6 -------------------------------------------------------------------------------- /Arrays/MaxSum SubArray/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMinInArray(int arr[], int N) 5 | { 6 | int min = arr[0]; 7 | for(int i=1;i0) 23 | { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | 31 | long long CalculateMaxSumAndPrintMaxSumSubArray(int arr[], int N) 32 | { 33 | long long max_sum = INT_MIN; 34 | int fin_start_index = -1, fin_end_index = -1; 35 | if(N>=1) 36 | { 37 | if(isPositiveNumberExists(arr,N)) 38 | { 39 | long long cur_sum = 0; 40 | int start_index = 0; 41 | //take it to first positive integer 42 | for(int i=0;i0) 45 | { 46 | start_index = i; 47 | break; 48 | } 49 | } 50 | for(int i=start_index;icur_sum) 54 | { 55 | cur_sum = arr[i]; 56 | start_index = i; 57 | if(cur_sum>max_sum) 58 | { 59 | max_sum = cur_sum; 60 | fin_start_index = start_index; 61 | fin_end_index = i; 62 | } 63 | } 64 | else if(cur_sum<0) 65 | { 66 | start_index = i+1; 67 | } 68 | else if(cur_sum>max_sum) 69 | { 70 | max_sum = cur_sum; 71 | fin_start_index = start_index; 72 | fin_end_index = i; 73 | } 74 | } 75 | } 76 | else 77 | { 78 | max_sum = findMinInArray(arr, N); 79 | } 80 | } 81 | cout<<"start index: "< 4 | using namespace std; 5 | 6 | int minmm(int a, int b) 7 | { 8 | if(a &A, vector &B, int s, int e, int C) 14 | { 15 | int len = C+1; 16 | cout<<"s: "<e) 19 | return 0; 20 | if(s==e) 21 | return B[0]; 22 | 23 | for(int i=s; i<=s; i++) 24 | { 25 | for(int j=i+1; j<=e; j++) 26 | { 27 | cout<<"i: "<=p_len) 33 | { 34 | b_index = k; 35 | break; 36 | } 37 | } 38 | //cout<<"p_len: "< &A, vector &B, int C) 60 | { 61 | int res = 0; 62 | 63 | sort(A.begin(), A.end()); 64 | sort(B.begin(), B.end()); 65 | res = dp(A, B, 0, A.size()-1, C); 66 | return res; 67 | } 68 | 69 | int main() 70 | { 71 | // vector A = {2, 3, 5, 8, 11, 15}; 72 | // vector B ={2, 3}; 73 | //vector A = {2, 4, 18}; 74 | //vector B ={2, 4, 6, 7}; 75 | // vector A = {3, 1, 25}; 76 | // vector B ={2, 4, 5}; 77 | vector A = {1, 5, 10, 20, 25, 30, 35, 38}; 78 | vector B ={2, 5, 8, 10, 44}; 79 | 80 | int C = 398, ans = 0; 81 | //printVec(res); 82 | ans = solve(A, B, C); 83 | cout<<"ans: "<baab 14 | (2)(1)(12) ->bal 15 | (2)(11)(2)->bkb 16 | (21)(1)(2)->uab 17 | (21)(12)->ul -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/CountPossibleStrings/main.cpp: -------------------------------------------------------------------------------- 1 | // C/C++ program to remove all adjacent duplicates from a string 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isInRange(char a, char b) 7 | { 8 | if( a == '0' || a == '1' || (a == '2' && (b >= '0' && b <= '5')) ) 9 | return true; 10 | return false; 11 | } 12 | 13 | int countPossibilities(string str, int n) 14 | { 15 | if(n == 0) 16 | return 0; 17 | else if(n==1) 18 | return 1; 19 | 20 | vector DP(n+2, 0); 21 | 22 | DP[0] = 1; 23 | if(isInRange(str[0], str[1])) 24 | DP[1] = 2; 25 | else 26 | DP[1] = 1; 27 | 28 | 29 | for(int i=2; i 3 | 4 | using namespace std; 5 | 6 | void printVec(vector vec) 7 | { 8 | for(int i=0;i& DP) 16 | { 17 | if(s>e) 18 | return 0; 19 | if(s==e) 20 | return 1; 21 | 22 | if(DP[s+1] != 0) 23 | return DP[s+1]; 24 | DP[s+1] += countPossibilities(str, s+1, e, DP); 25 | //count += countPossibilities(str, s+2, e); 26 | if(s+2<=e) 27 | { 28 | if( str[s+1] == '0' || str[s+1] == '1' || (str[s+1] == '2' && (str[s+2] == '0' || 29 | str[s+2] == '1' ||str[s+2] == '2' || str[s+2] == '3' || str[s+2] == '4' || str[s+2] == '5')) ) 30 | DP[s+1] += countPossibilities(str, s+2, e, DP); 31 | } 32 | 33 | 34 | 35 | return DP[s+1]; 36 | } 37 | 38 | // Driver program to test above functions 39 | int main() 40 | { 41 | string str= "2112"; 42 | vector DP(str.size()+2, 0); 43 | cout<< countPossibilities(str, str.size()-1, DP) < DP(str.size()+2, 0); 54 | 55 | //printVec(DP); 56 | //cout<< countPossibilities(str, -1, str.size()-1, DP) < 3 | 4 | using namespace std; 5 | 6 | int DP[100002]; 7 | 8 | int countPossibilities(vector V, vector> Ads, int ms, int me) 9 | { 10 | int count = 0; 11 | 12 | if(ms>me) 13 | return 0; 14 | if(DP[ms] !=0) 15 | { 16 | cout<<"Used\n"; 17 | return DP[ms]; 18 | } 19 | 20 | for(int i=0; i=ms && Ads[i][1]<=me) 23 | { 24 | count = max( V[i] + countPossibilities(V, Ads, Ads[i][1]+5, me), count ); 25 | } 26 | } 27 | DP[ms]=count; 28 | 29 | return count; 30 | } 31 | 32 | int main() 33 | { 34 | int m=20; 35 | vector V({6, 3, 9, 7, 10}); 36 | vector> Ads( 37 | { 38 | {12, 13}, 39 | {2, 3}, 40 | {6, 9}, 41 | {14, 17}, 42 | {10, 12} 43 | } 44 | ); 45 | //vector V({3, 9, 10, 6, 7}); 46 | /*vector> Ads( 47 | { 48 | {2, 3}, 49 | {6, 9}, 50 | {10, 12}, 51 | {12, 13}, 52 | {14, 17} 53 | } 54 | );*/ 55 | //Assuming Ads in sorted order 56 | memset(DP, 0, 1e5+2); 57 | cout< 3 | 4 | using namespace std; 5 | 6 | //This function is jjust for debuggin purpose 7 | int findMaximalCommanlity(string str) 8 | { 9 | if(str.size()<2) 10 | return 0; 11 | 12 | int maxMatch = 0, match = 0, n =str.size(); 13 | int Lt[26] = {0}; 14 | int Rt[26] = {0}; 15 | 16 | //for Lt --- str[0] 17 | Lt[str[0]-'a']++; 18 | //for Rt --- str[1]:str[n-1] 19 | for(int i=n-1;i>=1;i--) 20 | { 21 | Rt[str[i]-'a']++; 22 | if(Lt[str[i]-'a'] == 1) 23 | { 24 | match = 1; 25 | maxMatch = 1; 26 | } 27 | } 28 | 29 | //now slide Lt window right and Rt window right 30 | for(int i=1;i<=n-2;i++) 31 | { 32 | //cout<<"St "< Rt[str[i]-'a']) 38 | { 39 | match--; 40 | } 41 | else 42 | { 43 | if( !(Lt[str[i]-'a'] ==0 && Rt[str[i]-'a']==1) ) 44 | { 45 | match++; 46 | if(match>maxMatch) 47 | maxMatch = match; 48 | } 49 | } 50 | Lt[str[i]-'a']++; 51 | Rt[str[i]-'a']--; 52 | //cout<<"Ed "< 3 | 4 | using namespace std; 5 | 6 | //This function is jjust for debuggin purpose 7 | void printVector(vector> v) 8 | { 9 | if(v.size()<1) 10 | return; 11 | cout<<"["; 12 | for(int i=0;i>& matrix, int rank) 19 | { 20 | if(matrix.size()<1) 21 | return -1; 22 | 23 | int r =matrix.size(), c= matrix[0].size(), marks=0; 24 | 25 | //To maintain index after sorting we are considering pair in vector 26 | vector> studMarks; 27 | 28 | for(int i=0; i> matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; 48 | vector> matrix = {{79,89,15},{71,96,88},{85,89,92},{71,96,88},{71,96,99}, {71,96,88}}; 49 | int rank = 3; 50 | cout< 3 | 4 | using namespace std; 5 | 6 | void printVector(vector v) 7 | { 8 | if(v.size()<1) 9 | return; 10 | cout<<"["; 11 | for(int i=0;i spiralOrder(vector>& matrix) 37 | { 38 | vector retVec; 39 | if(matrix.size() < 1) 40 | return retVec; 41 | int r1=0, r2=matrix.size()-1, c1=0, c2=matrix[0].size()-1; 42 | while(r1<=r2 && c1<=c2) 43 | { 44 | //cout<=c1;j--) 53 | if(IsPrime(matrix[r2][j])) 54 | retVec.push_back(matrix[r2][j]); 55 | if(c1!=c2) 56 | for(int i=r2-1;i>r1;i--) 57 | if(IsPrime(matrix[i][c1])) 58 | retVec.push_back(matrix[i][c1]); 59 | c1++; c2--; r1++; r2--; 60 | } 61 | return retVec; 62 | } 63 | 64 | // Driver program to test above functions 65 | int main() 66 | { 67 | vector> matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; 68 | printVector(spiralOrder(matrix)); 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/OA 2020 Strange Sorting Problem/Question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrudwik/Data_structures/5e6087edb785a042df8058a95188ceb52c9bda3f/Companies/Goldmaan Sachs/Previously asked programs/OA 2020 Strange Sorting Problem/Question.png -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/OA 2020 Strange Sorting Problem/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector > sortArr(int arr[], int n) 5 | { 6 | vector > vp; 7 | 8 | for (int i = 0; i < n; ++i) { 9 | vp.push_back(make_pair(arr[i], i)); 10 | } 11 | 12 | sort(vp.begin(), vp.end()); 13 | return vp; 14 | } 15 | 16 | //reverse number 17 | int reversDigits(int num) 18 | { 19 | int rev_num = 0; 20 | while(num > 0) 21 | { 22 | rev_num = rev_num*10 + num%10; 23 | num = num/10; 24 | } 25 | return rev_num; 26 | } 27 | 28 | int transformNumAsPerMapping(vector> mapVec, int n) 29 | { 30 | int res = 0; 31 | 32 | while(n) 33 | { 34 | int k = mapVec[n%10].second; 35 | res = res*10 + k; 36 | n=n/10; 37 | } 38 | res = reversDigits(res); 39 | return res; 40 | } 41 | 42 | bool compare_as_pair (pair i,pair j) 43 | { 44 | return ( i.first < j.first ); 45 | } 46 | 47 | vector strangeSort(int mapping[], vector nums, int m) 48 | { 49 | vector> mapVec = sortArr(mapping, m); 50 | 51 | int n = nums.size(); 52 | vector > resVec; 53 | for(int i=0; i finVec; 62 | for(int i=0; i< resVec.size(); i++) 63 | { 64 | finVec.push_back(resVec[i].second); 65 | } 66 | return finVec; 67 | } 68 | 69 | void printVec(vector finVec) 70 | { 71 | for(int i=0; i< finVec.size(); i++) 72 | { 73 | cout< nums = { "12", "02", "4", "023", "65", "83", "224", "50"}; 81 | int mapping[] = { 3, 5, 4, 6, 2, 7, 9, 8, 0, 1 }; //fixed size 10 82 | vector nums = { "990", "332", "32"}; 83 | int m = sizeof(mapping) / sizeof(mapping[0]); 84 | 85 | vector finVec; 86 | finVec = strangeSort(mapping, nums, m); 87 | 88 | printVec(finVec); 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/Share Purchases/Input-output.txt: -------------------------------------------------------------------------------- 1 | Example 1: 2 | Input: "ABC" 3 | Output: 1 4 | 5 | 6 | Example 2: 7 | Input: "ABCCBA" 8 | Ouput: 7 9 | 10 | 11 | Example 3: 12 | Input: "PQACBA" 13 | Output: 7 -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/Share Purchases/Shares_Purchase_Question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrudwik/Data_structures/5e6087edb785a042df8058a95188ceb52c9bda3f/Companies/Goldmaan Sachs/Previously asked programs/Share Purchases/Shares_Purchase_Question.png -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/Share Purchases/main.cpp: -------------------------------------------------------------------------------- 1 | // C/C++ Solution of Share Purchase problem 2 | #include 3 | 4 | using namespace std; 5 | 6 | int CountTimePeriodOnWhichTheseThreeSharesAreInvested(vector str) 7 | { 8 | int n = str.size(); 9 | if(n<3) 10 | return 0; 11 | int count = 0, s=0, e = s; // sliding window 's' is start index 'e' is end index of the current window 12 | int a=0,b=0,c=0; //counters of A,B,C shares respectively 13 | 14 | while(s<=n-3 && e<=n-1) 15 | { 16 | if(str[e] == 'A') 17 | a++; 18 | else if(str[e] == 'B') 19 | b++; 20 | else if(str[e] == 'C') 21 | c++; 22 | 23 | while(a>0 && b>0 && c>0 && s<=n-3 && e<=n-1) 24 | { 25 | // suppose, [ABC]BCA here total 4 possible combinations are ABC,ABCB, ABCBC, BACBCA. (n-e = 6-2=4) 26 | count += (n - e); 27 | s++; 28 | 29 | if(str[s-1] == 'A') 30 | a--; 31 | else if(str[s-1] == 'B') 32 | b--; 33 | else if(str[s-1] == 'C') 34 | c--; 35 | } 36 | e++; 37 | } 38 | 39 | return count; 40 | } 41 | 42 | // Driver program to test above functions 43 | int main() 44 | { 45 | //string s = "ABCCBA"; 46 | //string s = "PQACBA"; 47 | string s = "ABBCZBAC"; 48 | vector str(s.begin(), s.end()); 49 | cout< 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | string print(stack s) 9 | { 10 | string str; 11 | while (s.size()) { 12 | str += s.top(); 13 | s.pop(); 14 | } 15 | return str; 16 | } 17 | 18 | string removeDuplicates(string S) 19 | { 20 | stack s; 21 | int len = S.size(); 22 | for (int i = len - 1; i >= 0; i--) { 23 | s.push(S[i]); 24 | while(i-1>=0 && s.size() && s.top() == S[i-1]) 25 | { 26 | s.pop();i--; 27 | } 28 | } 29 | 30 | //putting stack data into string 31 | string str = ""; 32 | while(s.size()) 33 | { 34 | str += s.top(); 35 | s.pop(); 36 | } 37 | 38 | return str; 39 | } 40 | 41 | // Driver program to test above functions 42 | int main() 43 | { 44 | string str1 = "geeksforgeeg"; 45 | cout << removeDuplicates(str1) << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/count specific numbers in range/Question: -------------------------------------------------------------------------------- 1 | Count numbers in range such that digits in it and it’s product with q are unequal 2 | Given a range of numbers [l, r] and an integer q. The task is to count all such number in the given range such that any digit of the number does not match with any digit in its product with the given number q. 3 | 4 | Examples: 5 | 6 | Input : l = 10, r = 12, q = 2 7 | Output : 1 8 | 10*2 = 20 which has 0 as same digit 9 | 12*2 = 24 which as 2 as same digit 10 | 11*2 = 22 no same digit 11 | 12 | Input : l = 5, r = 15, q = 2 13 | Output : 9 -------------------------------------------------------------------------------- /Companies/Goldmaan Sachs/Previously asked programs/count specific numbers in range/main.cpp: -------------------------------------------------------------------------------- 1 | // C/C++ program to remove all adjacent duplicates from a string 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool doesNoNumMatch(int a, int b) 7 | { 8 | unordered_set s; 9 | while(a) 10 | { 11 | int n = a%10; 12 | s.insert(n); 13 | a = a/10; 14 | } 15 | 16 | while(b) 17 | { 18 | int n = b%10; 19 | if(s.find(n)!=s.end()) 20 | return false; 21 | b = b/10; 22 | } 23 | return true; 24 | } 25 | 26 | int countSpecificNumsInRange(int l, int r, int q) 27 | { 28 | int count = 0; 29 | 30 | for(int i=l;i<=r;i++) 31 | { 32 | if(doesNoNumMatch(i,i*q)) 33 | count++; 34 | } 35 | 36 | return count; 37 | } 38 | 39 | // Driver program to test above functions 40 | int main() 41 | { 42 | //[l,r] q 43 | //int l = 10,r= 12,q=2; 44 | int l = 5,r= 15,q=2; 45 | 46 | cout< 3 | using namespace std; 4 | 5 | int trappedAmountOfRainWater(vector V) 6 | { 7 | int n = V.size(); 8 | if(n<3) //for upto 2 buildings rain water can't be trapped 9 | return 0; 10 | int s=0,e=n-1,leftMax = 0, rightMax = 0, totalWater = 0; 11 | //leftMax is current leftmax from left tll element s 12 | //rightMax is current rightMax from right tll element e 13 | 14 | while(sleftMax) 19 | leftMax = V[s]; 20 | else 21 | totalWater += leftMax - V[s]; 22 | s++; 23 | } 24 | else 25 | { 26 | if(V[e]>rightMax) 27 | rightMax = V[e]; 28 | else 29 | totalWater += rightMax - V[e]; 30 | e--; 31 | } 32 | } 33 | return totalWater; 34 | } 35 | 36 | int main() 37 | { 38 | int m=20; 39 | //vector V({0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}); 40 | vector V({0, 5, 2, 5, 1, 3}); 41 | cout< 2 | using namespace std; 3 | 4 | long long totalSadness (vector Height, long long n, long long &index) 5 | { 6 | long long sadness = 0; 7 | set> temp; 8 | long long prev = Height[0], last_max = 0; 9 | temp.insert(Height[0]); 10 | for(long long i=1;i> :: iterator itr; 14 | for(itr = temp.begin(); itr!=temp.end(); ++itr) 15 | { 16 | if(*itr>Height[i]) 17 | count++; 18 | else 19 | break; 20 | } 21 | if(sadness>last_max) 22 | { 23 | last_max = sadness; index = i; 24 | } 25 | sadness += count; 26 | //cout< Height) 32 | { 33 | long long n = Height.size(); 34 | if(n==1) 35 | return 0; 36 | long long index = 0; 37 | cout<<"Yo "<<" "; 38 | long long nextSad = totalSadness(Height, n, index), prevSad = 0; 39 | cout<<"Yo "<<" "; 40 | //cout<> T; 78 | for(int t_i=0; t_i> N; 82 | vector Height(N); 83 | for(int i_Height=0; i_Height> Height[i_Height]; 86 | } 87 | 88 | long long out_; 89 | out_ = func1(Height); 90 | cout << out_; 91 | cout << "\n"; 92 | } 93 | return 0; 94 | } -------------------------------------------------------------------------------- /LeetCode.cpp: -------------------------------------------------------------------------------- 1 | /*The idea here is to store all the nodes and their values in sorted order using inorder traversal. 2 | Then we maintain a right Sum array and replace node's value by sum of nodes greater than equal to that node easily.*/ 3 | 4 | //Time Complexity (n), Space Complexity O(n) 5 | 6 | class Solution { 7 | public: 8 | void helper(vector& nodes, vector& sums, TreeNode* root){ 9 | if(!root) 10 | return; 11 | helper(nodes,sums,root->left); 12 | nodes.push_back(root); 13 | sums.push_back(root->val); 14 | helper(nodes,sums,root->right); 15 | } 16 | TreeNode* bstToGst(TreeNode* root) { 17 | vector nodes; 18 | vector sums; 19 | helper(nodes, sums, root); 20 | int n = sums.size(); 21 | for(int i=n-2;i>=0;i--){ 22 | sums.at(i) += sums.at(i+1); 23 | } 24 | for(int i=0;ival = sums.at(i); 26 | } 27 | return root; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Math/Prime Check/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool IsPrime(int n) 6 | { 7 | if(n<=1) 8 | return false; 9 | if(n<=3) 10 | return true; 11 | 12 | //Every prime number will be of form 6x+1 or6x-1 {where x is any natural number} 13 | if( (n-1)%6 !=0 && (n+1)%6!=0 ) 14 | return false; 15 | if(n%2==0 || n%3==0) 16 | return false; 17 | 18 | for(int i=5;i*i<=n;i+=6) 19 | if(n%i==0 || n%(i+2)==0) 20 | return false; 21 | 22 | return true; 23 | } 24 | 25 | int main() 26 | { 27 | int n=13; 28 | if(IsPrime(n)) 29 | cout<<"Number is prime"; 30 | else 31 | cout<<"Number is not a prime"; 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Search/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | //Binary Search implementation 2 | #include 3 | 4 | int BinarySearch(int arr[], int s, int e, int k) 5 | { 6 | if(s<=e) 7 | { 8 | int m = (s+e)/2;//find out the middle index 9 | if(karr[m]) 14 | { 15 | return BinarySearch(arr, m+1, e, k); 16 | } 17 | else 18 | { 19 | return m; 20 | } 21 | } 22 | return -1; 23 | } 24 | 25 | int main() 26 | { 27 | int arr[] = {3,6,7,12,15,18}; //assuming arraay is in ascending order 28 | int N = sizeof(arr)/sizeof(arr[0]); 29 | int k =18; 30 | 31 | //sea1rching for the element k 32 | int index = BinarySearch(arr,0, N-1,k); 33 | if(index!=-1) 34 | std::cout<<"Element Found at index "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | //Testing purpose 7 | void printVec(vector s) 8 | { 9 | for(int i=0;i& s) 17 | { 18 | int st = 0, ed = s.size()-1; 19 | while(st vec = {'a','z','x','x','z','y'};//("azxxzy"); 32 | reverseString(vec); 33 | printVec(vec); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Track/MyTracker.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrudwik/Data_structures/5e6087edb785a042df8058a95188ceb52c9bda3f/Track/MyTracker.xlsx -------------------------------------------------------------------------------- /VizExperts/legacy.cpp: -------------------------------------------------------------------------------- 1 | //================= Legacy Code ==================== 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | template 13 | void RenderSphereOnScreen(const NumType* const _coords) 14 | { 15 | //Some time consuming Graphics stuff 16 | // No need To implement it 17 | std::cout<<_coords[0]<<" "<<_coords[1]<<" "<<_coords[2]</SphereRenderTask/SphereRenderTask 24 | * error : SphereRenderTask 25 | */ 26 | template 27 | class SphereRenderTask 28 | { 29 | NumType _coords[3]; 30 | //This static_assert will only accept int, float and double 31 | static_assert(std::is_same::value || std::is_same::value || std::is_same::value, 32 | "some meaningful error message"); 33 | public: 34 | SphereRenderTask(const NumType& x, const NumType& y, const NumType& z): _coords{x, y, z}{} 35 | 36 | void operator()() const 37 | { 38 | RenderSphereOnScreen(_coords); 39 | } 40 | }; 41 | 42 | void readFile(std::ifstream &inputfile, std::vector*> &taskList, int start_line, int end_line) 43 | { 44 | int x,y,z; 45 | string temp = ""; 46 | int cur = 0; 47 | while (!inputfile.eof() && !inputfile.fail() && cur!=end_line) 48 | { 49 | if(cur>= start_line && cur <= end_line) 50 | { 51 | inputfile >> x >> y>> z; 52 | taskList.push_back(new SphereRenderTask(x,y,z)); 53 | } 54 | else 55 | { 56 | getline(inputfile, temp); 57 | } 58 | 59 | cur++; 60 | } 61 | } 62 | 63 | void SplitLength(std::ifstream &inputfile, int &th1, int&th2) 64 | { 65 | string temp = ""; 66 | while (!inputfile.eof() ) { 67 | std::cout<<"Current Pos: "<*> taskList; 88 | 89 | std::ifstream inputfile, inputfile2; 90 | //prepare inputfile to throw if failbit gets set 91 | std::ios_base::iostate exceptionMask = inputfile.exceptions() | std::ios::failbit; 92 | inputfile.exceptions(exceptionMask); 93 | inputfile2.exceptions(exceptionMask); 94 | 95 | try { 96 | inputfile.open(inputFilePath); 97 | 98 | int th1 = 0, th2 = 0; 99 | SplitLength(inputfile, th1, th2); 100 | cout<<"th1: "<*>::iterator iter=taskList.begin(); iter!=taskList.end(); ++iter) 123 | { 124 | (**iter)(); 125 | } 126 | } 127 | catch (std::ios_base::failure& e) { 128 | if ( e.code() == std::make_error_condition(std::io_errc::stream) ) 129 | std::cerr << "Stream error!\n"; 130 | else 131 | std::cerr << "Unknown failure opening file.\n"; 132 | } 133 | catch (...) { 134 | std::cerr << "Generic exception caught\n"; 135 | } 136 | } 137 | 138 | int main(int argc, const char* argv[]) 139 | { 140 | //SphereRenderTask *abc = new SphereRenderTask(1,2,3); 141 | if(argc<1) 142 | { 143 | std::cerr<<"Bad Input"< 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | template 13 | void RenderSphereOnScreen(const NumType* const _coords) 14 | { 15 | //Some time consuming Graphics stuff 16 | // No need To implement it 17 | std::cout<<_coords[0]<<" "<<_coords[1]<<" "<<_coords[2]</SphereRenderTask/SphereRenderTask 24 | * error : SphereRenderTask 25 | */ 26 | template 27 | class SphereRenderTask 28 | { 29 | NumType _coords[3]; 30 | //This static_assert will only accept int, float and double 31 | static_assert(std::is_same::value || std::is_same::value || std::is_same::value, 32 | "some meaningful error message"); 33 | public: 34 | SphereRenderTask(const NumType& x, const NumType& y, const NumType& z): _coords{x, y, z}{} 35 | 36 | void operator()() const 37 | { 38 | RenderSphereOnScreen(_coords); 39 | } 40 | }; 41 | 42 | void readFile(std::ifstream &inputfile, std::vector*> &taskList, int start_line, int end_line) 43 | { 44 | int x,y,z; 45 | string temp = ""; 46 | int cur = 0; 47 | while (!inputfile.eof() && !inputfile.fail() && cur!=end_line) 48 | { 49 | if(cur>= start_line && cur <= end_line) 50 | { 51 | inputfile >> x >> y>> z; 52 | taskList.push_back(new SphereRenderTask(x,y,z)); 53 | } 54 | else 55 | { 56 | getline(inputfile, temp); 57 | } 58 | 59 | cur++; 60 | } 61 | } 62 | 63 | void SplitLength(std::ifstream &inputfile, int &th1, int&th2) 64 | { 65 | string temp = ""; 66 | while (!inputfile.eof() ) { 67 | std::cout<<"Current Pos: "<*> taskList; 88 | 89 | std::ifstream inputfile, inputfile2; 90 | //prepare inputfile to throw if failbit gets set 91 | std::ios_base::iostate exceptionMask = inputfile.exceptions() | std::ios::failbit; 92 | inputfile.exceptions(exceptionMask); 93 | inputfile2.exceptions(exceptionMask); 94 | 95 | try { 96 | inputfile.open(inputFilePath); 97 | 98 | int th1 = 0, th2 = 0; 99 | SplitLength(inputfile, th1, th2); 100 | cout<<"th1: "<*>::iterator iter=taskList.begin(); iter!=taskList.end(); ++iter) 123 | { 124 | (**iter)(); 125 | } 126 | } 127 | catch (std::ios_base::failure& e) { 128 | if ( e.code() == std::make_error_condition(std::io_errc::stream) ) 129 | std::cerr << "Stream error!\n"; 130 | else 131 | std::cerr << "Unknown failure opening file.\n"; 132 | } 133 | catch (...) { 134 | std::cerr << "Generic exception caught\n"; 135 | } 136 | } 137 | 138 | int main(int argc, const char* argv[]) 139 | { 140 | //SphereRenderTask *abc = new SphereRenderTask(1,2,3); 141 | if(argc<1) 142 | { 143 | std::cerr<<"Bad Input"<