From f6896e3ca3c772f724e7034b30e5f8ed516c0ead Mon Sep 17 00:00:00 2001 From: Karthick K Date: Fri, 1 Dec 2023 21:30:00 +0530 Subject: [PATCH 01/25] Adding first program --- .gitignore | 1 + December 01/.gitignore | 2 ++ December 01/cpp_karthickk17.cpp | 15 +++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 December 01/cpp_karthickk17.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode 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< Date: Sat, 2 Dec 2023 12:55:58 +0530 Subject: [PATCH 02/25] Delete December 02 /.gitignore --- December 02 /.gitignore | 231 ---------------------------------------- 1 file changed, 231 deletions(-) delete mode 100644 December 02 /.gitignore diff --git a/December 02 /.gitignore b/December 02 /.gitignore deleted file mode 100644 index 8e0ca25..0000000 --- a/December 02 /.gitignore +++ /dev/null @@ -1,231 +0,0 @@ -# gitignore file for "A December of Algorithms 2023" -# visit https://github.com/SVCE-ACM/A-December-of-Algorithms -# Written individually to adjust for the needs of the problem - -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# node.js -/node_modules -package-lock.json - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -Pipfile.lock -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# Files and directories created by pub -.dart_tool/ -.packages -build/ -# If you're building an application, you may want to check-in your pubspec.lock -pubspec.lock - -# Directory created by dartdoc -# If you don't generate documentation locally you can remove this line. -doc/api/ - -# Avoid committing generated Javascript files: -*.dart.js -*.info.json # Produced by the --dump-info flag. -*.js # When generated by dart2js. Don't specify *.js if your - # project includes source files written in JavaScript. -*.js_ -*.js.deps -*.js.map - -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf From 5a4504c380dfed0f4b45acd9cf1099bbe5745ba9 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Sat, 2 Dec 2023 14:00:00 +0530 Subject: [PATCH 03/25] Adding second program. --- December 02/.gitignore | 233 ++++++++++++++++++++++++++++++++ December 02/cpp_karthickk17.cpp | 22 +++ 2 files changed, 255 insertions(+) create mode 100644 December 02/.gitignore create mode 100644 December 02/cpp_karthickk17.cpp diff --git a/December 02/.gitignore b/December 02/.gitignore new file mode 100644 index 0000000..5e9a3d7 --- /dev/null +++ b/December 02/.gitignore @@ -0,0 +1,233 @@ +# gitignore file for "A December of Algorithms 2023" +# visit https://github.com/SVCE-ACM/A-December-of-Algorithms +# Written individually to adjust for the needs of the problem + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# node.js +/node_modules +package-lock.json + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +Pipfile.lock +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ + +# Avoid committing generated Javascript files: +*.dart.js +*.info.json # Produced by the --dump-info flag. +*.js # When generated by dart2js. Don't specify *.js if your + # project includes source files written in JavaScript. +*.js_ +*.js.deps +*.js.map + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +.vscode \ No newline at end of file diff --git a/December 02/cpp_karthickk17.cpp b/December 02/cpp_karthickk17.cpp new file mode 100644 index 0000000..93028c4 --- /dev/null +++ b/December 02/cpp_karthickk17.cpp @@ -0,0 +1,22 @@ +#include + +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< Date: Sat, 2 Dec 2023 14:06:43 +0530 Subject: [PATCH 04/25] Small edit --- December 02/cpp_karthickk17.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/December 02/cpp_karthickk17.cpp b/December 02/cpp_karthickk17.cpp index 93028c4..261ff57 100644 --- a/December 02/cpp_karthickk17.cpp +++ b/December 02/cpp_karthickk17.cpp @@ -13,7 +13,8 @@ int main() { } for(int i:arr){ if(count[i] > 0) { - cout< Date: Sun, 3 Dec 2023 09:25:29 +0530 Subject: [PATCH 05/25] Adding third program --- December 03/cpp_karthickk17.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 December 03/cpp_karthickk17.cpp diff --git a/December 03/cpp_karthickk17.cpp b/December 03/cpp_karthickk17.cpp new file mode 100644 index 0000000..f5b8f4d --- /dev/null +++ b/December 03/cpp_karthickk17.cpp @@ -0,0 +1,28 @@ +#include + +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 Date: Mon, 4 Dec 2023 09:20:02 +0530 Subject: [PATCH 06/25] Adding fourth program --- December 04/cpp_karthickk17.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 December 04/cpp_karthickk17.cpp diff --git a/December 04/cpp_karthickk17.cpp b/December 04/cpp_karthickk17.cpp new file mode 100644 index 0000000..565c0a8 --- /dev/null +++ b/December 04/cpp_karthickk17.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() { + string str; + cin>>str; + int n = str.length(); + if(n == 1) cout<<"Error"; + else if(n % 2 == 0){ + if(tolower(str[n/2]) == tolower(str[(n/2)-1])) + cout< Date: Mon, 4 Dec 2023 09:29:16 +0530 Subject: [PATCH 07/25] Adding comment --- December 04/cpp_karthickk17.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/December 04/cpp_karthickk17.cpp b/December 04/cpp_karthickk17.cpp index 565c0a8..b4d71b3 100644 --- a/December 04/cpp_karthickk17.cpp +++ b/December 04/cpp_karthickk17.cpp @@ -7,6 +7,8 @@ int main() { 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< Date: Tue, 5 Dec 2023 09:25:41 +0530 Subject: [PATCH 08/25] Adding fifth program --- December 05/cpp_karthickk17.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 December 05/cpp_karthickk17.cpp diff --git a/December 05/cpp_karthickk17.cpp b/December 05/cpp_karthickk17.cpp new file mode 100644 index 0000000..937a608 --- /dev/null +++ b/December 05/cpp_karthickk17.cpp @@ -0,0 +1,19 @@ +#include + +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 doesn'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 60. + avg=accumulate(arr.begin(),arr.end(),0)/n; + sort(arr.begin(),arr.end()); + cout< Date: Wed, 6 Dec 2023 17:12:53 +0530 Subject: [PATCH 09/25] Adding sixth program --- .gitignore | 1 + December 06/cpp_karthickk17.cpp | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 December 06/cpp_karthickk17.cpp diff --git a/.gitignore b/.gitignore index 722d5e7..b42e836 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +.txt \ No newline at end of file diff --git a/December 06/cpp_karthickk17.cpp b/December 06/cpp_karthickk17.cpp new file mode 100644 index 0000000..ead480e --- /dev/null +++ b/December 06/cpp_karthickk17.cpp @@ -0,0 +1,61 @@ +#include + +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< Date: Thu, 7 Dec 2023 09:29:05 +0530 Subject: [PATCH 10/25] Adding seventh program --- December 07/cpp_karthickk17.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 December 07/cpp_karthickk17.cpp diff --git a/December 07/cpp_karthickk17.cpp b/December 07/cpp_karthickk17.cpp new file mode 100644 index 0000000..69eb5b5 --- /dev/null +++ b/December 07/cpp_karthickk17.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +int main() { + int width,height,radius; + cin>>width>>height>>radius; + float diagonal = sqrt(pow(width,2)+pow(height,2)); + cout< Date: Thu, 7 Dec 2023 09:31:05 +0530 Subject: [PATCH 11/25] Adding comment --- December 07/cpp_karthickk17.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/December 07/cpp_karthickk17.cpp b/December 07/cpp_karthickk17.cpp index 69eb5b5..eb0a4fe 100644 --- a/December 07/cpp_karthickk17.cpp +++ b/December 07/cpp_karthickk17.cpp @@ -6,6 +6,6 @@ int main() { int width,height,radius; cin>>width>>height>>radius; float diagonal = sqrt(pow(width,2)+pow(height,2)); - cout< Date: Fri, 8 Dec 2023 09:12:11 +0530 Subject: [PATCH 12/25] Adding eigth program --- December 08/cpp_karthickk17.cpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 December 08/cpp_karthickk17.cpp diff --git a/December 08/cpp_karthickk17.cpp b/December 08/cpp_karthickk17.cpp new file mode 100644 index 0000000..9084ebf --- /dev/null +++ b/December 08/cpp_karthickk17.cpp @@ -0,0 +1,45 @@ +#include + +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; +} From da27f37da9aeb7eed614b9fde26f0ab71aab39e5 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Fri, 8 Dec 2023 09:15:54 +0530 Subject: [PATCH 13/25] Adding eighth program --- December 08/cpp_karthickk17.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/December 08/cpp_karthickk17.cpp b/December 08/cpp_karthickk17.cpp index 9084ebf..f96ed71 100644 --- a/December 08/cpp_karthickk17.cpp +++ b/December 08/cpp_karthickk17.cpp @@ -36,7 +36,7 @@ int main() { 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 + // Displaying the number with proper spacing. cout << setw(maxDigits) << num << " "; } cout << endl; From 257b833ce4e54e6b149d2a7bf3e6937ce65cad14 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Sat, 9 Dec 2023 09:23:41 +0530 Subject: [PATCH 14/25] Adding ninth program --- December 09/cpp_karthickk17.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 December 09/cpp_karthickk17.cpp 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< Date: Sun, 10 Dec 2023 13:04:28 +0530 Subject: [PATCH 15/25] Adding tenth program --- December 10/cpp_karthickk17.cpp | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 December 10/cpp_karthickk17.cpp diff --git a/December 10/cpp_karthickk17.cpp b/December 10/cpp_karthickk17.cpp new file mode 100644 index 0000000..a61257a --- /dev/null +++ b/December 10/cpp_karthickk17.cpp @@ -0,0 +1,38 @@ +#include + +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< Date: Mon, 11 Dec 2023 09:54:26 +0530 Subject: [PATCH 16/25] adding eleventh program --- December 11/cpp_karthickk17.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 December 11/cpp_karthickk17.cpp diff --git a/December 11/cpp_karthickk17.cpp b/December 11/cpp_karthickk17.cpp new file mode 100644 index 0000000..606d16d --- /dev/null +++ b/December 11/cpp_karthickk17.cpp @@ -0,0 +1,25 @@ +#include + +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; +} From 218c9c5aaf873ecc0c274d66ec3cc62c9df724bf Mon Sep 17 00:00:00 2001 From: Karthick K Date: Tue, 12 Dec 2023 10:12:40 +0530 Subject: [PATCH 17/25] Adding twelfth program --- December 12/cpp_karthickk17.cpp | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 December 12/cpp_karthickk17.cpp 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; +} From 47c59ed635f1f6049071b66baa7858177b0a2b45 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Wed, 13 Dec 2023 10:31:07 +0530 Subject: [PATCH 18/25] adding thirteenth program --- December 13/cpp_karthickk17.cpp | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 December 13/cpp_karthickk17.cpp 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; +} From 80a82fcc0aaeade6ed604e1c5562c22ce193f0e5 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Wed, 20 Dec 2023 20:00:44 +0530 Subject: [PATCH 19/25] Adding program --- December 14/cpp_karthickk17.cpp | 183 ++++++++++++++++++++++++++++++++ December 15/cpp_karthickk17.cpp | 27 +++++ December 16/cpp_karthickk17.cpp | 101 ++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 December 14/cpp_karthickk17.cpp create mode 100644 December 15/cpp_karthickk17.cpp create mode 100644 December 16/cpp_karthickk17.cpp diff --git a/December 14/cpp_karthickk17.cpp b/December 14/cpp_karthickk17.cpp new file mode 100644 index 0000000..a4ca5be --- /dev/null +++ b/December 14/cpp_karthickk17.cpp @@ -0,0 +1,183 @@ +//{ Driver Code Starts +#include +using namespace std; + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; +}; + +// Utility function to create a new Tree Node +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + + +// } Driver Code Ends +/* A binary Tree node +struct Node +{ + int data; + struct Node *left, *right; +}; +*/ + +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) { + // return the sorted vector of all nodes at k dist + 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; + } + +}; + +//{ Driver Code Starts. + +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..b6999d2 --- /dev/null +++ b/December 16/cpp_karthickk17.cpp @@ -0,0 +1,101 @@ +#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 n, int m) { + 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-1][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() +{ + 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 From 7e8788211b1079a07076db8c2b9b11531c42bba3 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Wed, 20 Dec 2023 22:18:55 +0530 Subject: [PATCH 20/25] change comment --- December 05/cpp_karthickk17.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/December 05/cpp_karthickk17.cpp b/December 05/cpp_karthickk17.cpp index 937a608..f8da5c7 100644 --- a/December 05/cpp_karthickk17.cpp +++ b/December 05/cpp_karthickk17.cpp @@ -9,8 +9,8 @@ int main() { int avg; for (int i = 0; i < n; i++) cin>>arr[i]; - //The test cases doesn'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 60. + //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< Date: Wed, 20 Dec 2023 22:21:38 +0530 Subject: [PATCH 21/25] change comment --- December 16/cpp_karthickk17.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/December 16/cpp_karthickk17.cpp b/December 16/cpp_karthickk17.cpp index b6999d2..f9ae526 100644 --- a/December 16/cpp_karthickk17.cpp +++ b/December 16/cpp_karthickk17.cpp @@ -89,6 +89,7 @@ int calculate_time (vector>& grid) { 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}, From 01f11f0a1571307d6ba0cd80a69b88ba5934315a Mon Sep 17 00:00:00 2001 From: Karthick K Date: Thu, 21 Dec 2023 09:12:46 +0530 Subject: [PATCH 22/25] adding program --- December 21/cpp_karthickk17.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 December 21/cpp_karthickk17.cpp 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 From ebad481adc29a0335fe6b33d21810e30986362e7 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Fri, 22 Dec 2023 11:47:27 +0530 Subject: [PATCH 23/25] adding program --- .gitignore | 2 +- December 17/cpp_karthickk17.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 December 17/cpp_karthickk17.cpp diff --git a/.gitignore b/.gitignore index b42e836..564d3fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .vscode -.txt \ No newline at end of file +*.txt \ 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..1471aed --- /dev/null +++ b/December 17/cpp_karthickk17.cpp @@ -0,0 +1,29 @@ +void detectAndRemoveLoop(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; +} + From e883fc2c7980d4759be2435242c2267e79f530a9 Mon Sep 17 00:00:00 2001 From: Karthick K Date: Sun, 31 Dec 2023 13:40:21 +0530 Subject: [PATCH 24/25] Adding program --- December 16/cpp_karthickk17.cpp | 4 +- December 18/cpp_karthickk17.cpp | 66 ++++++++++++++++ December 19/cpp_karthickk17.cpp | 42 ++++++++++ December 20/cpp_karthickk17.cpp | 100 ++++++++++++++++++++++++ December 22/cpp_karthickk17.cpp | 96 +++++++++++++++++++++++ December 23/cpp_karthickk17.cpp | 32 ++++++++ December 24/cpp_karthickk17.cpp | 77 ++++++++++++++++++ December 25/cpp_karthickk17.cpp | 67 ++++++++++++++++ December 26/cpp_karthickk17.cpp | 17 ++++ December 27/cpp_karthickk17.cpp | 49 ++++++++++++ December 28/cpp_karthickk17.cpp | 85 ++++++++++++++++++++ December 29/cpp_karthickk17.cpp | 33 ++++++++ December 30/cpp_karthickk17.cpp | 134 ++++++++++++++++++++++++++++++++ December 31/cpp_karthickk17.cpp | 70 +++++++++++++++++ 14 files changed, 870 insertions(+), 2 deletions(-) create mode 100644 December 18/cpp_karthickk17.cpp create mode 100644 December 19/cpp_karthickk17.cpp create mode 100644 December 20/cpp_karthickk17.cpp create mode 100644 December 22/cpp_karthickk17.cpp create mode 100644 December 23/cpp_karthickk17.cpp create mode 100644 December 24/cpp_karthickk17.cpp create mode 100644 December 25/cpp_karthickk17.cpp create mode 100644 December 26/cpp_karthickk17.cpp create mode 100644 December 27/cpp_karthickk17.cpp create mode 100644 December 28/cpp_karthickk17.cpp create mode 100644 December 29/cpp_karthickk17.cpp create mode 100644 December 30/cpp_karthickk17.cpp create mode 100644 December 31/cpp_karthickk17.cpp diff --git a/December 16/cpp_karthickk17.cpp b/December 16/cpp_karthickk17.cpp index f9ae526..1657908 100644 --- a/December 16/cpp_karthickk17.cpp +++ b/December 16/cpp_karthickk17.cpp @@ -3,7 +3,7 @@ using namespace std; //Check if the coordinated entered i.e x and y form a valid pair -bool check(int x, int y, int n, int m) { +bool check(int x, int y, int m, int n) { if(x >= 0 && y >= 0 && x < m && y < n) { return true; } @@ -58,7 +58,7 @@ int calculate_time (vector>& grid) { next_queue_size++; grid[x][y-1] = 1; } - if (check(x, y, m, n) && check(x, y+1, m, n) && grid[x-1][y+1] == 0) + 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++; diff --git a/December 18/cpp_karthickk17.cpp b/December 18/cpp_karthickk17.cpp new file mode 100644 index 0000000..0ff33e0 --- /dev/null +++ b/December 18/cpp_karthickk17.cpp @@ -0,0 +1,66 @@ +#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() +{ + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/December 18/input.txt", "r", stdin); + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/December 18/output.txt", "w", stdout); + 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 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..9057a56 --- /dev/null +++ b/December 24/cpp_karthickk17.cpp @@ -0,0 +1,77 @@ +#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() { + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/input.txt", "r", stdin); + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/output.txt", "w", stdout); + 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<next != NULL) { + fast = fast->next->next; + slow = slow-> next; + if(slow == fast) + break; + } + if(slow != fast) { + cout<<"No Cycle Found" << endl; + return; + } + cout << "Cycle Found" << endl; +} + 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..690b4ac --- /dev/null +++ b/December 28/cpp_karthickk17.cpp @@ -0,0 +1,85 @@ +#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() { + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/input.txt", "r", stdin); + freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/output.txt", "w", stdout); + 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 From 6ca59582a0dd4f98a9b1d707ea85a99c63b5b44c Mon Sep 17 00:00:00 2001 From: Karthick K Date: Sun, 31 Dec 2023 13:57:46 +0530 Subject: [PATCH 25/25] Adding program --- December 14/cpp_karthickk17.cpp | 36 +------ December 17/cpp_karthickk17.cpp | 175 ++++++++++++++++++++++++++++---- December 18/cpp_karthickk17.cpp | 2 - December 24/cpp_karthickk17.cpp | 2 - December 26/cpp_karthickk17.cpp | 93 +++++++++++++++-- December 28/cpp_karthickk17.cpp | 2 - 6 files changed, 238 insertions(+), 72 deletions(-) diff --git a/December 14/cpp_karthickk17.cpp b/December 14/cpp_karthickk17.cpp index a4ca5be..ef4d807 100644 --- a/December 14/cpp_karthickk17.cpp +++ b/December 14/cpp_karthickk17.cpp @@ -1,8 +1,6 @@ -//{ Driver Code Starts #include using namespace std; -// Tree Node struct Node { int data; @@ -10,7 +8,6 @@ struct Node Node* right; }; -// Utility function to create a new Tree Node Node* newNode(int val) { Node* temp = new Node; @@ -21,62 +18,48 @@ Node* newNode(int val) return temp; } -// Function to Build Tree Node* buildTree(string str) { - // Corner Case + if(str.length() == 0 || str[0] == 'N') return NULL; - - // Creating vector of strings from input - // string after spliting by space + vector ip; istringstream iss(str); for(string str; iss >> str; ) ip.push_back(str); - // Create the root of the tree Node* root = newNode(stoi(ip[0])); - // Push the root to the queue queue queue; queue.push(root); - // Starting from the second element int i = 1; while(!queue.empty() && i < ip.size()) { - // Get and remove the front of the queue + Node* currNode = queue.front(); queue.pop(); - // Get the current node's value from the string string currVal = ip[i]; - // If the left child is not null if(currVal != "N") { - // Create the left child for the current node currNode->left = newNode(stoi(currVal)); - // Push it to the queue queue.push(currNode->left); } - // For the right child i++; if(i >= ip.size()) break; currVal = ip[i]; - // If the right child is not null if(currVal != "N") { - // Create the right child for the current node currNode->right = newNode(stoi(currVal)); - // Push it to the queue queue.push(currNode->right); } i++; @@ -85,16 +68,6 @@ Node* buildTree(string str) return root; } - -// } Driver Code Ends -/* A binary Tree node -struct Node -{ - int data; - struct Node *left, *right; -}; -*/ - class Solution { private: @@ -115,7 +88,6 @@ class Solution } int helper(Node* root, int target, int k) { - // return the sorted vector of all nodes at k dist if(root == NULL) return -1; if(root->data == target) @@ -152,8 +124,6 @@ class Solution }; -//{ Driver Code Starts. - int main() { diff --git a/December 17/cpp_karthickk17.cpp b/December 17/cpp_karthickk17.cpp index 1471aed..da62f05 100644 --- a/December 17/cpp_karthickk17.cpp +++ b/December 17/cpp_karthickk17.cpp @@ -1,29 +1,160 @@ -void detectAndRemoveLoop(Node* head) +#include +using namespace std; + +struct Node { + int data; + Node* next; - Node *fast, *slow; - fast = slow = head; - while (fast != NULL && fast->next != NULL) { - fast = fast->next->next; - slow = slow-> next; - if(slow == fast) - break; + Node(int val) + { + data = val; + next = NULL; } - if(slow != fast) { - cout<<"No Loop detected!" << endl; - return; +}; + +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; } - slow = head; - if (slow == fast) - while (fast->next != slow) - fast = fast->next; - else { - while (slow->next != fast->next) { - slow = slow->next; - fast = fast->next; - } + + return true; +} + +int length(Node* head) +{ + int ret = 0; + while(head) + { + ret++; + head = head->next; } - fast->next = NULL; - return; + 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 index 0ff33e0..cc72290 100644 --- a/December 18/cpp_karthickk17.cpp +++ b/December 18/cpp_karthickk17.cpp @@ -29,8 +29,6 @@ int nCr(int n, int r) { int main() { - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/December 18/input.txt", "r", stdin); - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/December 18/output.txt", "w", stdout); int t; cin >> t; while(t--) { diff --git a/December 24/cpp_karthickk17.cpp b/December 24/cpp_karthickk17.cpp index 9057a56..b9b554c 100644 --- a/December 24/cpp_karthickk17.cpp +++ b/December 24/cpp_karthickk17.cpp @@ -62,8 +62,6 @@ void bruteforce(vector& arr, int n) { cout << count << endl; } int main() { - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/input.txt", "r", stdin); - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/output.txt", "w", stdout); int n; cin >> n; vector arr(n); diff --git a/December 26/cpp_karthickk17.cpp b/December 26/cpp_karthickk17.cpp index 2967461..dfdb39f 100644 --- a/December 26/cpp_karthickk17.cpp +++ b/December 26/cpp_karthickk17.cpp @@ -1,17 +1,88 @@ -void detectLoop(Node* head) +#include +using namespace std; + +struct Node { - Node *fast, *slow; - fast = slow = head; - while (fast != NULL && fast->next != NULL) { - fast = fast->next->next; - slow = slow-> next; - if(slow == fast) - break; + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; } - if(slow != fast) { - cout<<"No Cycle Found" << endl; +}; + +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; } - cout << "Cycle Found" << endl; +}; + +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 28/cpp_karthickk17.cpp b/December 28/cpp_karthickk17.cpp index 690b4ac..2d4f796 100644 --- a/December 28/cpp_karthickk17.cpp +++ b/December 28/cpp_karthickk17.cpp @@ -25,8 +25,6 @@ class c { }; int main() { - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/input.txt", "r", stdin); - freopen("C:/Users/karth/OneDrive/Desktop/December of Algorithms/A-December-Of-Algorithms-2023/output.txt", "w", stdout); int x = 3; int z = 3; i items[] = {