diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..bb4d2cd Binary files /dev/null and b/.DS_Store differ diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..c4376ec --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,34 @@ +name: Codeforces solutions using MkDocs + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the main branch +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + name: Codeforces Solutions + runs-on: ubuntu-latest + steps: + - name: Checkout Main + uses: actions/checkout@v2 + + - name: Set up Python 3.12 + uses: actions/setup-python@v2 + with: + python-version: "3.12" + architecture: x64 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements.txt + + - name: Deploy + run: | + git pull + cd codeforces-solutions + mkdocs gh-deploy --force diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8cab77f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +codeforces-solutions/site/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..31fa019 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "iostream": "cpp", + "ostream": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1005277 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ + **Solutions** diff --git a/codeforces-solutions/.DS_Store b/codeforces-solutions/.DS_Store new file mode 100644 index 0000000..bff6f81 Binary files /dev/null and b/codeforces-solutions/.DS_Store differ diff --git a/codeforces-solutions/docs/.DS_Store b/codeforces-solutions/docs/.DS_Store new file mode 100644 index 0000000..2ef856b Binary files /dev/null and b/codeforces-solutions/docs/.DS_Store differ diff --git a/codeforces-solutions/docs/dp/01knapsack.md b/codeforces-solutions/docs/dp/01knapsack.md new file mode 100644 index 0000000..00cde17 --- /dev/null +++ b/codeforces-solutions/docs/dp/01knapsack.md @@ -0,0 +1,58 @@ +**Approach 1 : Recursion :sweat_smile:** + +=== "Java" + + ```java + ``` + +- [x] **Time Complexity :** O(2^(n\*W)) where n is number of items, and W is the capacity of knapsack + +- [x] **Space Complexity :** O(max(n,W)) where n is number of items, and W is the capacity of knapsack + +
+ +**Approach 2 : Memoization :smile:** + +=== "Java" + + ```java + ``` + +- [x] **Time Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack + +- [x] **Space Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack + +
+ +**Approach 3 : Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + int W = 50; + int n = 3; + int[] p = {60, 100, 120}; + int[] wt = {10, 20, 30}; + + int[][] dp = new int[n + 1][W + 1]; + + for(int i = 0; i <= n; i++){ + for(int j = 0; j <= W; j++){ + if(i == 0 || j == 0) + continue; + else if(wt[i-1] > j) + dp[i][j] = dp[i-1][j]; + else + dp[i][j] = Math.max(dp[i-1][j], (dp[i-1][j-wt[i-1]]+p[i-1])); + } + } + System.out.println("Maximum Profit = " + dp[n][W]); + } + } + ``` + +- [x] **Time Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack + +- [x] **Space Complexity :** O(n\*W) where n is number of items, and W is the capacity of knapsack diff --git a/codeforces-solutions/docs/dp/longestcommonnsubsequence.md b/codeforces-solutions/docs/dp/longestcommonnsubsequence.md new file mode 100644 index 0000000..070519a --- /dev/null +++ b/codeforces-solutions/docs/dp/longestcommonnsubsequence.md @@ -0,0 +1,92 @@ +**Approach 1 : Recursion :sweat_smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + String s1 = "AGGTAB", s2 = "GXTXAYB"; + + int m = s1.length(), n = s2.length(); + int lcs = solve(m-1, n-1, s1, s2); + System.out.println("Length of LCS = " + lcs); + } + + public static int solve(int i, int j, String s1, String s2){ + // base case + if(i < 0 || j < 0) + return 0; + // if the current characters of both strings match, + // then lcs length = 1 + lcs length for remaining part of these strings + if(s1.charAt(i) == s2.charAt(j)) + return 1 + solve(i-1, j-1, s1, s2); + return Math.max(solve(i-1, j, s1, s2), solve(i, j-1, s1, s2)); + } + } + ``` + +- [x] **Time Complexity :** O(2^m\*n^) where m and n are the length of the two strings + +- [x] **Space Complexity :** O(max(m,n)) where m and n are the length of the two strings + +
+ +**Approach 2 : Memoization :smile:** + +=== "Java" + + ```java + ``` + +- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings + +- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings + +
+ +**Approach 3 : Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + String s1 = "ABCDGH", s2 = "AEDFHR"; + + int m = s1.length(), n = s2.length(); + int[][] dp = new int[m + 1][n + 1]; + + for(int i = 0; i < m + 1; i++){ + for(int j = 0; j < n + 1; j++){ + if(i == 0 || j == 0) + dp[i][j] = 0; + else if(s1.charAt(i-1) == s2.charAt(j-1)) + dp[i][j] = 1 + dp[i-1][j-1]; + else + dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); + } + } + int lcs = dp[m][n]; + System.out.println("Length of LCS : " + dp[m][n]); + + char[] str = new char[lcs]; + int i = m, j = n; + while(i > 0 && j > 0){ + if(dp[i-1][j] == dp[i][j]) + i--; + else if(dp[i][j-1] == dp[i][j]) + j--; + else{ + str[--lcs] = s1.charAt(i-1); + i--; + j--; + } + } + System.out.println("LCS is : " + String.valueOf(str)); + } + } + ``` + +- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings + +- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings diff --git a/codeforces-solutions/docs/dp/longestcommonsubstring.md b/codeforces-solutions/docs/dp/longestcommonsubstring.md new file mode 100644 index 0000000..041fef7 --- /dev/null +++ b/codeforces-solutions/docs/dp/longestcommonsubstring.md @@ -0,0 +1,44 @@ +**Approach : Dynamic Programming - Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + String s1 = "abcdaf", s2 = "zbcdf"; + + int m = s1.length(), n = s2.length(); + int[][] dp = new int[m + 1][n + 1]; + int r = -1, c = -1, len = 0; + + for(int i = 1; i <= m; i++){ + for(int j = 1; j <= n; j++){ + if(s1.charAt(i-1) == s2.charAt(j-1)){ + dp[i][j] = 1 + dp[i-1][j-1]; + if(len < dp[i][j]){ + r = i; + c = j; + len = dp[i][j]; + } + } + } + } + System.out.println("Length of longest common substring : " + len); + char[] str = new char[len]; + if(r == -1 && c == -1) + System.out.println("Longest Common Substring does not exist"); + else{ + while(dp[r][c] > 0){ + str[--len] = s1.charAt(r-1); + r--; + c--; + } + System.out.println("Longest Common Substring is : " + String.valueOf(str)); + } + } + } + ``` + +- [x] **Time Complexity :** O(m\*n) where m and n are the length of the two strings + +- [x] **Space Complexity :** O(m\*n) where m and n are the length of the two strings diff --git a/codeforces-solutions/docs/dp/longestincreasingsubsequence.md b/codeforces-solutions/docs/dp/longestincreasingsubsequence.md new file mode 100644 index 0000000..0df3919 --- /dev/null +++ b/codeforces-solutions/docs/dp/longestincreasingsubsequence.md @@ -0,0 +1,27 @@ +**Approach : Dynamic Programming - Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + int[] nums = {10,9,2,5,3,7,101,18}; + int n = nums.length; + int[] dp = new int[n]; + Arrays.fill(dp, 1); + int len = 1; + for(int i = 1; i < n; i++){ + for(int j = 0; j < i; j++){ + if(nums[j] < nums[i] && dp[i] <= dp[j]) + dp[i] = 1 + dp[j]; + } + len = Math.max(len, dp[i]); + } + System.out.println("Length of LIS = " + len); + } + } + ``` + +- [x] **Time Complexity :** O(n^2^) where n is the length of the array + +- [x] **Space Complexity :** O(n) where n is the length of the array diff --git a/codeforces-solutions/docs/dp/longestpalindromicsubsequence.md b/codeforces-solutions/docs/dp/longestpalindromicsubsequence.md new file mode 100644 index 0000000..f10a9d8 --- /dev/null +++ b/codeforces-solutions/docs/dp/longestpalindromicsubsequence.md @@ -0,0 +1,78 @@ +- Basically, the idea is to find length of LCS of a string and it's reverse + +**Approach 1 : Recursion :sweat_smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + String s1 = ""; + String s2 = new StringBuilder(s1).reverse().toString(); + + int n = s1.length(); + int lps = solve(n-1, n-1, s1, s2); + System.out.println("Length of LPS = " + lps); + } + + public static int solve(int i, int j, String s1, String s2){ + // base case + if(i < 0 || j < 0) + return 0; + // if the current characters of both strings match, + // then lcs length = 1 + lcs length for remaining part of these strings + if(s1.charAt(i) == s2.charAt(j)) + return 1 + solve(i-1, j-1, s1, s2); + return Math.max(solve(i-1, j, s1, s2), solve(i, j-1, s1, s2)); + } + } + ``` + +- [x] **Time Complexity :** O(2^n\*n^) where n is the length of the string + +- [x] **Space Complexity :** O(n) where n is the length of the string + +
+ +**Approach 2 : Memoization :smile:** + +=== "Java" + + ```java + ``` + +- [x] **Time Complexity :** O(n^2^) where n is the length of the string + +- [x] **Space Complexity :** O(n^2^) where n is the length of the string + +
+ +**Approach 3 : Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + String s1 = ""; + String s2 = new StringBuilder(s1).reverse().toString(); + + int n = s1.length(); + int[][] dp = new int[n + 1][n + 1]; + + for(int i = 1; i <= n; i++){ + for(int j = 1; j <= n; j++){ + if(s1.charAt(i-1) == s2.charAt(j-1)) + dp[i][j] = 1 + dp[i-1][j-1]; + else + dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); + } + } + System.out.println("Length of LPS = " + dp[n][n]); + } + } + ``` + +- [x] **Time Complexity :** O(n^2^) where n is the length of the string + +- [x] **Space Complexity :** O(n^2^) where n is the length of the string diff --git a/codeforces-solutions/docs/dp/mincoinchange.md b/codeforces-solutions/docs/dp/mincoinchange.md new file mode 100644 index 0000000..8813e07 --- /dev/null +++ b/codeforces-solutions/docs/dp/mincoinchange.md @@ -0,0 +1,69 @@ +**Approach 1 : Recursion :sweat_smile:** + +=== "Java" + + ```java + class Solution{ + public static void main(String[] args) { + int[] coins = {1, 5, 7}; + int n = 18; + int minCoins = solve(n, coins); + System.out.println("To make a change of " + n +", minimum coins needed are " + minCoins); + } + + public static int solve(int n, int[] coins){ + if(n == 0) + return 0; + int minimum = Integer.MAX_VALUE; + for(int i = 0; i < coins.length; i++){ + if(n-coins[i] >= 0){ + int subMin = solve(n-coins[i], coins); + if(subMin != Integer.MAX_VALUE && subMin + 1 < minimum) + minimum = 1 + subMin; + } + } + return minimum; + } + } + ``` + +- [x] **Time Complexity :** O(2^(n\*V)) where n is number of different coins, and V is the change to make + +- [x] **Space Complexity :** O(max(n,V)) where n is number of different coins, and V is the change to make + +
+ +**Approach 3 : Tabulation :smile:** + +=== "Java" + + ```java + class Solution { + public static void main(String[] args) { + int[] coins = {1, 5, 7}; + int n = 14; + int totalCoins = coins.length; + int[] dp = new int[n + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + for(int i = 1; i < dp.length; i++){ + for(int j = 0; j < totalCoins; j++){ + if(coins[j] <= i){ + int min = dp[i-coins[j]]; + if(min != Integer.MAX_VALUE && min + 1 < dp[i]) + dp[i] = min + 1; + } + } + } + int minCoins = dp[n]; + if(minCoins != Integer.MAX_VALUE) + System.out.println("To make a change of " + n +", minimum coins needed are " + minCoins); + else + System.out.println("We cannot make a change of " + n +" using given coins"); + } + } + ``` + +- [x] **Time Complexity :** O(n\*V) where n is number of different coins, and V is the change to make + +- [x] **Space Complexity :** O(n\*V) where n is number of different coins, and V is the change to make diff --git a/codeforces-solutions/docs/images/codeforces.jpeg b/codeforces-solutions/docs/images/codeforces.jpeg new file mode 100644 index 0000000..21008fa Binary files /dev/null and b/codeforces-solutions/docs/images/codeforces.jpeg differ diff --git a/codeforces-solutions/docs/index.md b/codeforces-solutions/docs/index.md new file mode 100644 index 0000000..6101122 --- /dev/null +++ b/codeforces-solutions/docs/index.md @@ -0,0 +1,10 @@ +

Solutions to Codeforces problems

+

+ + Codeforces + +

+ +## Getting Started + +On this website, you will find the solutions to [Codeforces](https://codeforces.com/problemset/) problems in **C++** and **Java** along with time & space complexities. diff --git a/codeforces-solutions/docs/problems/1475/A.md b/codeforces-solutions/docs/problems/1475/A.md new file mode 100644 index 0000000..9920b9a --- /dev/null +++ b/codeforces-solutions/docs/problems/1475/A.md @@ -0,0 +1,57 @@ +--- +tags: + - math + - number theory + - "*900" +--- +# [A. Odd Divisors](https://codeforces.com/problemset/problem/1475/A) + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + + int main() + { + int t; + cin >> t; + while (t-- > 0) + { + long long int n; + cin >> n; + if ((n & (n - 1)) != 0) + cout << "YES" << endl; + else + cout << "NO" << endl; + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + public class Main { + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + while (t-- > 0) { + long n = sc.nextLong(); + if ((n & (n - 1)) != 0) + System.out.println("YES"); + else + System.out.println("NO"); + } + } + } + ``` + +- [X] **Time Complexity :** O(1) +- [X] **Space Complexity :** O(1) diff --git a/codeforces-solutions/docs/problems/1475/B.md b/codeforces-solutions/docs/problems/1475/B.md new file mode 100644 index 0000000..4277d2a --- /dev/null +++ b/codeforces-solutions/docs/problems/1475/B.md @@ -0,0 +1,147 @@ +--- +tags: + - brute force + - dp + - math + - "*900" +--- +# [B. New Year's Number](https://codeforces.com/problemset/problem/1475/B) + +### Approach 1: Bottom-Up Dynamic Programming + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + #define N 1000000 + + void solve(vector`` &dp) + { + dp[0] = true; + for (int i = 2020; i <= N; i++) + { + if (dp[i - 2020] == true || dp[i - 2021] == true) + dp[i] = true; + } + } + + int main() + { + int t; + cin >> t; + vector`` dp(N + 1, false); + solve(dp); + + while (t-- > 0) + { + int n; + cin >> n; + if (dp[n] == true) + cout << "YES" << endl; + else + cout << "NO" << endl; + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + public class Main { + private static final int N = 1000000; + + public static void solve(boolean[] dp) { + dp[0] = true; + for (int i = 2020; i <= N; i++) { + if (dp[i - 2020] == true || dp[i - 2021] == true) + dp[i] = true; + } + } + + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + boolean[] dp = new boolean[N + 1]; + solve(dp); + + while (t-- > 0) { + int n = sc.nextInt(); + if (dp[n] == true) + System.out.println("YES"); + else + System.out.println("NO"); + } + } + } + ``` + +- [X] **Time Complexity :** O(N) where `N` = 106 +- [X] **Space Complexity :** O(N) where `N` = 106 + +
+ +### Approach 2: Mathematical + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + + int main() + { + int t; + cin >> t; + + while (t-- > 0) + { + int n; + cin >> n; + int y = n % 2020; + int x = (n - y) / 2020 - y; + if (x >= 0 && (x * 2020 + y * 2021) == n) + cout << "YES" << endl; + else + cout << "NO" << endl; + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + class Main { + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + + while (t-- > 0) { + int n = sc.nextInt(); + int y = n % 2020; + int x = (n - y) / 2020 - y; + if (x >= 0 && (x * 2020 + y * 2021) == n) + System.out.println("YES"); + else + System.out.println("NO"); + } + } + } + ``` + +- [X] **Time Complexity :** O(t) where `t` is number of testcases +- [X] **Space Complexity :** O(1) diff --git a/codeforces-solutions/docs/problems/1974/A.md b/codeforces-solutions/docs/problems/1974/A.md new file mode 100644 index 0000000..ec9c239 --- /dev/null +++ b/codeforces-solutions/docs/problems/1974/A.md @@ -0,0 +1,81 @@ +--- +tags: + - greedy + - math + - "*800" +--- + +# [A. Phone Desktop](https://codeforces.com/contest/1974/problem/A) + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + + void solve(int x, int y) + { + int two_must = y / 2; + int two_extra = y % 2; + int total = two_must + two_extra; + int one_that_can_fit = 7 * two_must + 11 * two_extra; + if (x > one_that_can_fit) + { + int one_extra = (x - one_that_can_fit); + total += (one_extra / 15) + (one_extra % 15 > 0 ? 1 : 0); + } + cout << total << endl; + return; + } + + int main() + { + int t; + cin >> t; + + while (t-- > 0) + { + int x, y; + cin >> x >> y; + solve(x, y); + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + public class Main { + public static void solve(int x, int y) { + int two_must = y / 2; + int two_extra = y % 2; + int total = two_must + two_extra; + int one_that_can_fit = 7 * two_must + 11 * two_extra; + if (x > one_that_can_fit) { + int one_extra = (x - one_that_can_fit); + total += (one_extra / 15) + (one_extra % 15 > 0 ? 1 : 0); + } + System.out.println(total); + return; + } + + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + while (t-- > 0) { + int x = sc.nextInt(), y = sc.nextInt(); + solve(x, y); + } + } + } + ``` + +- [x] **Time Complexity :** O(t) where `t` is number of testcases +- [x] **Space Complexity :** O(1) diff --git a/codeforces-solutions/docs/problems/1975/A.md b/codeforces-solutions/docs/problems/1975/A.md new file mode 100644 index 0000000..e0aa68c --- /dev/null +++ b/codeforces-solutions/docs/problems/1975/A.md @@ -0,0 +1,123 @@ +--- +tags: + - brute force + - implementation + - sortings +--- +# [A. Bazoka and Mocha's Array](https://codeforces.com/contest/1975/problem/A) + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + + void solve(vector`` &arr){ + int n = arr.size(); + int peakIndex = -1; + for (int i = 0; i < n - 1; i++) + { + if (arr[i] > arr[i + 1]) + { + peakIndex = i; + break; + } + } + if (peakIndex == -1) + { + cout << "Yes" << endl; + } + else + { + bool notPossible = false; + for (int i = peakIndex + 1; i < n; i++) + { + int j = (i + 1) % n; + if (arr[j] < arr[i]) + { + notPossible = true; + break; + } + } + if (notPossible == false) + cout << "Yes" << endl; + else + cout << "No" << endl; + } + } + + int main() + { + int t; + cin >> t; + + while (t-- > 0) + { + int n; + cin >> n; + vector`` arr; + for (int i = 0; i < n; i++) + { + int num; + cin >> num; + arr.push_back(num); + } + solve(arr); + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + public class Main { + public static void solve(int[] arr) { + int n = arr.length; + int peakIndex = -1; + for (int i = 0; i < n - 1; i++) { + if (arr[i] > arr[i + 1]) { + peakIndex = i; + break; + } + } + if (peakIndex == -1) + System.out.println("Yes"); + else { + boolean notPossible = false; + for (int i = peakIndex + 1; i < n; i++) { + int j = (i + 1) % n; + if (arr[j] < arr[i]) { + notPossible = true; + break; + } + } + if (notPossible == false) + System.out.println("Yes"); + else + System.out.println("No"); + } + } + + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + arr[i] = sc.nextInt(); + solve(arr); + } + } + } + ``` + +- [X] **Time Complexity :** O(t \* n) where `t` is number of testcases and `n` is the length of array +- [X] **Space Complexity :** O(1) diff --git a/codeforces-solutions/docs/problems/1975/B.md b/codeforces-solutions/docs/problems/1975/B.md new file mode 100644 index 0000000..6c21f2e --- /dev/null +++ b/codeforces-solutions/docs/problems/1975/B.md @@ -0,0 +1,136 @@ +--- +tags: + - brute force + - greedy + - math + - sortings +--- +# [B. 378QAQ and Mocha's Array](https://codeforces.com/contest/1975/problem/B) + +=== "C++" + + ```c++ + #include + using namespace std; + #define ll long long int + #define ulli unsigned long long int + #define li long int + + int gcd(int a, int b) + { + if (a == 0) + return b; + return gcd(b % a, a); + } + + void solve(vector`` &arr) + { + int n = arr.size(), i = 0, j = -1; + if (arr[i] == 1) + { + cout << "Yes" << endl; + return; + } + for (int k = 1; k < n; k++) + { + if (gcd(arr[i], arr[k]) != arr[i]) + { + j = k; + break; + } + } + if (j == -1) + { + cout << "Yes" << endl; + return; + } + for (int k = 0; k < n; k++) + { + if (!(arr[k] % arr[i] == 0 || arr[k] % arr[j] == 0)) + { + System.out.println("No"); + return; + } + } + cout << "Yes" << endl; + return; + } + + int main() + { + int t; + cin >> t; + + while (t-- > 0) + { + int n; + cin >> n; + vector`` arr; + for (int i = 0; i < n; i++) + { + int num; + cin >> num; + arr.push_back(num); + } + sort(arr.begin(), arr.end()); + solve(arr); + } + return 0; + } + ``` + +=== "Java" + + ```java + import java.util.*; + import java.io.*; + + public class Main { + public static int gcd(int a, int b) { + if (a == 0) + return b; + return gcd(b % a, a); + } + + public static void solve(int[] arr) { + int n = arr.length, i = 0, j = -1; + if (arr[i] == 1) { + System.out.println("Yes"); + return; + } + for (int k = 1; k < n; k++) { + if (gcd(arr[i], arr[k]) != arr[i]) { + j = k; + break; + } + } + if (j == -1) { + System.out.println("Yes"); + return; + } + for (int k = 0; k < n; k++) { + if (!(arr[k] % arr[i] == 0 || arr[k] % arr[j] == 0)) { + System.out.println("No"); + return; + } + } + System.out.println("Yes"); + return; + } + public static void main(String[] args) { + FastReader sc = new FastReader(); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + arr[i] = sc.nextInt(); + Arrays.sort(arr); + solve(arr); + } + } + } + ``` + +- [X] **Time Complexity :** O(t \* n \* logN ) where `t` is number of testcases, `n` is the length of array and `N` = max(`a[i]`) +- [X] **Space Complexity :** O(1) diff --git a/codeforces-solutions/docs/stylesheets/extra.css b/codeforces-solutions/docs/stylesheets/extra.css new file mode 100644 index 0000000..e69de29 diff --git a/codeforces-solutions/docs/tags.md b/codeforces-solutions/docs/tags.md new file mode 100644 index 0000000..6f43a45 --- /dev/null +++ b/codeforces-solutions/docs/tags.md @@ -0,0 +1,5 @@ +# Tags + +Following is a list of tags: + +[TAGS] diff --git a/codeforces-solutions/mkdocs.yml b/codeforces-solutions/mkdocs.yml new file mode 100644 index 0000000..fb94821 --- /dev/null +++ b/codeforces-solutions/mkdocs.yml @@ -0,0 +1,117 @@ +site_name: Codeforces Solutions +site_description: codeforces solutions +site_author: Girish Thatte +site_url: https://girishgr8.github.io + +nav: + - Introduction: index.md + - Standard Problems: + - Dynamic Programming: + - "0/1 Knapsack": dp/01knapsack.md + - "Longest Common Subsequence(LCS)": dp/longestcommonnsubsequence.md + - "Longest Common Substring": dp/longestcommonsubstring.md + - "Longest Increasing Subsequence(LIS)": dp/longestincreasingsubsequence.md + - "Longest Palindromic Subsequence(LPS)": dp/longestpalindromicsubsequence.md + - "Coin Change - minimum number of coins to make the change": dp/mincoinchange.md + + - Codeforces Problems: + - 1475: + - A. Odd Divisor: problems/1475/A.md + - B. New Year's Number: problems/1475/B.md + - 1974: + - A. Phone Desktop: problems/1974/A.md + - 1975: + - A. Bazoka and Mocha's Array: problems/1975/A.md + - B. 378QAQ and Mocha's Array: problems/1975/B.md + +# https://squidfunk.github.io/mkdocs-material/ + +theme: + highlightjs: true + font: + text: Roboto + name: "material" + language: en + include_sidebar: true + #favicon: assets/img/icon.png + #logo: assets/img/logo.png + icon: + repo: fontawesome/brands/git-alt + feature: + tabs: true + # palette: + # primary: "indigo" + # accent: "deep orange" + palette: + # Palette toggle for dark mode + - scheme: slate + primary: "black" + accent: "orange" + toggle: + icon: material/weather-sunny + name: Switch to light mode + # Palette toggle for light mode + - scheme: default + toggle: + icon: material/weather-night + name: Switch to dark mode + i18n: + prev: "Previous" + next: "Next" + features: + - navigation.sections + - navigation.instant + +copyright: "Copyright © 2024 Girish Thatte" + +extra_css: + - stylesheets/extra.css + # - assets/styles/atom-one-light.css # HightlightJS's CSS theme + +extra_javascript: + # - assets/js/hljs/highlight.pack.js # Download from https://highlightjs.org/download/ + # - assets/js/extra.js + - https://polyfill.io/v3/polyfill.min.js?features=es6 + - https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js + +plugins: + - search + - tags: + tags_file: tags.md + +# https://squidfunk.github.io/mkdocs-material/extensions/ +markdown_extensions: + - pymdownx.arithmatex: + generic: true + - attr_list + - admonition + - footnotes + - pymdownx.betterem + - pymdownx.caret + - pymdownx.details + - pymdownx.mark # highlight text + - pymdownx.highlight: + use_pygments: true # hljs is used instead of pygment for TOML highlighting support + linenums: true + linenums_style: pymdownx-inline + anchor_linenums: true + - pymdownx.inlinehilite + - pymdownx.snippets + - pymdownx.superfences + - pymdownx.smartsymbols + - pymdownx.tabbed: + alternate_style: true + - pymdownx.tasklist: + custom_checkbox: true + - def_list + - pymdownx.tilde + - pymdownx.snippets: + check_paths: true + - toc: + permalink: true + - pymdownx.emoji: + emoji_index: !!python/name:material.extensions.emoji.twemoji + emoji_generator: !!python/name:material.extensions.emoji.to_svg + +extra: + disqus: "codeforces-solutions" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0677fde --- /dev/null +++ b/requirements.txt @@ -0,0 +1,29 @@ +Babel==2.15.0 +certifi==2024.6.2 +charset-normalizer==3.3.2 +click==8.1.7 +colorama==0.4.6 +ghp-import==2.1.0 +idna==3.7 +Jinja2==3.1.4 +Markdown==3.6 +MarkupSafe==2.1.5 +mergedeep==1.3.4 +mkdocs==1.6.0 +mkdocs-get-deps==0.2.0 +mkdocs-material==9.5.25 +mkdocs-material-extensions==1.3.1 +packaging==24.0 +paginate==0.5.6 +pathspec==0.12.1 +platformdirs==4.2.2 +Pygments==2.18.0 +pymdown-extensions==10.8.1 +python-dateutil==2.9.0.post0 +PyYAML==6.0.1 +pyyaml_env_tag==0.1 +regex==2024.5.15 +requests==2.32.3 +six==1.16.0 +urllib3==2.2.1 +watchdog==4.0.1