diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..564d3fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +*.txt \ No newline at end of file diff --git a/December 01/.gitignore b/December 01/.gitignore index 8e0ca25..5e9a3d7 100644 --- a/December 01/.gitignore +++ b/December 01/.gitignore @@ -229,3 +229,5 @@ modules.order Module.symvers Mkfile.old dkms.conf + +.vscode \ No newline at end of file diff --git a/December 01/cpp_karthickk17.cpp b/December 01/cpp_karthickk17.cpp new file mode 100644 index 0000000..cc64e4a --- /dev/null +++ b/December 01/cpp_karthickk17.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() { + int n; + cin>>n; + vector arr(n); + for(int i=0;i>arr[i]; + cout< + +using namespace std; + +int main() { + int n; + cin>>n; + vector arr(n); + unordered_map count; + for(int i=0;i>arr[i]; + count[arr[i]]++; + } + for(int i:arr){ + if(count[i] > 0) { + cout< + +using namespace std; + +int main() { + int n; + cin>>n; + vector arr(n); + int count = 1, max; + for (int i = 0; i < n; i++) + { + cin>>arr[i]; + + if(i == 0) + max = arr[i]; + //Assigning the default value of the max as the first element. + //It can also be assigned as INT_MIN. It doesn't change the result. + else{ + + if(max + +using namespace std; + +int main() { + string str; + cin>>str; + int n = str.length(); + if(n == 1) cout<<"Error"; + //The smallest substing is of length 2 or 3. Its 2 in case of even length string and 3 in case of odd length string. + //All palindromic strings must have the same substring at the middle part. If it doesn't, it is not a palindrome. + else if(n % 2 == 0){ + if(tolower(str[n/2]) == tolower(str[(n/2)-1])) + cout< + +using namespace std; + +int main() { + int n; + cin>>n; + vector arr(n); + int avg; + for (int i = 0; i < n; i++) + cin>>arr[i]; + //The test cases don't provide correct answer for the given question. In the first test case + //the average is 30 and the sum of elements greater than or equal to average are 30 40 and 50 whose sum is 120. + avg=accumulate(arr.begin(),arr.end(),0)/n; + sort(arr.begin(),arr.end()); + cout< + +using namespace std; + +vector fin;//To store the final string array +int maxlen = 0;//To store the current maximum optimal array + +void helper(int tempSize,string prev, vector arr, vector temp) { + + if(tempSize>maxlen){ + fin=temp; + maxlen=tempSize; + } + for(size_t i=0; i < arr.size(); i++) { + if(tempSize == 0){//It takes care of the initial cases. It ensures every string is started at least once. + string tempStore=arr[i]; + temp.push_back(tempStore); + arr[i]="-1"; + helper(tempSize+1,tempStore,arr,temp); + arr[i]=tempStore; + temp.pop_back(); + + } + else if(arr[i] != "-1") { + int count = 0; + for (int j = 0; j < prev.length(); j++) { + if(prev[j] != arr[i][j]) + count++; + } + if(count <= 1) {//If the count is 1, then the string has only one difference with the previous one + string tempStore=arr[i]; + arr[i] = "-1"; + temp.push_back(tempStore); + helper(tempSize+1,tempStore,arr,temp);//It uses a recursion technique to find all possible combinations + //Can use dp to optimize this better. + arr[i]=tempStore; + temp.pop_back(); + } + + } + } +} + +int main() { + int n; + //cin>>n; + n=7; + vector arr(n); + arr={"cat", "cot", "dot", "dog", "cog", "coat", "doll"}; + // for(int i=0;i>arr[i]; + helper(0,"",arr, {}); + if(fin.size() == 1) + cout<<"No valid chain"; + else{ + for(string t:fin) + cout< + +using namespace std; + +int main() { + int width,height,radius; + cin>>width>>height>>radius; + float diagonal = sqrt(pow(width,2)+pow(height,2)); + cout< + +using namespace std; +/* +The program works as follows: +First the numbers are placed increasingly like 1,2,3,... +1 is always placed at the first row and middle column. +for a 3x3 matrix, it is placed as + 0 1 0 + 0 0 0 + 0 0 0 +Then we go to north-east element of our position. For element 5, it is 3 and for element 8, it is 6. + 1 2 3 + 4 5 6 + 7 8 9 +If the element already contains a number, place the holding string directly below it. +If the positions go beyond the boundaries of the matrix, rotate it appropriately such that it comes within the range. +*/ +int main() { + int n; + cin>>n; + vector> matrix(n, vector(n,0)); + int r=0, c=(n/2), cur_num=1; + while(cur_num <= n*n) { + matrix[r][c] = cur_num; + cur_num++; + c=(c+1)%n; + r=r-1; + if(r<0) r=n-1; + if(matrix[r][c] != 0) { + c-=1; + if(c<0) c=n-1; + r=(r+2)%n; + } + } + int maxDigits = to_string(n*n).length();//n*n is the maximum number in the matrix + for (vector arr : matrix) { + for (int num : arr) { + // Displaying the number with proper spacing. + cout << setw(maxDigits) << num << " "; + } + cout << endl; + } + return 0; +} diff --git a/December 09/cpp_karthickk17.cpp b/December 09/cpp_karthickk17.cpp new file mode 100644 index 0000000..7e6bed1 --- /dev/null +++ b/December 09/cpp_karthickk17.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() { + int no_words = 1;//The first word doesn't have uppercase letters + //Every uppercase character marks the beginning of new word. + string line; + cin>>line; + for(char ch:line) { + if(ch >= 'A' && ch <= 'Z') + no_words++; + } + cout< + +using namespace std; + +int main() { + set tables{"emp"};//It is used for storing table names + unordered_map> table_column_map{{"emp",{"empname"}}};//It is used for storing the list of columns for the given table + unordered_map> column_values{{"empname",{"Shivnash Kumar", "Ragul Gupta"}}};//It is used for storing the list of columns for the given table + string query, tableName, columnName; + int length, startIndex; + getline(cin, query); + //query = "select substring(empname,4,13) from emp;"; + transform(query.begin(),query.end(),query.begin(),::tolower); + regex pattern(R"(select\s+substring\((\w+),(\d+),(\d+)\)\s+from\s+(\w+);)"); + + // Match the regular expression pattern against the query + smatch matches; + if (regex_match(query, matches, pattern)) { + // Extract the matched components + columnName = matches[1].str(); + startIndex = stoi(matches[2].str())-1; // Convert to int + length = stoi(matches[3].str()); // Convert to int + tableName = matches[4].str(); + } + else { + cout << "Invalid query format." << endl; + } + if(tables.find(tableName) == tables.end()) cout<<"Invalid Table Name"; + auto it = table_column_map.find(tableName); + if (it == table_column_map.end() || find(it->second.begin(), it->second.end(), columnName) == it->second.end()) { + cout << "Column is not present in the table." << endl; + } + it = column_values.find(columnName); + for(string str: it->second) { + cout< + +using namespace std; + +void add_and_convert(int num1, int num2) { + int sum = num1 + num2; + string ans = ""; + while (sum > 0) { + if (sum & 1) //In the bit representation, if the bit is set as 1, the string is appended with 1 + ans += "1"; + else + ans += "0"; + sum >>= 1; + } + //As the bits are checked from left to right, we have to reverse finally to display in properly. + reverse(ans.begin(), ans.end()); + cout<> num1 >> num2; + add_and_convert(num1, num2); + return 0; +} diff --git a/December 12/cpp_karthickk17.cpp b/December 12/cpp_karthickk17.cpp new file mode 100644 index 0000000..b43c594 --- /dev/null +++ b/December 12/cpp_karthickk17.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +bool binarySearch(vector box, string target) { + int low = 0; + int high = box.size() - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + + if (box[mid] == target) { + return true; // Gold found in this box + } else if (box[mid] < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return false; // Gold not found in this box +} + +string findGoldBox(vector> boxes) { + for (auto box = boxes.begin(); box != boxes.end(); box++) { + sort(box->begin(), box->end()); //Sorting the items based on the alphabetical order. + if (binarySearch(*box, "Gold")) + return {"Box"+to_string(box-boxes.begin()+1)+" contains the Gold"};//To display the position of the box + } + + return "Gold not found in any box"; // This should not happen if the input is valid +} + +int main() { + + vector> boxes = { + {"Emerald", "Ruby", "Bronze", "Silver"}, + {"Gold", "Diamond", "Ruby", "Copper"}, + {"Ruby", "Platinum", "Bronze", "Silver"} + }; + + cout << findGoldBox(boxes) << endl; + + return 0; +} diff --git a/December 13/cpp_karthickk17.cpp b/December 13/cpp_karthickk17.cpp new file mode 100644 index 0000000..8925e14 --- /dev/null +++ b/December 13/cpp_karthickk17.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +string textToNum(string encrypted_string) { + string decrypted_string = ""; + for (char ch: encrypted_string) { + if ( isalpha(ch) ) { + if ( ch <= 'C' ) + decrypted_string += '2'; + else if ( ch <= 'F' ) + decrypted_string += '3'; + else if ( ch <= 'I' ) + decrypted_string += '4'; + else if ( ch <= 'L' ) + decrypted_string += '5'; + else if ( ch <= 'O' ) + decrypted_string += '6'; + else if ( ch <= 'S' ) + decrypted_string += '7'; + else if ( ch <= 'V' ) + decrypted_string += '8'; + else + decrypted_string += '9'; + } + else + decrypted_string += ch; + } + return decrypted_string; +} + +int main() { + + string encrypted_string; + cin >> encrypted_string; + cout << textToNum(encrypted_string); + return 0; +} diff --git a/December 14/cpp_karthickk17.cpp b/December 14/cpp_karthickk17.cpp new file mode 100644 index 0000000..ef4d807 --- /dev/null +++ b/December 14/cpp_karthickk17.cpp @@ -0,0 +1,153 @@ +#include +using namespace std; + +struct Node +{ + int data; + Node* left; + Node* right; +}; + +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + +Node* buildTree(string str) +{ + + if(str.length() == 0 || str[0] == 'N') + return NULL; + + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + Node* root = newNode(stoi(ip[0])); + + queue queue; + queue.push(root); + + int i = 1; + while(!queue.empty() && i < ip.size()) { + + + Node* currNode = queue.front(); + queue.pop(); + + string currVal = ip[i]; + + if(currVal != "N") { + + currNode->left = newNode(stoi(currVal)); + + queue.push(currNode->left); + } + + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + if(currVal != "N") { + + currNode->right = newNode(stoi(currVal)); + + queue.push(currNode->right); + } + i++; + } + + return root; +} + +class Solution +{ +private: + +public: + vector nodes; + + void KDistanceNodesDown(Node* root, int k) { + if (root == NULL) + return; + if(k == 0) { + nodes.push_back(root->data); + return; + } + KDistanceNodesDown(root->left, k-1); + KDistanceNodesDown(root->right, k-1); + + } + + int helper(Node* root, int target, int k) { + if(root == NULL) + return -1; + if(root->data == target) + { + KDistanceNodesDown(root, k); + return 0; + } + int dl = helper(root->left, target, k); + if (dl != -1){ + if (dl + 1 == k) + nodes.push_back(root->data); + else + KDistanceNodesDown(root->right, k-dl-2); + return 1 + dl; + } + int dr = helper(root->right, target, k); + if (dr != -1){ + if (dr + 1 == k) + nodes.push_back(root->data); + else + KDistanceNodesDown(root->left, k-dr-2); + return 1 + dr; + } + return -1; + } + + vector KDistanceNodes(Node* root, int target , int k) + { + nodes.clear(); + helper(root, target, k); + sort(nodes.begin(), nodes.end()); + return nodes; + } + +}; + +int main() +{ + + int t; + cin>>t; + getchar(); + + Solution x = Solution(); + + while(t--) + { + string s; + getline(cin,s); + Node* head = buildTree(s); + + int target, k; + cin>> target >> k; + getchar(); + + vector res = x.KDistanceNodes(head, target, k); + + for( int i=0; i + +using namespace std; + +unordered_set string_set; + +void helper (string str, int index, string temp_str) { + if (index >= str.length()) + return; + temp_str += str[index]; + string_set.insert(temp_str); + helper(str, index+1, temp_str); + temp_str.pop_back(); + helper(str, index+1, temp_str); +} + +int main() +{ + string str; + cin >> str; + helper(str, 0, ""); + // for (string s:string_set) + // cout << s << " "; + // cout << endl; + cout << string_set.size() + 1 << endl; // To include the empty set, I have added the +1 in the output. + return 0; +} \ No newline at end of file diff --git a/December 16/cpp_karthickk17.cpp b/December 16/cpp_karthickk17.cpp new file mode 100644 index 0000000..1657908 --- /dev/null +++ b/December 16/cpp_karthickk17.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +//Check if the coordinated entered i.e x and y form a valid pair +bool check(int x, int y, int m, int n) { + if(x >= 0 && y >= 0 && x < m && y < n) { + return true; + } + return false; +} + +//Calculates the time +int calculate_time (vector>& grid) { + int time = 0; + //Queue stores the coordinates of zombies + queue> q; + int m = grid.size(), n = grid[0].size(); + //Initalizing the queue with the zombie coordinates + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 1) + { + q.push({i,j}); + } + } + } + + //In order to iterate through the queue and find the coordinates of the zombies to focus on, we need its count. + int cur_queue_size = q.size(), next_queue_size = 0; + + while (q.size() != 0) + { + + for (int i = 0; i < cur_queue_size; i++) + { + pair coord = q.front(); + q.pop(); + int x = coord.first, y = coord.second; + //Checking up, down, left and right + if (check(x, y, m, n) && check(x-1, y, m, n) && grid[x-1][y] == 0) + { + q.push({x-1,y}); + next_queue_size++; + grid[x-1][y] = 1; + } + if (check(x, y, m, n) && check(x+1, y, m, n) && grid[x+1][y] == 0) + { + q.push({x+1,y}); + next_queue_size++; + grid[x+1][y] = 1; + } + if (check(x, y, m, n) && check(x, y-1, m, n) && grid[x][y-1] == 0) + { + q.push({x,y-1}); + next_queue_size++; + grid[x][y-1] = 1; + } + if (check(x, y, m, n) && check(x, y+1, m, n) && grid[x][y+1] == 0) + { + q.push({x,y+1}); + next_queue_size++; + grid[x][y+1] = 1; + } + } + //If there is no change, then we can exit the loop + if (next_queue_size == 0) + break; + cur_queue_size = next_queue_size; + next_queue_size = 0; + time++; + + } + //To check if any human or empty cell still exists + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 0) + { + return -1; + } + } + } + return time; +} + +int main() +{ + //The second test is incorrect. The time in minutes required to infect the entire city is 2 minutes and not 4. + vector> grid { + {0, 1, 0, 0}, + {1, 1, 1, 1}, + {0, 1, -1, 0}, + {0, 0, 0, 0} + }; + // Calling the function to calculate the time taken to infect the city + cout << calculate_time(grid) << endl; + return 0; +} \ No newline at end of file diff --git a/December 17/cpp_karthickk17.cpp b/December 17/cpp_karthickk17.cpp new file mode 100644 index 0000000..da62f05 --- /dev/null +++ b/December 17/cpp_karthickk17.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + +struct Node +{ + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; + } +}; + +void loopHere(Node* head, Node* tail, int position) +{ + if(position==0) return; + + Node* walk = head; + for(int i=1; inext; + tail->next = walk; +} + +bool isLoop(Node* head) +{ + if(!head) return false; + + Node* fast = head->next; + Node* slow = head; + + while( fast != slow) + { + if( !fast || !fast->next ) return false; + fast=fast->next->next; + slow=slow->next; + } + + return true; +} + +int length(Node* head) +{ + int ret = 0; + while(head) + { + ret++; + head = head->next; + } + return ret; +} + +bool notOriginal(Node *head, unordered_map&myMap){ + + while(head){ + if(myMap.find(head)==myMap.end()) return true; + if(myMap[head] != (head->data)) return true; + + head=head->next; + } +} + + + + +// } Driver Code Ends +/* +structure of linked list node: + +struct Node +{ + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; + } +}; + +*/ + +class Solution +{ + public: + //Function to remove a loop in the linked list. + void removeLoop(Node* head) + { + // code here + // just remove the loop without losing any nodes + Node *fast, *slow; + fast = slow = head; + while (fast != NULL && fast->next != NULL) { + fast = fast->next->next; + slow = slow-> next; + if(slow == fast) + break; + } + if(slow != fast) { + //cout<<"No Loop detected!" << endl; + return; + } + slow = head; + if (slow == fast) + while (fast->next != slow) + fast = fast->next; + else { + while (slow->next != fast->next) { + slow = slow->next; + fast = fast->next; + } + } + fast->next = NULL; + return; + } +}; + +//{ Driver Code Starts. + +int main() +{ + int t; + cin>>t; + while(t--) + { + unordered_mapmyMap; + + int n, num; + cin>>n; + + Node *head, *tail; + cin>> num; + head = tail = new Node(num); + + myMap[head]=num; + + for(int i=0 ; i> num; + tail->next = new Node(num); + tail = tail->next; + myMap[tail]=num; + } + + int pos; + cin>> pos; + loopHere(head,tail,pos); + + Solution ob; + ob.removeLoop(head); + + if( isLoop(head) || length(head)!=n || notOriginal(head, myMap)) + cout<<"0\n"; + else + cout<<"1\n"; + } + return 0; +} \ No newline at end of file diff --git a/December 18/cpp_karthickk17.cpp b/December 18/cpp_karthickk17.cpp new file mode 100644 index 0000000..cc72290 --- /dev/null +++ b/December 18/cpp_karthickk17.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +void dfs(int src, int par, vector edges[], vector& arr) { + for (auto ch : edges[src]) { + // cout< n) return 0; + + if (r == 0 || n == r) return 1; + + double res = 0; + for (int i = 0; i < r; i++) { + + res += log(n-i) - log(i+1); + } + return (int)round(exp(res)); +} + +int main() +{ + int t; + cin >> t; + while(t--) { + int n, x; + cin >> n >> x; + vector arr(n+1); + for (int i = 1; i <= n; i++) + { + cin >> arr[i]; + } + vector edges [n+1]; + for (int i = 0; i < n - 1; i++) + { + int u, v; + cin >> u >> v; + edges[u].push_back(v); + edges[v].push_back(u); + } + dfs(1,-1, edges, arr); + if (arr[1]%x != 0) { + for (int i = 1; i <= n; i++) + cout << 0 << " "; + cout << endl; + return 0; + } + // counting the number of good edges + int ct = 0; + for (int i = 2; i <= n; i++) if (arr[i]%x == 0) ct++; + + for (int i = 1; i <= n; i++) cout << nCr(ct, i-1) << " "; cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/December 19/cpp_karthickk17.cpp b/December 19/cpp_karthickk17.cpp new file mode 100644 index 0000000..6a2e83e --- /dev/null +++ b/December 19/cpp_karthickk17.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; + +int sum_subsequence(vector& sequence, int start, int end) { + int sum = 0; + for (int i = start; i <= end; i++) { + if (isdigit(sequence[i][0])) { + sum += stoi(sequence[i]); + } + } + return sum; +} + +int symbolic_sum(vector& sequence) { + int result = 0; + int n = sequence.size(); + for (int i = 0; i < n; i++) { + if (sequence[i][0] == 'X') { + int multiplier = 1; + if (sequence[i].size() > 1) { + multiplier = stoi(sequence[i].substr(1)); + } + int subsum = sum_subsequence(sequence, i, n - 1); + result += multiplier * subsum; + } + } + return result; +} + +int main() { + vector sequence1 = {"X3", "3", "X2", "2", "X1", "1", "4"}; + int result1 = symbolic_sum(sequence1); + cout << result1 << endl; + + vector sequence2 = {"X2", "1", "X3", "2", "3", "X2", "4", "X1", "5"}; + int result2 = symbolic_sum(sequence2); + cout << result2 << endl; + + return 0; +} diff --git a/December 20/cpp_karthickk17.cpp b/December 20/cpp_karthickk17.cpp new file mode 100644 index 0000000..615a9cd --- /dev/null +++ b/December 20/cpp_karthickk17.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +string min_dist(map& distances, set& spt_set) { + int n = distances.size(); + string u; + int min = INT_MAX; + for (auto it: distances) + { + if (it.second < min && spt_set.find(it.first) == spt_set.end()) + { + u = it.first; + min = it.second; + } + } + return u; +} + +void dijkstra(map>& graph, map& distances, map& prev_node, set& spt_set) { + int n = graph.size(); + for (int i = 0; i < n-1; i++) + { + string u = min_dist(distances, spt_set); + spt_set.insert(u); + for (auto it:graph[u]) + { + if (spt_set.find(it.first) == spt_set.end() && (distances[u] + it.second < distances[it.first])) + { + distances[it.first] = distances[u] + it.second; + prev_node[it.first] = u; + } + } + } +} + +void print_path(map& prev_node, string end_cave) { + if (end_cave == "") + { + return; + } + string prev_cave = prev_node[end_cave]; + print_path(prev_node, prev_cave); + cout << end_cave <<" "; +} + +int main() { +//--------------------Input----------------------------------- + map> graph = { + {{"Cave_A"}, + { + {{"Cave_B"},{3}}, + {{"Cave_C"}, {7}} + } + }, + {{"Cave_B"}, + { + {{"Cave_D"},{7}}, + {{"Cave_E"}, {1}} + } + }, + {{"Cave_C"}, + { + {{"Cave_D"},{3}}, + } + }, + {{"Cave_D"}, + { + {{"Cave_E"},{5}}, + } + }, + {{"Cave_E"}, + {} + } + }; + string start_cave = "Cave_A"; + string end_cave = "Cave_E"; + int n = graph.size(); +//------------------------------------------------------------ + set spt_set; + map distances { + {{"Cave_A"},{INT_MAX}}, + {{"Cave_B"},{INT_MAX}}, + {{"Cave_C"},{INT_MAX}}, + {{"Cave_D"},{INT_MAX}}, + {{"Cave_E"},{INT_MAX}} + }; + map prev_node { + {{"Cave_A"},{}}, + {{"Cave_B"},{}}, + {{"Cave_C"},{}}, + {{"Cave_D"},{}}, + {{"Cave_E"},{}} + }; + distances[start_cave] = 0; + dijkstra(graph, distances, prev_node, spt_set); +//------------------------------------------------------------ + print_path(prev_node, end_cave); + cout << endl; + return 0; +} \ No newline at end of file diff --git a/December 21/cpp_karthickk17.cpp b/December 21/cpp_karthickk17.cpp new file mode 100644 index 0000000..4b70f0d --- /dev/null +++ b/December 21/cpp_karthickk17.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() { + // The second test case is incorrect. For the plain text to be MADRASHIGHCOURT, the cipher text must be IWZNWODECDYKQNP + // string str = "RQHODQTLATCTQ"; + string str = "IWZNWODECDYKQNP"; + int key = 22; + // cin >> str; + for (int i = 0; i < str.length(); i++) + { + int t = str[i] - 'A'; + t = (t - key) % 26; + if (t < 0) + { + t += 26; + } + char ch = t + 'A'; + str[i] = ch; + } + cout << str << endl; + return 0; +} \ No newline at end of file diff --git a/December 22/cpp_karthickk17.cpp b/December 22/cpp_karthickk17.cpp new file mode 100644 index 0000000..72e673d --- /dev/null +++ b/December 22/cpp_karthickk17.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +//Check if the coordinated entered i.e x and y form a valid pair +bool check(int x, int y, int m, int n) { + if(x >= 0 && y >= 0 && x < m && y < n) { + return true; + } + return false; +} + +//Calculates the time +int calculate_time (vector>& grid) { + int time = 0; + //Queue stores the coordinates of rotten oranges + queue> q; + int m = grid.size(), n = grid[0].size(); + //Initalizing the queue with the rotten oranges coordinates + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 2) + { + q.push({i,j}); + } + } + } + + //In order to iterate through the queue and find the coordinates of the rotten oranges to focus on, we need its count. + int cur_queue_size = q.size(), next_queue_size = 0; + + while (q.size() != 0) + { + + for (int i = 0; i < cur_queue_size; i++) + { + pair coord = q.front(); + q.pop(); + int x = coord.first, y = coord.second; + //Checking up, down, left and right + if (check(x, y, m, n) && check(x-1, y, m, n) && grid[x-1][y] == 1) + { + q.push({x-1,y}); + next_queue_size++; + grid[x-1][y] = 2; + } + if (check(x, y, m, n) && check(x+1, y, m, n) && grid[x+1][y] == 1) + { + q.push({x+1,y}); + next_queue_size++; + grid[x+1][y] = 2; + } + if (check(x, y, m, n) && check(x, y-1, m, n) && grid[x][y-1] == 1) + { + q.push({x,y-1}); + next_queue_size++; + grid[x][y-1] = 2; + } + if (check(x, y, m, n) && check(x, y+1, m, n) && grid[x][y+1] == 1) + { + q.push({x,y+1}); + next_queue_size++; + grid[x][y+1] = 2; + } + } + //If there is no change, then we can exit the loop + if (next_queue_size == 0) + break; + cur_queue_size = next_queue_size; + next_queue_size = 0; + time++; + + } + //To check if any fresh orange or empty cell still exists + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (grid[i][j] == 1) + { + return -1; + } + } + } + return time; +} + +int main() +{ + vector> grid {{2,2,0,1}}; + // Calling the function to calculate the time taken to infect the city + cout << calculate_time(grid) << endl; + return 0; +} \ No newline at end of file diff --git a/December 23/cpp_karthickk17.cpp b/December 23/cpp_karthickk17.cpp new file mode 100644 index 0000000..8aadfb4 --- /dev/null +++ b/December 23/cpp_karthickk17.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main () +{ + int n, upper_sum = 0, lower_sum = 0; + cin >> n; + bool s = false; + for (int i = 0; i < n; i++) + { + int x, y; + cin >> x >> y; + if ((n == 1) && + ((x % 2 != 0) || + (y % 2 != 0))) + { + cout << -1; + return 0; + } + if (y % 2 + x % 2 == 1) + s = true; + upper_sum += x; + lower_sum += y; + } + if (upper_sum % 2 == 0 && lower_sum % 2 == 0) + cout << "0" << endl; + else if (upper_sum % 2 == 1 && lower_sum % 2 == 1 && s) + cout << "1" << endl; + else + cout << "-1" << endl; + return 0; +} \ No newline at end of file diff --git a/December 24/cpp_karthickk17.cpp b/December 24/cpp_karthickk17.cpp new file mode 100644 index 0000000..b9b554c --- /dev/null +++ b/December 24/cpp_karthickk17.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +int inversions = 0; +void merge(int, int, int, vector&); +void merge_sort(int left, int right, vector& arr) { + if(left >= right) + return ; + int mid = (left+right)/2; + merge_sort(left, mid, arr); + merge_sort(mid+1, right, arr); + merge(left, mid, right, arr); +} + +void merge(int left, int mid, int right, vector& arr) { + int i = left, j = mid+1, n = right-left+1; + // cout< merged; + while (i <= mid && j <=right) + { + if (arr[i] < arr[j]) { + merged.push_back(arr[i]); + i++; + } + else { + merged.push_back(arr[j]); + inversions += (mid-i+1); + j++; + } + } + while (i <= mid) { + merged.push_back(arr[i]); + i++; + } + while (j <= right) { + merged.push_back(arr[j]); + j++; + } + for (int p = 0; p < n; ++p) { + arr[left + p] = merged[p]; + } + // for (int i = 0; i < arr.size(); i++) + // { + // cout<& arr, int n) { + int count = 0; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + if (arr[j] < arr[i]) + { + count++; + } + + } + } + cout << count << endl; +} +int main() { + int n; + cin >> n; + vector arr(n); + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + // bruteforce(arr, n); + merge_sort(0, n-1, arr); + cout << inversions << endl; +} \ No newline at end of file diff --git a/December 25/cpp_karthickk17.cpp b/December 25/cpp_karthickk17.cpp new file mode 100644 index 0000000..55cc969 --- /dev/null +++ b/December 25/cpp_karthickk17.cpp @@ -0,0 +1,67 @@ +/* +In this program, I created a graph for the test cases. The edge u-v represents that v is dependent on u. +Also u should be completed before v. The first test case seems to be incorrect. +There are no dependencies for the first task. +Hence it will be completed in time 1. +The second task will be completed in time 2. +Similarly, third in time 3, fourth in time 4 and fifth in time 5. +Hence, the minimum time required to complete the five tasks is 5 time units. +I have checked with another program and it showed the same. +*/ + + +#include + +using namespace std; + +vector minimumTime(int n, map>& graph) +{ + vector ans(n+1, 0); + vector indegree(n+1, 0); + queue q; + for(int i = 1; i <= n; i++) { + for(int j = 0; j < graph[i].size(); j++) { + indegree[graph[i][j]]++; + } + } + for(int i = 1; i <= n; i++) { + if(indegree[i] == 0) { + q.push(i); + ans[i] = 1; + } + } + while(!q.empty()) { + int u = q.front(); + q.pop(); + for(int i:graph[u]) { + indegree[i]--; + if(indegree[i] == 0) { + q.push(i); + ans[i] = ans[u] + 1; + } + } + } + ans.erase(ans.begin()+0); + return ans; +} + +int main() +{ + vector tasks = {1, 2, 3, 4, 5}; + vector> dependencies = {{}, {1}, {2}, {3}, {4,1}}; + map> g; + for (int i = 0; i < dependencies.size(); i++) + { + if(dependencies[i].size() == 0) g[tasks[i]] = {}; + for(int j = 0; j < dependencies[i].size(); j++) + g[dependencies[i][j]].push_back(tasks[i]); + } + int n = tasks.size(); + vector ans = minimumTime(n, g); + // for (int i = 0; i < n; i++) + // { + // cout< +using namespace std; + +struct Node +{ + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; + } +}; + +void loopHere(Node* head, Node* tail, int position) +{ + if(position==0) return; + + Node* walk = head; + for(int i=1; inext; + tail->next = walk; +} + +class Solution +{ + public: + void detectLoop(Node* head) + { + + Node *fast, *slow; + fast = slow = head; + while (fast != NULL && fast->next != NULL) { + fast = fast->next->next; + slow = slow-> next; + if(slow == fast) + break; + } + if(slow != fast) { + cout<<"No Loop detected!" << endl; + return; + } + slow = head; + if (slow == fast) + while (fast->next != slow) + fast = fast->next; + else { + while (slow->next != fast->next) { + slow = slow->next; + fast = fast->next; + } + } + fast->next = NULL; + return; + } +}; + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, num; + cin>>n; + + Node *head, *tail; + cin>> num; + head = tail = new Node(num); + + for(int i=0 ; i> num; + tail->next = new Node(num); + tail = tail->next; + } + + int pos; + cin>> pos; + loopHere(head,tail,pos); + + Solution ob; + ob.detectLoop(head); + } + return 0; +} + diff --git a/December 27/cpp_karthickk17.cpp b/December 27/cpp_karthickk17.cpp new file mode 100644 index 0000000..4953965 --- /dev/null +++ b/December 27/cpp_karthickk17.cpp @@ -0,0 +1,49 @@ +/* +The second test case seems to be incorrect. +The curr_petrol values if we start at 3th petrol bunk is +position: 1 2 3 4 5 +curr_petrol: 1 -1 4 3 5 +As we cant reach back to petrol bunk 3, we cannot form a cycle. Hence the output is -1. +*/ + + +#include +using namespace std; + +int find_start(int n, vector& petrol, vector& distance) +{ + int start = 0; + int end = 1; + int curr_petrol = petrol[start] - distance[start]; + while (end != start || curr_petrol < 0) + { + while (curr_petrol < 0 && start != end) + { + curr_petrol -= petrol[start] - distance[start]; + start = (start + 1) % n; + if (start == 0) + return -1; + } + + curr_petrol += petrol[end] - distance[end]; + end = (end + 1) % n; + } + return start; +} + + +int main() { + int N = 4; + vector Petrol {4, 6, 7, 4}; + vector Distance {6, 5, 3, 5}; + int out = find_start(N, Petrol, Distance); + if(out != -1) out++; + cout << out << endl; + N = 5; + Petrol = {2, 3, 7, 4, 5}; + Distance = {6, 5, 3, 5, 3}; + out = find_start(N, Petrol, Distance); + if(out != -1) out++; + cout << out << endl; + return 0; +} \ No newline at end of file diff --git a/December 28/cpp_karthickk17.cpp b/December 28/cpp_karthickk17.cpp new file mode 100644 index 0000000..2d4f796 --- /dev/null +++ b/December 28/cpp_karthickk17.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +class i { + + public: + int k,r,m,n; + i(int k, int r, int m, int n) { + this->k=k; + this->r=r; + this->m=m; + this->n=n; + } +}; + +class c { + + public: + int k, r; + c(int k, int r) { + this->k=k; + this->r=r; + } +}; + +int main() { + int x = 3; + int z = 3; + i items[] = { + i(10,100,5,110), + i(9,200,2,200), + i(20,200,30,300) + }; + // i items[] = { + // i(8, 150, 10, 160), + // i(5, 180, 12, 200), + // i(20, 250, 15, 300), + // i(15, 300, 18, 250), + + // }; + c clients[] = { + c(5,100), + c(9,500), + c(20,400) + }; + // c clients[] = { + // c(6, 200), + // c(14, 280), + // c(8, 220), + // c(25, 350) + // }; + // for(i item :items) { + // cout<< item.m << " "; + // } + // cout << endl; + sort(items, items+x, [&](i& i1, i& i2) { + return i1.m < i2.m; + }); + sort(clients, clients+z, [&](c& c1, c& c2) { + return c1.k > c2.k; + }); + vector sold(x,false); + int count = 0; + for(c client: clients) { + + for (int i = 0; i < x; i++) + { + if(!sold[i] && items[i].m >= client.k && items[i].n <= client.r) { + sold[i] = true; + count++; + break; + } + } + + } + cout << count << endl; + // for(i item :items) { + // cout<< item.m << " "; + // } + // cout << endl; + return 0; +} \ No newline at end of file diff --git a/December 29/cpp_karthickk17.cpp b/December 29/cpp_karthickk17.cpp new file mode 100644 index 0000000..798f4bb --- /dev/null +++ b/December 29/cpp_karthickk17.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main() { + int cur_x = 0, cur_y = 0, time = 0; + vector walk = {'w', 'e', 'w', 'e', 'w', 'e', 'w', 'e', 'w', 'n'}; + for (int i = 0; i < walk.size(); i++) + { + if (walk[i] == 'n') + { + cur_y++; + time++; + } + else if (walk[i] == 's') + { + cur_y--; + time++; + } + else if (walk[i] == 'e') + { + cur_x++; + time++; + } + else + { + cur_x--; + time++; + } + } + if(!cur_x && !cur_y && time == 10) cout<<"TRUE"; + else cout<<"FALSE"; + return 0; +} \ No newline at end of file diff --git a/December 30/cpp_karthickk17.cpp b/December 30/cpp_karthickk17.cpp new file mode 100644 index 0000000..f26c162 --- /dev/null +++ b/December 30/cpp_karthickk17.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +// Function to compute the shortest path using BFS +vector shortestPath(int n, vector tree[], int start, int end) { + vector distance(n+1, -1); + vector parent(n+1, -1); + + queue q; + q.push(start); + distance[start] = 0; + + while (!q.empty()) { + int current = q.front(); + q.pop(); + + for (int neighbor : tree[current]) { + if (distance[neighbor] == -1) { + distance[neighbor] = distance[current] + 1; + parent[neighbor] = current; + q.push(neighbor); + } + } + } + + // Reconstructing the path + vector path; + int current = end; + + while (current != start) { + path.push_back(current); + current = parent[current]; + } + + path.push_back(start); + reverse(path.begin(), path.end()); + + return path; +} + +int inversions = 0; +void merge(int left, int mid, int right, vector& arr) { + int i = left, j = mid+1, n = right-left+1; + vector merged; + while (i <= mid && j <=right) + { + if (arr[i] <= arr[j]) { + merged.push_back(arr[i]); + i++; + } + else { + merged.push_back(arr[j]); + inversions += (mid-i+1); + j++; + } + } + while (i <= mid) { + merged.push_back(arr[i]); + i++; + } + while (j <= right) { + merged.push_back(arr[j]); + j++; + } + for (int p = 0; p < n; ++p) { + arr[left + p] = merged[p]; + } +} + +void merge_sort(int left, int right, vector& arr) { + if(left >= right) + return ; + int mid = (left+right)/2; + merge_sort(left, mid, arr); + merge_sort(mid+1, right, arr); + merge(left, mid, right, arr); +} + +int main() { + int n; // Number of nodes + int q; // Number of queries + cin >> n; + cin >> q; + + unordered_set nodes; + vector color(n+1); // Colors of each vertices + for (int i = 1; i <= n; i++) + { + cin >> color[i]; + nodes.insert(i); + } + + vector tree[n+1]; + for (int i = 1; i < n; ++i) { + int x, y; + cin >> x >> y; + tree[x].push_back(y); + tree[y].push_back(x); + } + + while(q > 0) + { + int x, y; // Starting and ending nodes of the path + cin >> x >> y; + + vector path = shortestPath(n, tree, x, y); + vector query_path; + for (int node : path) { + // cout << node << " "; + query_path.push_back(color[node]); + } + // cout << endl; + vector query_path_reverse(query_path.begin(), query_path.end()); + reverse(query_path_reverse.begin(), query_path_reverse.end()); + // for (int node : query_path) { + // cout << node << " "; + // } + // cout << endl; + inversions = 0; + merge_sort(0, path.size() - 1, query_path); + int f = inversions; + inversions = 0; + // for (int node : query_path_reverse) { + // cout << node << " "; + // } + // cout << endl; + merge_sort(0, path.size() - 1, query_path_reverse); + f += inversions; + cout << f << endl; + q--; + } + + return 0; +} diff --git a/December 31/cpp_karthickk17.cpp b/December 31/cpp_karthickk17.cpp new file mode 100644 index 0000000..6b81753 --- /dev/null +++ b/December 31/cpp_karthickk17.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +bool isSafe(vector>& board, int row, int col) +{ + int i, j; + int N = board.size(); + for (i = 0; i < col; i++) + if (board[row][i]) + return false; + for (i = row, j = col; i >= 0 && j >= 0; i--, j--) + if (board[i][j]) + return false; + for (i = row, j = col; j >= 0 && i < N; i++, j--) + if (board[i][j]) + return false; + + return true; +} + +bool n_queen(int n, vector>& board, int col, vector>>& ans) { + if(col >= n) { + vector> temp; + for(int i = 0; i < n; i++) { + for(int j = 0; j < n; j++) { + if(board[i][j] == 1) { + temp.push_back({i+1,j+1}); + } + } + } + ans.push_back(temp); + return true; + } + bool res = false; + for (int i = 0; i < n; i++) { + if (isSafe(board, i, col)) { + board[i][col] = 1; + res = n_queen(n, board, col + 1, ans) || res; + board[i][col] = 0; + } + } + return res; +} + +void driver(int n, vector>& board, vector>>& ans) { + + if(!n_queen(n, board, 0, ans)) { + cout << "No Solution found!" << endl; + return; + } + else { + sort(ans.begin(), ans.end()); + for(vector> sol: ans) { + for(pair coord: sol) { + cout<<"("<> board(n, vector(n, 0)); + vector>> ans; + driver(n, board, ans); + return 0; +} \ No newline at end of file