| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 1, "input": "a = 5, b = 4", "output": "0.0722389", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ Program to find the biggest Reuleaux triangle\\n// inscribed within in a square which in turn\\n// is inscribed within an ellipse\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to find the biggest reuleaux triangle\\nfloat Area(float a, float b)\\n{\\n\\n // length of the axes cannot be negative\\n if (a < 0 && b < 0)\\n return -1;\\n\\n // height of the reuleaux triangle\\n float h = sqrt(((pow(a, 2) + pow(b, 2))\\n / (pow(a, 2) * pow(b, 2))));\\n\\n // area of the reuleaux triangle\\n float A = 0.70477 * pow(h, 2);\\n\\n return A;\\n}\\n\\n// Driver code\\nint main()\\n{\\n float a = 5, b = 4;\\n cout << Area(a, b) << endl;\\n\\n return 0;\\n}", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 1, "input": "a = 5, b = 4", "output": "0.0722389", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java Program to find the biggest Reuleaux triangle\\n// inscribed within in a square which in turn\\n// is inscribed within an ellipse\\nimport java.io.*;\\n\\nclass InfobayAI \\n{\\n \\n// Function to find the biggest reuleaux triangle\\nstatic float Area(float a, float b)\\n{\\n\\n // length of the axes cannot be negative\\n if (a < 0 && b < 0)\\n return -1;\\n\\n // height of the reuleaux triangle\\n float h = (float)Math.sqrt(((Math.pow(a, 2) + Math.pow(b, 2))\\n / (Math.pow(a, 2) * Math.pow(b, 2))));\\n\\n // area of the reuleaux triangle\\n float A = (float)(0.70477 * Math.pow(h, 2));\\n\\n return A;\\n}\\n\\n// Driver code\\npublic static void main (String[] args)\\n{\\n float a = 5, b = 4;\\n System.out.println(Area(a, b));\\n}\\n}\\n\\n", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 1, "input": "a = 5, b = 4", "output": "0.0722389", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 Program to find the biggest Reuleaux \\n# triangle inscribed within in a square\\n# which in turn is inscribed within an ellipse \\nimport math;\\n\\n# Function to find the biggest \\n# reuleaux triangle \\ndef Area(a, b):\\n\\n # length of the axes cannot \\n # be negative \\n if (a < 0 and b < 0): \\n return -1; \\n\\n # height of the reuleaux triangle \\n h = math.sqrt(((pow(a, 2) + pow(b, 2)) /\\n (pow(a, 2) * pow(b, 2)))); \\n\\n # area of the reuleaux triangle \\n A = 0.70477 * pow(h, 2); \\n\\n return A; \\n\\n# Driver code \\na = 5;\\nb = 4; \\nprint(round(Area(a, b), 7));\\n\\n", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 2, "input": "a = 7, b = 11", "output": "0.0202076", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ Program to find the biggest Reuleaux triangle\\n// inscribed within in a square which in turn\\n// is inscribed within an ellipse\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to find the biggest reuleaux triangle\\nfloat Area(float a, float b)\\n{\\n\\n // length of the axes cannot be negative\\n if (a < 0 && b < 0)\\n return -1;\\n\\n // height of the reuleaux triangle\\n float h = sqrt(((pow(a, 2) + pow(b, 2))\\n / (pow(a, 2) * pow(b, 2))));\\n\\n // area of the reuleaux triangle\\n float A = 0.70477 * pow(h, 2);\\n\\n return A;\\n}\\n\\n// Driver code\\nint main()\\n{\\n float a = 5, b = 4;\\n cout << Area(a, b) << endl;\\n\\n return 0;\\n}", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 2, "input": "a = 7, b = 11", "output": "0.0202076", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java Program to find the biggest Reuleaux triangle\\n// inscribed within in a square which in turn\\n// is inscribed within an ellipse\\nimport java.io.*;\\n\\nclass InfobayAI \\n{\\n \\n// Function to find the biggest reuleaux triangle\\nstatic float Area(float a, float b)\\n{\\n\\n // length of the axes cannot be negative\\n if (a < 0 && b < 0)\\n return -1;\\n\\n // height of the reuleaux triangle\\n float h = (float)Math.sqrt(((Math.pow(a, 2) + Math.pow(b, 2))\\n / (Math.pow(a, 2) * Math.pow(b, 2))));\\n\\n // area of the reuleaux triangle\\n float A = (float)(0.70477 * Math.pow(h, 2));\\n\\n return A;\\n}\\n\\n// Driver code\\npublic static void main (String[] args)\\n{\\n float a = 5, b = 4;\\n System.out.println(Area(a, b));\\n}\\n}\\n\\n", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 1, "problem_slug": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse", "title": "Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse", "problem_statement": "Given an ellipse with major axis length and minor axis 2a & 2b respectively which inscribes a square which in turn inscribes a reuleaux triangle. The task is to find the maximum possible area of this reuleaux triangle.", "sample_sno": 2, "input": "a = 7, b = 11", "output": "0.0202076", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 Program to find the biggest Reuleaux \\n# triangle inscribed within in a square\\n# which in turn is inscribed within an ellipse \\nimport math;\\n\\n# Function to find the biggest \\n# reuleaux triangle \\ndef Area(a, b):\\n\\n # length of the axes cannot \\n # be negative \\n if (a < 0 and b < 0): \\n return -1; \\n\\n # height of the reuleaux triangle \\n h = math.sqrt(((pow(a, 2) + pow(b, 2)) /\\n (pow(a, 2) * pow(b, 2)))); \\n\\n # area of the reuleaux triangle \\n A = 0.70477 * pow(h, 2); \\n\\n return A; \\n\\n# Driver code \\na = 5;\\nb = 4; \\nprint(round(Area(a, b), 7));\\n\\n", "source_json_file": "biggest-reuleaux-triangle-inscribed-within-a-square-which-is-inscribed-within-an-ellipse.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 1, "input": "a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}", "output": "Yes\nThe king cannot make any move as it has been check mate.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for checking if a king\\n// can move a valid move or not when\\n// N nights are there in a modified chessboard\\n#include <bits/stdc++.h>\\nusing namespace std;\\nbool checkCheckMate(pair<int, int> a[], int n, int kx, int ky)\\n{\\n\\n // Pair of hash to mark the coordinates\\n map<pair<int, int>, int> mpp;\\n\\n // iterate for Given N knights\\n for (int i = 0; i < n; i++) {\\n int x = a[i].first;\\n int y = a[i].second;\\n\\n // mark all the \"L\" shaped coordinates\\n // that can be reached by a Knight\\n\\n // initial position\\n mpp[{ x, y }] = 1;\\n\\n // 1-st move\\n mpp[{ x - 2, y + 1 }] = 1;\\n\\n // 2-nd move\\n mpp[{ x - 2, y - 1 }] = 1;\\n\\n // 3-rd move\\n mpp[{ x + 1, y + 2 }] = 1;\\n\\n // 4-th move\\n mpp[{ x + 1, y - 2 }] = 1;\\n\\n // 5-th move\\n mpp[{ x - 1, y + 2 }] = 1;\\n\\n // 6-th move\\n mpp[{ x + 2, y + 1 }] = 1;\\n\\n // 7-th move\\n mpp[{ x + 2, y - 1 }] = 1;\\n\\n // 8-th move\\n mpp[{ x - 1, y - 2 }] = 1;\\n }\\n\\n // iterate for all possible 8 coordinates\\n for (int i = -1; i < 2; i++) {\\n for (int j = -1; j < 2; j++) {\\n int nx = kx + i;\\n int ny = ky + j;\\n if (i != 0 && j != 0) {\\n\\n // check a move can be made or not\\n if (!mpp[{ nx, ny }]) {\\n return true;\\n }\\n }\\n }\\n }\\n\\n // any moves\\n return false;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n pair<int, int> a[] = { { 1, 0 }, { 0, 2 }, { 2, 5 }, \\n { 4, 4 }, { 5, 0 }, { 6, 2 }};\\n\\n int n = sizeof(a) / sizeof(a[0]);\\n\\n int x = 3, y = 2;\\n if (checkCheckMate(a, n, x, y))\\n cout << \"Not Checkmate!\";\\n else\\n cout << \"Yes its checkmate!\";\\n\\n return 0;\\n}", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 1, "input": "a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}", "output": "Yes\nThe king cannot make any move as it has been check mate.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for checking if a king\\n// can move a valid move or not when\\n// N nights are there in a modified chessboard\\nimport java.util.*;\\n\\nclass InfobayAI \\n{\\nstatic class pair\\n{ \\n int first, second; \\n public pair(int first, int second) \\n { \\n this.first = first; \\n this.second = second; \\n } \\n} \\n\\nstatic boolean checkCheckMate(pair a[], int n,\\n int kx, int ky)\\n{\\n\\n // Pair of hash to mark the coordinates\\n HashMap<pair, \\n Integer> mpp = new HashMap<pair, \\n Integer>();\\n\\n // iterate for Given N knights\\n for (int i = 0; i < n; i++) \\n {\\n int x = a[i].first;\\n int y = a[i].second;\\n\\n // mark all the \"L\" shaped coordinates\\n // that can be reached by a Knight\\n\\n // initial position\\n mpp.put(new pair( x, y ), 1);\\n\\n // 1-st move\\n mpp.put(new pair( x - 2, y + 1 ), 1);\\n\\n // 2-nd move\\n mpp.put(new pair( x - 2, y - 1 ), 1);\\n\\n // 3-rd move\\n mpp.put(new pair( x + 1, y + 2 ), 1);\\n\\n // 4-th move\\n mpp.put(new pair( x + 1, y - 2 ), 1);\\n\\n // 5-th move\\n mpp.put(new pair( x - 1, y + 2 ), 1);\\n\\n // 6-th move\\n mpp.put(new pair( x + 2, y + 1 ), 1);\\n\\n // 7-th move\\n mpp.put(new pair( x + 2, y - 1 ), 1);\\n\\n // 8-th move\\n mpp.put(new pair( x - 1, y - 2 ), 1);\\n }\\n\\n // iterate for all possible 8 coordinates\\n for (int i = -1; i < 2; i++) \\n {\\n for (int j = -1; j < 2; j++) \\n {\\n int nx = kx + i;\\n int ny = ky + j;\\n if (i != 0 && j != 0)\\n {\\n\\n // check a move can be made or not\\n pair p =new pair(nx, ny );\\n if (mpp.get(p) != null)\\n {\\n return true;\\n }\\n }\\n }\\n }\\n\\n // any moves\\n return false;\\n}\\n\\n// Driver Code\\npublic static void main(String[] args) \\n{\\n pair a[] = {new pair( 1, 0 ), new pair( 0, 2 ), \\n new pair( 2, 5 ), new pair( 4, 4 ), \\n new pair( 5, 0 ), new pair( 6, 2 )};\\n\\n int n = a.length;\\n\\n int x = 3, y = 2;\\n if (checkCheckMate(a, n, x, y))\\n System.out.println(\"Not Checkmate!\");\\n else\\n System.out.println(\"Yes its checkmate!\");\\n }\\n}\\n\\n", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 1, "input": "a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}", "output": "Yes\nThe king cannot make any move as it has been check mate.", "solution_sno": 3, "solution_name": "sol3", "language": "python3", "code": "# Python3 program for checking if a king \\n# can move a valid move or not when \\n# N nights are there in a modified chessboard \\n\\ndef checkCheckMate(a, n, kx, ky): \\n\\n # Pair of hash to mark the coordinates \\n mpp = {} \\n\\n # iterate for Given N knights \\n for i in range(0, n): \\n x = a[i][0] \\n y = a[i][1] \\n\\n # mark all the \"L\" shaped coordinates \\n # that can be reached by a Knight \\n\\n # initial position \\n mpp[(x, y)] = 1\\n\\n # 1-st move \\n mpp[(x - 2, y + 1)] = 1\\n\\n # 2-nd move \\n mpp[(x - 2, y - 1)] = 1\\n\\n # 3-rd move \\n mpp[(x + 1, y + 2)] = 1\\n\\n # 4-th move \\n mpp[(x + 1, y - 2)] = 1\\n\\n # 5-th move \\n mpp[(x - 1, y + 2)] = 1\\n\\n # 6-th move \\n mpp[(x + 2, y + 1)] = 1\\n\\n # 7-th move \\n mpp[(x + 2, y - 1)] = 1\\n\\n # 8-th move \\n mpp[(x - 1, y - 2)] = 1\\n \\n # iterate for all possible 8 coordinates \\n for i in range(-1, 2): \\n for j in range(-1, 2): \\n nx = kx + i \\n ny = ky + j \\n \\n if i != 0 and j != 0: \\n \\n # check a move can be made or not \\n if not mpp[(nx, ny)]: \\n return True\\n \\n # any moves \\n return False\\n\\n# Driver Code \\nif __name__ == \"__main__\": \\n\\n a = [[1, 0], [0, 2], [2, 5], \\n [4, 4], [5, 0], [6, 2]] \\n\\n n = len(a) \\n x, y = 3, 2\\n \\n if checkCheckMate(a, n, x, y): \\n print(\"Not Checkmate!\") \\n else:\\n print(\"Yes its checkmate!\")\\n\\n", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 2, "input": "a[] = { {1, 1} } king -> {3, 4}", "output": "No\nThe king can make valid moves.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for checking if a king\\n// can move a valid move or not when\\n// N nights are there in a modified chessboard\\n#include <bits/stdc++.h>\\nusing namespace std;\\nbool checkCheckMate(pair<int, int> a[], int n, int kx, int ky)\\n{\\n\\n // Pair of hash to mark the coordinates\\n map<pair<int, int>, int> mpp;\\n\\n // iterate for Given N knights\\n for (int i = 0; i < n; i++) {\\n int x = a[i].first;\\n int y = a[i].second;\\n\\n // mark all the \"L\" shaped coordinates\\n // that can be reached by a Knight\\n\\n // initial position\\n mpp[{ x, y }] = 1;\\n\\n // 1-st move\\n mpp[{ x - 2, y + 1 }] = 1;\\n\\n // 2-nd move\\n mpp[{ x - 2, y - 1 }] = 1;\\n\\n // 3-rd move\\n mpp[{ x + 1, y + 2 }] = 1;\\n\\n // 4-th move\\n mpp[{ x + 1, y - 2 }] = 1;\\n\\n // 5-th move\\n mpp[{ x - 1, y + 2 }] = 1;\\n\\n // 6-th move\\n mpp[{ x + 2, y + 1 }] = 1;\\n\\n // 7-th move\\n mpp[{ x + 2, y - 1 }] = 1;\\n\\n // 8-th move\\n mpp[{ x - 1, y - 2 }] = 1;\\n }\\n\\n // iterate for all possible 8 coordinates\\n for (int i = -1; i < 2; i++) {\\n for (int j = -1; j < 2; j++) {\\n int nx = kx + i;\\n int ny = ky + j;\\n if (i != 0 && j != 0) {\\n\\n // check a move can be made or not\\n if (!mpp[{ nx, ny }]) {\\n return true;\\n }\\n }\\n }\\n }\\n\\n // any moves\\n return false;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n pair<int, int> a[] = { { 1, 0 }, { 0, 2 }, { 2, 5 }, \\n { 4, 4 }, { 5, 0 }, { 6, 2 }};\\n\\n int n = sizeof(a) / sizeof(a[0]);\\n\\n int x = 3, y = 2;\\n if (checkCheckMate(a, n, x, y))\\n cout << \"Not Checkmate!\";\\n else\\n cout << \"Yes its checkmate!\";\\n\\n return 0;\\n}", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 2, "input": "a[] = { {1, 1} } king -> {3, 4}", "output": "No\nThe king can make valid moves.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for checking if a king\\n// can move a valid move or not when\\n// N nights are there in a modified chessboard\\nimport java.util.*;\\n\\nclass InfobayAI \\n{\\nstatic class pair\\n{ \\n int first, second; \\n public pair(int first, int second) \\n { \\n this.first = first; \\n this.second = second; \\n } \\n} \\n\\nstatic boolean checkCheckMate(pair a[], int n,\\n int kx, int ky)\\n{\\n\\n // Pair of hash to mark the coordinates\\n HashMap<pair, \\n Integer> mpp = new HashMap<pair, \\n Integer>();\\n\\n // iterate for Given N knights\\n for (int i = 0; i < n; i++) \\n {\\n int x = a[i].first;\\n int y = a[i].second;\\n\\n // mark all the \"L\" shaped coordinates\\n // that can be reached by a Knight\\n\\n // initial position\\n mpp.put(new pair( x, y ), 1);\\n\\n // 1-st move\\n mpp.put(new pair( x - 2, y + 1 ), 1);\\n\\n // 2-nd move\\n mpp.put(new pair( x - 2, y - 1 ), 1);\\n\\n // 3-rd move\\n mpp.put(new pair( x + 1, y + 2 ), 1);\\n\\n // 4-th move\\n mpp.put(new pair( x + 1, y - 2 ), 1);\\n\\n // 5-th move\\n mpp.put(new pair( x - 1, y + 2 ), 1);\\n\\n // 6-th move\\n mpp.put(new pair( x + 2, y + 1 ), 1);\\n\\n // 7-th move\\n mpp.put(new pair( x + 2, y - 1 ), 1);\\n\\n // 8-th move\\n mpp.put(new pair( x - 1, y - 2 ), 1);\\n }\\n\\n // iterate for all possible 8 coordinates\\n for (int i = -1; i < 2; i++) \\n {\\n for (int j = -1; j < 2; j++) \\n {\\n int nx = kx + i;\\n int ny = ky + j;\\n if (i != 0 && j != 0)\\n {\\n\\n // check a move can be made or not\\n pair p =new pair(nx, ny );\\n if (mpp.get(p) != null)\\n {\\n return true;\\n }\\n }\\n }\\n }\\n\\n // any moves\\n return false;\\n}\\n\\n// Driver Code\\npublic static void main(String[] args) \\n{\\n pair a[] = {new pair( 1, 0 ), new pair( 0, 2 ), \\n new pair( 2, 5 ), new pair( 4, 4 ), \\n new pair( 5, 0 ), new pair( 6, 2 )};\\n\\n int n = a.length;\\n\\n int x = 3, y = 2;\\n if (checkCheckMate(a, n, x, y))\\n System.out.println(\"Not Checkmate!\");\\n else\\n System.out.println(\"Yes its checkmate!\");\\n }\\n}\\n\\n", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 2, "problem_slug": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard", "title": "Check if a king can move a valid move or not when N nights are there in a modified chessboard", "problem_statement": "Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king's coordinate, the task is to check if the King is checkmate or not.", "sample_sno": 2, "input": "a[] = { {1, 1} } king -> {3, 4}", "output": "No\nThe king can make valid moves.", "solution_sno": 3, "solution_name": "sol3", "language": "python3", "code": "# Python3 program for checking if a king \\n# can move a valid move or not when \\n# N nights are there in a modified chessboard \\n\\ndef checkCheckMate(a, n, kx, ky): \\n\\n # Pair of hash to mark the coordinates \\n mpp = {} \\n\\n # iterate for Given N knights \\n for i in range(0, n): \\n x = a[i][0] \\n y = a[i][1] \\n\\n # mark all the \"L\" shaped coordinates \\n # that can be reached by a Knight \\n\\n # initial position \\n mpp[(x, y)] = 1\\n\\n # 1-st move \\n mpp[(x - 2, y + 1)] = 1\\n\\n # 2-nd move \\n mpp[(x - 2, y - 1)] = 1\\n\\n # 3-rd move \\n mpp[(x + 1, y + 2)] = 1\\n\\n # 4-th move \\n mpp[(x + 1, y - 2)] = 1\\n\\n # 5-th move \\n mpp[(x - 1, y + 2)] = 1\\n\\n # 6-th move \\n mpp[(x + 2, y + 1)] = 1\\n\\n # 7-th move \\n mpp[(x + 2, y - 1)] = 1\\n\\n # 8-th move \\n mpp[(x - 1, y - 2)] = 1\\n \\n # iterate for all possible 8 coordinates \\n for i in range(-1, 2): \\n for j in range(-1, 2): \\n nx = kx + i \\n ny = ky + j \\n \\n if i != 0 and j != 0: \\n \\n # check a move can be made or not \\n if not mpp[(nx, ny)]: \\n return True\\n \\n # any moves \\n return False\\n\\n# Driver Code \\nif __name__ == \"__main__\": \\n\\n a = [[1, 0], [0, 2], [2, 5], \\n [4, 4], [5, 0], [6, 2]] \\n\\n n = len(a) \\n x, y = 3, 2\\n \\n if checkCheckMate(a, n, x, y): \\n print(\"Not Checkmate!\") \\n else:\\n print(\"Yes its checkmate!\")\\n\\n", "source_json_file": "check-if-a-king-can-move-a-valid-move-or-not-when-n-nights-are-there-in-a-modified-chessboard.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 1, "input": "A[] = {1, 2, 3, 4, 5}, M = 3", "output": "Yes\n?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.\nWe can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where\ngcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.\nThus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ code to implement the approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to find gcd of two numbers\\nint gcd(int a, int b)\\n{\\n if (b == 0)\\n {\\n return a;\\n }\\n return gcd(b, a % b);\\n}\\n\\n// Function to find check whether\\n// non-overlapping subarray exists\\nstring find(int arr[], int n, int m)\\n{\\n int G = 0;\\n int g = 0;\\n int count = 0;\\n for (int i = 0; i < n; i++)\\n {\\n G = gcd(G, arr[i]);\\n }\\n for (int i = 0; i < n; i++)\\n {\\n\\n // arr[i] = sc.nextInt();\\n g = gcd(g, arr[i]);\\n if (g == G)\\n {\\n count++;\\n g = 0;\\n }\\n }\\n if (count >= m)\\n return \"Yes\";\\n return \"No\";\\n}\\n\\n// Driver code\\nint main()\\n{\\n int A[] = {1, 2, 3, 4, 5};\\n int N = sizeof(A) / sizeof(A[0]);\\n int K = 3;\\n\\n // Function call\\n cout << (find(A, N, K));\\n}\\n\\n", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 1, "input": "A[] = {1, 2, 3, 4, 5}, M = 3", "output": "Yes\n?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.\nWe can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where\ngcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.\nThus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java code to implement the approach\\n\\nimport java.io.*;\\nimport java.util.*;\\n\\npublic class InfobayAI {\\n\\n // Function to find gcd of two numbers\\n public static int gcd(int a, int b)\\n {\\n if (b == 0) {\\n return a;\\n }\\n return gcd(b, a % b);\\n }\\n\\n // Function to find check whether\\n // non-overlapping subarray exists\\n static String find(int arr[], int n, int m)\\n {\\n int G = 0;\\n int g = 0;\\n int count = 0;\\n for (int i = 0; i < n; i++) {\\n G = gcd(G, arr[i]);\\n }\\n for (int i = 0; i < n; i++) {\\n\\n // arr[i] = sc.nextInt();\\n g = gcd(g, arr[i]);\\n if (g == G) {\\n count++;\\n g = 0;\\n }\\n }\\n if (count >= m)\\n return \"Yes\";\\n return \"No\";\\n }\\n\\n // Driver code\\n public static void main(String[] args)\\n {\\n int A[] = { 1, 2, 3, 4, 5 };\\n int N = A.length;\\n int K = 3;\\n\\n // Function call\\n System.out.println(find(A, N, K));\\n }\\n}", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 1, "input": "A[] = {1, 2, 3, 4, 5}, M = 3", "output": "Yes\n?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.\nWe can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where\ngcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.\nThus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python code to implement the approach\\n\\n# Function to find gcd of two numbers\\ndef gcd(a, b):\\n if (b == 0):\\n return a\\n return gcd(b, a % b)\\n\\n# Function to find check whether\\n# non-overlapping subarray exists\\ndef find(arr, n, m):\\n G = 0\\n g = 0\\n count = 0\\n for i in range(n):\\n G = gcd(G, arr[i])\\n\\n for i in range(n):\\n g = gcd(g, arr[i])\\n if (g == G):\\n count += 1\\n g = 0\\n\\n if (count >= m):\\n return \"Yes\"\\n return \"No\"\\n\\n# Driver code\\nif __name__ == \"__main__\":\\n A = [1, 2, 3, 4, 5]\\n N = 5\\n K = 3\\n \\n # Function call\\n print(find(A, N, K))\\n\\n", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 2, "input": "A[] = {6, 12, 18, 24}", "output": "No", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ code to implement the approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to find gcd of two numbers\\nint gcd(int a, int b)\\n{\\n if (b == 0)\\n {\\n return a;\\n }\\n return gcd(b, a % b);\\n}\\n\\n// Function to find check whether\\n// non-overlapping subarray exists\\nstring find(int arr[], int n, int m)\\n{\\n int G = 0;\\n int g = 0;\\n int count = 0;\\n for (int i = 0; i < n; i++)\\n {\\n G = gcd(G, arr[i]);\\n }\\n for (int i = 0; i < n; i++)\\n {\\n\\n // arr[i] = sc.nextInt();\\n g = gcd(g, arr[i]);\\n if (g == G)\\n {\\n count++;\\n g = 0;\\n }\\n }\\n if (count >= m)\\n return \"Yes\";\\n return \"No\";\\n}\\n\\n// Driver code\\nint main()\\n{\\n int A[] = {1, 2, 3, 4, 5};\\n int N = sizeof(A) / sizeof(A[0]);\\n int K = 3;\\n\\n // Function call\\n cout << (find(A, N, K));\\n}\\n\\n", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 2, "input": "A[] = {6, 12, 18, 24}", "output": "No", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java code to implement the approach\\n\\nimport java.io.*;\\nimport java.util.*;\\n\\npublic class InfobayAI {\\n\\n // Function to find gcd of two numbers\\n public static int gcd(int a, int b)\\n {\\n if (b == 0) {\\n return a;\\n }\\n return gcd(b, a % b);\\n }\\n\\n // Function to find check whether\\n // non-overlapping subarray exists\\n static String find(int arr[], int n, int m)\\n {\\n int G = 0;\\n int g = 0;\\n int count = 0;\\n for (int i = 0; i < n; i++) {\\n G = gcd(G, arr[i]);\\n }\\n for (int i = 0; i < n; i++) {\\n\\n // arr[i] = sc.nextInt();\\n g = gcd(g, arr[i]);\\n if (g == G) {\\n count++;\\n g = 0;\\n }\\n }\\n if (count >= m)\\n return \"Yes\";\\n return \"No\";\\n }\\n\\n // Driver code\\n public static void main(String[] args)\\n {\\n int A[] = { 1, 2, 3, 4, 5 };\\n int N = A.length;\\n int K = 3;\\n\\n // Function call\\n System.out.println(find(A, N, K));\\n }\\n}", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 3, "problem_slug": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g", "title": "Check if Array has at least M non-overlapping Subarray with gcd G", "problem_statement": "Given an array A[] and M, the task is to check whether there exist M non-overlapping subarrays(non-empty) of A for which the average of the GCD of those subarrays equals G where G denotes the gcd of all the numbers in the array A.", "sample_sno": 2, "input": "A[] = {6, 12, 18, 24}", "output": "No", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python code to implement the approach\\n\\n# Function to find gcd of two numbers\\ndef gcd(a, b):\\n if (b == 0):\\n return a\\n return gcd(b, a % b)\\n\\n# Function to find check whether\\n# non-overlapping subarray exists\\ndef find(arr, n, m):\\n G = 0\\n g = 0\\n count = 0\\n for i in range(n):\\n G = gcd(G, arr[i])\\n\\n for i in range(n):\\n g = gcd(g, arr[i])\\n if (g == G):\\n count += 1\\n g = 0\\n\\n if (count >= m):\\n return \"Yes\"\\n return \"No\"\\n\\n# Driver code\\nif __name__ == \"__main__\":\\n A = [1, 2, 3, 4, 5]\\n N = 5\\n K = 3\\n \\n # Function call\\n print(find(A, N, K))\\n\\n", "source_json_file": "check-if-array-has-at-least-m-non-overlapping-subarray-with-gcd-g.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 1, "input": "arr[] = {10, 2, 10, 5}, X = 3, K = 3", "output": "1\nExplanation:\nXOR of the given array = 7. The only possible array satisfying the condition is {10, 2, 10, 8}, obtained by incrementing the last array element thrice.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for the above approach\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Stores the final answer\\nint ans = 0;\\n\\n// Utility function to count arrays\\n// having at least K elements exceeding\\n// XOR of all given array elements\\nvoid countArraysUtil(vector<int>& arr,\\n int X, int K,\\n int xorVal)\\n{\\n // If no operations are left\\n if (X == 0) {\\n\\n // Stores the count of\\n // possible arrays\\n int cnt = 0;\\n\\n // Count array elements are\\n // greater than XOR\\n for (int i = 0; i < arr.size(); i++) {\\n\\n if (arr[i] > xorVal)\\n cnt++;\\n }\\n if (cnt >= K)\\n ans++;\\n return;\\n }\\n \\n // Stores first element\\n int temp = arr[0];\\n\\n // Delete first element\\n arr.erase(arr.begin());\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Insert first element into vector\\n arr.insert(arr.begin(), temp);\\n\\n // Stores the last element\\n temp = arr.back();\\n\\n // Remove last element from vector\\n arr.pop_back();\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Push last element into vector\\n arr.push_back(temp);\\n\\n // Increment first element\\n arr[0]++;\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement first element\\n arr[0]--;\\n\\n // Increment last element\\n arr[arr.size() - 1]++;\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement last element\\n arr[arr.size() - 1]--;\\n}\\n\\n// Function to find the count of \\n// arrays having atleast K elements \\n// greater than XOR of array\\nvoid countArrays(vector<int>& arr, \\n int X, int K)\\n{\\n // Stores the XOR value\\n// of original array\\n int xorVal = 0;\\n\\n // Traverse the vector\\n for (int i = 0; i < arr.size(); i++)\\n xorVal = xorVal ^ arr[i];\\n\\n countArraysUtil(arr, X, K, xorVal);\\n\\n // Print the answer\\n cout << ans;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n // Given vector\\n vector<int> arr = { 10, 2, 10, 5 };\\n\\n // Given value of X & K\\n int X = 3, K = 3;\\n\\n countArrays(arr, X, K);\\n\\n return 0;\\n}", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 1, "input": "arr[] = {10, 2, 10, 5}, X = 3, K = 3", "output": "1\nExplanation:\nXOR of the given array = 7. The only possible array satisfying the condition is {10, 2, 10, 8}, obtained by incrementing the last array element thrice.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.util.ArrayList;\\nclass InfobayAI{\\n\\n// Stores the final answer\\nstatic int ans = 0;\\n\\n// Utility function to count arrays\\n// having at least K elements exceeding\\n// XOR of all given array elements\\npublic static void countArraysUtil(ArrayList<Integer> arr,\\n int X, int K,\\n int xorVal)\\n{\\n // If no operations are left\\n if (X == 0) {\\n\\n // Stores the count of\\n // possible arrays\\n int cnt = 0;\\n\\n // Count array elements are\\n // greater than XOR\\n for (int i = 0; i < arr.size(); i++) {\\n\\n if (arr.get(i) > xorVal)\\n cnt++;\\n }\\n if (cnt >= K)\\n ans++;\\n return;\\n }\\n \\n // Stores first element\\n int temp = arr.get(0);\\n\\n // Delete first element\\n arr.remove(0);\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Insert first element into vector\\n arr.add(0, temp);\\n\\n // Stores the last element\\n temp = arr.get(arr.size() - 1);\\n\\n // Remove last element from vector\\n arr.remove(arr.size() - 1);\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Push last element into vector\\n arr.add(temp);\\n\\n // Increment first element\\n arr.set(0, arr.get(0) + 1);\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement first element\\n arr.set(0, arr.get(0) - 1);\\n\\n\\n // Increment last element\\n arr.set(arr.size() - 1, arr.get(arr.size() - 1) + 1);\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement last element\\n arr.set(arr.size() - 1, arr.get(arr.size() - 1) - 1);\\n\\n}\\n\\n// Function to find the count of \\n// arrays having atleast K elements \\n// greater than XOR of array\\npublic static void countArrays(ArrayList<Integer> arr, \\n int X, int K)\\n{\\n // Stores the XOR value\\n// of original array\\n int xorVal = 0;\\n\\n // Traverse the vector\\n for (int i = 0; i < arr.size(); i++)\\n xorVal = xorVal ^ arr.get(i);\\n\\n countArraysUtil(arr, X, K, xorVal);\\n\\n // Print the answer\\n System.out.println(ans);\\n}\\n\\n// Driver Code\\npublic static void main(String arg[])\\n{\\n \\n // Given vector\\n int[] input = {10, 2, 10, 5};\\n \\n // Convert the input as ArrayList\\n ArrayList<Integer> arr = new ArrayList<Integer>();\\n\\n for(int i : input){\\n arr.add(i);\\n }\\n \\n\\n // Given value of X & K\\n int X = 3, K = 3;\\n\\n countArrays(arr, X, K);\\n}\\n}\\n\\n", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 1, "input": "arr[] = {10, 2, 10, 5}, X = 3, K = 3", "output": "1\nExplanation:\nXOR of the given array = 7. The only possible array satisfying the condition is {10, 2, 10, 8}, obtained by incrementing the last array element thrice.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python program for the above approach\\n\\n# Stores the final answer\\nans = 0\\n\\n# Utility function to count arrays\\n# having at least K elements exceeding\\n# XOR of all given array elements\\ndef countArraysUtil( arr, X, K, xorVal):\\n global ans\\n \\n # If no operations are left\\n if (X == 0):\\n \\n # Stores the count of\\n # possible arrays\\n cnt = 0\\n\\n # Count array elements are\\n # greater than XOR\\n for i in range(len(arr)):\\n if (arr[i] > xorVal):\\n cnt += 1\\n if (cnt >= K):\\n ans += 1\\n return\\n \\n # Stores first element\\n temp = arr[0]\\n\\n # Delete first element\\n arr.pop(0)\\n\\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n\\n # Insert first element into vector\\n arr.insert(0, temp)\\n\\n # Stores the last element\\n temp = arr[-1]\\n\\n # Remove last element from vector\\n arr.pop()\\n\\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n\\n # Push last element into vector\\n arr.append(temp)\\n\\n # Increment first element\\n arr[0] += 1\\n \\n # Recursive call\\n countArraysUtil(arr, X - 1,K, xorVal)\\n \\n # Decrement first element\\n arr[0] -= 1\\n\\n # Increment last element\\n arr[len(arr) - 1] += 1\\n \\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n \\n # Decrement last element\\n arr[len(arr) - 1] -= 1\\n\\n# Function to find the count of \\n# arrays having atleast K elements \\n# greater than XOR of array\\ndef countArrays(arr, X, K):\\n \\n # Stores the XOR value\\n # of original array\\n xorVal = 0\\n\\n # Traverse the vector\\n for i in range(len(arr)):\\n xorVal = xorVal ^ arr[i]\\n countArraysUtil(arr, X, K, xorVal)\\n\\n # Print the answer\\n print(ans)\\n \\n# Driver Code\\n# Given vector\\narr = [ 10, 2, 10, 5 ]\\n\\n# Given value of X & K\\nX = 3\\nK = 3\\ncountArrays(arr, X, K)\\n\\n", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 2, "input": "arr[] = {3, 3, 4}, X = 3, K = 2", "output": "3", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for the above approach\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Stores the final answer\\nint ans = 0;\\n\\n// Utility function to count arrays\\n// having at least K elements exceeding\\n// XOR of all given array elements\\nvoid countArraysUtil(vector<int>& arr,\\n int X, int K,\\n int xorVal)\\n{\\n // If no operations are left\\n if (X == 0) {\\n\\n // Stores the count of\\n // possible arrays\\n int cnt = 0;\\n\\n // Count array elements are\\n // greater than XOR\\n for (int i = 0; i < arr.size(); i++) {\\n\\n if (arr[i] > xorVal)\\n cnt++;\\n }\\n if (cnt >= K)\\n ans++;\\n return;\\n }\\n \\n // Stores first element\\n int temp = arr[0];\\n\\n // Delete first element\\n arr.erase(arr.begin());\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Insert first element into vector\\n arr.insert(arr.begin(), temp);\\n\\n // Stores the last element\\n temp = arr.back();\\n\\n // Remove last element from vector\\n arr.pop_back();\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Push last element into vector\\n arr.push_back(temp);\\n\\n // Increment first element\\n arr[0]++;\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement first element\\n arr[0]--;\\n\\n // Increment last element\\n arr[arr.size() - 1]++;\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement last element\\n arr[arr.size() - 1]--;\\n}\\n\\n// Function to find the count of \\n// arrays having atleast K elements \\n// greater than XOR of array\\nvoid countArrays(vector<int>& arr, \\n int X, int K)\\n{\\n // Stores the XOR value\\n// of original array\\n int xorVal = 0;\\n\\n // Traverse the vector\\n for (int i = 0; i < arr.size(); i++)\\n xorVal = xorVal ^ arr[i];\\n\\n countArraysUtil(arr, X, K, xorVal);\\n\\n // Print the answer\\n cout << ans;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n // Given vector\\n vector<int> arr = { 10, 2, 10, 5 };\\n\\n // Given value of X & K\\n int X = 3, K = 3;\\n\\n countArrays(arr, X, K);\\n\\n return 0;\\n}", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 2, "input": "arr[] = {3, 3, 4}, X = 3, K = 2", "output": "3", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.util.ArrayList;\\nclass InfobayAI{\\n\\n// Stores the final answer\\nstatic int ans = 0;\\n\\n// Utility function to count arrays\\n// having at least K elements exceeding\\n// XOR of all given array elements\\npublic static void countArraysUtil(ArrayList<Integer> arr,\\n int X, int K,\\n int xorVal)\\n{\\n // If no operations are left\\n if (X == 0) {\\n\\n // Stores the count of\\n // possible arrays\\n int cnt = 0;\\n\\n // Count array elements are\\n // greater than XOR\\n for (int i = 0; i < arr.size(); i++) {\\n\\n if (arr.get(i) > xorVal)\\n cnt++;\\n }\\n if (cnt >= K)\\n ans++;\\n return;\\n }\\n \\n // Stores first element\\n int temp = arr.get(0);\\n\\n // Delete first element\\n arr.remove(0);\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Insert first element into vector\\n arr.add(0, temp);\\n\\n // Stores the last element\\n temp = arr.get(arr.size() - 1);\\n\\n // Remove last element from vector\\n arr.remove(arr.size() - 1);\\n\\n // Recursive call\\n countArraysUtil(arr, X - 1, \\n K, xorVal);\\n\\n // Push last element into vector\\n arr.add(temp);\\n\\n // Increment first element\\n arr.set(0, arr.get(0) + 1);\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement first element\\n arr.set(0, arr.get(0) - 1);\\n\\n\\n // Increment last element\\n arr.set(arr.size() - 1, arr.get(arr.size() - 1) + 1);\\n \\n // Recursive call\\n countArraysUtil(arr, X - 1,\\n K, xorVal);\\n \\n // Decrement last element\\n arr.set(arr.size() - 1, arr.get(arr.size() - 1) - 1);\\n\\n}\\n\\n// Function to find the count of \\n// arrays having atleast K elements \\n// greater than XOR of array\\npublic static void countArrays(ArrayList<Integer> arr, \\n int X, int K)\\n{\\n // Stores the XOR value\\n// of original array\\n int xorVal = 0;\\n\\n // Traverse the vector\\n for (int i = 0; i < arr.size(); i++)\\n xorVal = xorVal ^ arr.get(i);\\n\\n countArraysUtil(arr, X, K, xorVal);\\n\\n // Print the answer\\n System.out.println(ans);\\n}\\n\\n// Driver Code\\npublic static void main(String arg[])\\n{\\n \\n // Given vector\\n int[] input = {10, 2, 10, 5};\\n \\n // Convert the input as ArrayList\\n ArrayList<Integer> arr = new ArrayList<Integer>();\\n\\n for(int i : input){\\n arr.add(i);\\n }\\n \\n\\n // Given value of X & K\\n int X = 3, K = 3;\\n\\n countArrays(arr, X, K);\\n}\\n}\\n\\n", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 4, "problem_slug": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations", "title": "Count arrays having at least K elements exceeding XOR of all given array elements by X given operations", "problem_statement": "Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.\nSelect either first or last element from the given array.\nEither increment the selected element by 1 or delete the selected element.", "sample_sno": 2, "input": "arr[] = {3, 3, 4}, X = 3, K = 2", "output": "3", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python program for the above approach\\n\\n# Stores the final answer\\nans = 0\\n\\n# Utility function to count arrays\\n# having at least K elements exceeding\\n# XOR of all given array elements\\ndef countArraysUtil( arr, X, K, xorVal):\\n global ans\\n \\n # If no operations are left\\n if (X == 0):\\n \\n # Stores the count of\\n # possible arrays\\n cnt = 0\\n\\n # Count array elements are\\n # greater than XOR\\n for i in range(len(arr)):\\n if (arr[i] > xorVal):\\n cnt += 1\\n if (cnt >= K):\\n ans += 1\\n return\\n \\n # Stores first element\\n temp = arr[0]\\n\\n # Delete first element\\n arr.pop(0)\\n\\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n\\n # Insert first element into vector\\n arr.insert(0, temp)\\n\\n # Stores the last element\\n temp = arr[-1]\\n\\n # Remove last element from vector\\n arr.pop()\\n\\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n\\n # Push last element into vector\\n arr.append(temp)\\n\\n # Increment first element\\n arr[0] += 1\\n \\n # Recursive call\\n countArraysUtil(arr, X - 1,K, xorVal)\\n \\n # Decrement first element\\n arr[0] -= 1\\n\\n # Increment last element\\n arr[len(arr) - 1] += 1\\n \\n # Recursive call\\n countArraysUtil(arr, X - 1, K, xorVal)\\n \\n # Decrement last element\\n arr[len(arr) - 1] -= 1\\n\\n# Function to find the count of \\n# arrays having atleast K elements \\n# greater than XOR of array\\ndef countArrays(arr, X, K):\\n \\n # Stores the XOR value\\n # of original array\\n xorVal = 0\\n\\n # Traverse the vector\\n for i in range(len(arr)):\\n xorVal = xorVal ^ arr[i]\\n countArraysUtil(arr, X, K, xorVal)\\n\\n # Print the answer\\n print(ans)\\n \\n# Driver Code\\n# Given vector\\narr = [ 10, 2, 10, 5 ]\\n\\n# Given value of X & K\\nX = 3\\nK = 3\\ncountArrays(arr, X, K)\\n\\n", "source_json_file": "count-arrays-having-at-least-k-elements-exceeding-xor-of-all-given-array-elements-by-x-given-operations.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 1, "input": "arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}", "output": "3 3 3 5 1\nExplanation: For 1st query [2, 3], the subarray is {2, 2}.\nThe pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.\nFor 2nd query [2, 4], the subarray is {2, 2, 4}.\nThe pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.\nSimilarly, proceed for the further queries.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for the above approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n#define MAXN 200001\\nint table[1001][1001];\\n\\n// Function to build sparse table\\nvoid buildSparseTable(vector<int> arr, int n)\\n{\\n // GCD of single element is\\n // the element itself\\n for (int i = 0; i < n; i++)\\n table[i][0] = arr[i];\\n\\n // Build sparse table\\n for (int j = 1; j <= log2(n); j++)\\n for (int i = 0; i <= n - (1 << j);\\n i++)\\n table[i][j]\\n = __gcd(table[i][j - 1],\\n table[i + (1 << (j - 1))][j - 1]);\\n}\\n\\n// Function to return the GCD of\\n// all elements in range [L, R]\\nint find_gcd(int L, int R)\\n{\\n // Highest power of 2 that is not\\n // more than count of elements\\n int j = (int)log2(R - L + 1);\\n\\n // Return GCD in range\\n return __gcd(table[L][j],\\n table[R - (1 << j) + 1][j]);\\n}\\n\\n// Smallest prime factors array\\nint spf[MAXN];\\n\\n// Function to build the smallest\\n// prime factor array using Sieve\\nvoid build_spf()\\n{\\n spf[1] = 1;\\n for (int i = 2; i < MAXN; i++)\\n spf[i] = i;\\n for (int i = 4; i < MAXN; i += 2)\\n spf[i] = 2;\\n\\n for (int i = 3; i * i < MAXN; i++) {\\n if (spf[i] == i) {\\n for (int j = i * i; j < MAXN;\\n j += i)\\n if (spf[j] == j)\\n spf[j] = i;\\n }\\n }\\n}\\n\\n// Function to find the count of\\n// distinct prime factors of x\\nint getFactorization(int x)\\n{\\n // Stores the required count\\n int ctr = 0;\\n\\n while (x != 1) {\\n ctr++;\\n\\n // Stores smallest prime\\n // factor of x\\n int p = spf[x];\\n\\n while (x % p == 0)\\n x = x / p;\\n }\\n // Return count\\n return ctr;\\n}\\n// Function to count of coprime pairs such\\n// that the product of the pair divides\\n// all integers of subarray in given range\\nvoid solveQueries(vector<int> a, int n,\\n vector<vector<int> > q)\\n{\\n // Loop to iterate over queries\\n for (int i = 0; i < q.size(); i++) {\\n int l = q[i][0];\\n int r = q[i][1];\\n l--;\\n r--;\\n\\n // Stores gcd in the range\\n int gcd = find_gcd(l, r);\\n\\n // Stores the required count\\n int ans = 0;\\n\\n // Count the pairs of co-primes\\n // integers in given format\\n for (int i = 1; i * i <= gcd; i++) {\\n\\n // If i is a factor of gcd\\n if (gcd % i == 0) {\\n ans = ans + (1 << getFactorization(i));\\n if (gcd / i != i)\\n ans += (1\\n << getFactorization(gcd / i));\\n }\\n }\\n // Print answer\\n cout << ans << \" \";\\n }\\n}\\n\\n// Function to perform precomputation\\nvoid preProcess(vector<int> a, int n)\\n{\\n build_spf();\\n buildSparseTable(a, n);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n vector<int> arr = { 1, 2, 2, 4, 5 };\\n vector<vector<int> > queries = {\\n { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }\\n };\\n\\n preProcess(arr, arr.size());\\n solveQueries(arr, arr.size(), queries);\\n\\n return 0;\\n}", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 1, "input": "arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}", "output": "3 3 3 5 1\nExplanation: For 1st query [2, 3], the subarray is {2, 2}.\nThe pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.\nFor 2nd query [2, 4], the subarray is {2, 2, 4}.\nThe pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.\nSimilarly, proceed for the further queries.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.util.*;\\n\\nclass InfobayAI{\\n\\n static final int MAXN = 200001;\\n static int [][]table = new int[1001][1001];\\n\\n // Function to build sparse table\\n static void buildSparseTable(int[] arr, int n)\\n {\\n\\n // GCD of single element is\\n // the element itself\\n for (int i = 0; i < n; i++)\\n table[i][0] = arr[i];\\n\\n // Build sparse table\\n for (int j = 1; j <= Math.log(n); j++)\\n for (int i = 0; i <= n - (1 << j);\\n i++)\\n table[i][j]\\n = __gcd(table[i][j - 1],\\n table[i + (1 << (j - 1))][j - 1]);\\n }\\n static int __gcd(int a, int b) \\n { \\n return b == 0? a:__gcd(b, a % b); \\n }\\n\\n // Function to return the GCD of\\n // all elements in range [L, R]\\n static int find_gcd(int L, int R)\\n {\\n\\n // Highest power of 2 that is not\\n // more than count of elements\\n int j = (int)Math.log(R - L + 1);\\n\\n // Return GCD in range\\n return __gcd(table[L][j],\\n table[R - (1 << j) + 1][j]);\\n }\\n\\n // Smallest prime factors array\\n static int []spf = new int[MAXN];\\n\\n // Function to build the smallest\\n // prime factor array using Sieve\\n static void build_spf()\\n {\\n spf[1] = 1;\\n for (int i = 2; i < MAXN; i++)\\n spf[i] = i;\\n for (int i = 4; i < MAXN; i += 2)\\n spf[i] = 2;\\n\\n for (int i = 3; i * i < MAXN; i++) {\\n if (spf[i] == i) {\\n for (int j = i * i; j < MAXN;\\n j += i)\\n if (spf[j] == j)\\n spf[j] = i;\\n }\\n }\\n }\\n\\n // Function to find the count of\\n // distinct prime factors of x\\n static int getFactorization(int x)\\n {\\n\\n // Stores the required count\\n int ctr = 0;\\n\\n while (x != 1) {\\n ctr++;\\n\\n // Stores smallest prime\\n // factor of x\\n int p = spf[x];\\n\\n while (x % p == 0)\\n x = x / p;\\n }\\n\\n // Return count\\n return ctr;\\n }\\n\\n // Function to count of coprime pairs such\\n // that the product of the pair divides\\n // all integers of subarray in given range\\n static void solveQueries(int [] a, int n,\\n int [][] q)\\n {\\n\\n // Loop to iterate over queries\\n for (int i = 0; i < q.length; i++) {\\n int l = q[i][0];\\n int r = q[i][1];\\n l--;\\n r--;\\n\\n // Stores gcd in the range\\n int gcd = find_gcd(l, r);\\n\\n // Stores the required count\\n int ans = 0;\\n\\n // Count the pairs of co-primes\\n // integers in given format\\n for (int j = 1; j * j <= gcd; j++) {\\n\\n // If i is a factor of gcd\\n if (gcd % j == 0) {\\n ans = ans + (1 << getFactorization(j));\\n if (gcd / j != j)\\n ans += (1\\n << getFactorization(gcd / j));\\n }\\n }\\n\\n // Print answer\\n System.out.print(ans+ \" \");\\n }\\n }\\n\\n // Function to perform precomputation\\n static void preProcess(int [] a, int n)\\n {\\n build_spf();\\n buildSparseTable(a, n);\\n }\\n\\n // Driver Code\\n public static void main(String[] args)\\n {\\n int[] arr = { 1, 2, 2, 4, 5 };\\n int [][]queries = {\\n { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }\\n };\\n\\n preProcess(arr, arr.length);\\n solveQueries(arr, arr.length, queries);\\n\\n }\\n}\\n\\n", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 1, "input": "arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}", "output": "3 3 3 5 1\nExplanation: For 1st query [2, 3], the subarray is {2, 2}.\nThe pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.\nFor 2nd query [2, 4], the subarray is {2, 2, 4}.\nThe pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.\nSimilarly, proceed for the further queries.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# python program for the above approach\\nimport math\\nMAXN = 200001\\n\\n# creating 2-D table of size 1001*1001\\ntable = []\\n\\nfor i in range(0, 1001):\\n table.append([])\\n for j in range(0, 1001):\\n table[i].append([])\\n\\n# Function to build sparse table\\ndef buildSparseTable(arr, n):\\n\\n # GCD of single element is\\n # the element itself\\n for i in range(0, n):\\n table[i][0] = arr[i]\\n\\n # Build sparse table\\n for j in range(1, (int)(math.log2(n))+1):\\n for i in range(0, n-(1 << j) + 1):\\n table[i][j] = math.gcd(\\n table[i][j - 1], table[i + (1 << (j - 1))][j - 1])\\n\\n# Function to return the GCD of\\n# all elements in range [L, R]\\ndef find_gcd(L, R):\\n \\n # Highest power of 2 that is not\\n # more than count of elements\\n j = (int)(math.log2(R - L + 1))\\n\\n # Return GCD in range\\n return math.gcd(table[L][j],\\n table[R - (1 << j) + 1][j])\\n\\n# Smallest prime factors array\\nspf = [0]*MAXN\\n\\n# Function to build the smallest\\n# prime factor array using Sieve\\ndef build_spf():\\n\\n spf[1] = 1\\n for i in range(2, MAXN):\\n spf[i] = i\\n for i in range(2, MAXN, 2):\\n spf[i] = 2\\n\\n for i in range(3, (int)(math.sqrt(MAXN))):\\n if (spf[i] == i):\\n for j in range(i*i, MAXN, i):\\n if (spf[j] == j):\\n spf[j] = i\\n\\n# Function to find the count of\\n# distinct prime factors of x\\ndef getFactorization(x):\\n \\n # Stores the required count\\n ctr = 0\\n\\n while (x != 1):\\n ctr += 1\\n\\n # Stores smallest prime\\n # factor of x\\n x = (int)(x)\\n p = spf[x]\\n\\n while (x % p == 0):\\n x = x // p\\n\\n # Return count\\n return ctr\\n\\n# Function to count of coprime pairs such\\n# that the product of the pair divides\\n# all integers of subarray in given range\\ndef solveQueries(a, n, q):\\n \\n # Loop to iterate over queries\\n for i in range(len(q)):\\n l = q[i][0]\\n r = q[i][1]\\n l -= 1\\n r -= 1\\n\\n # Stores gcd in the range\\n gcd = find_gcd(l, r)\\n\\n # Stores the required count\\n ans = 0\\n\\n # Count the pairs of co-primes\\n # integers in given format\\n for i in range(1, (int)(math.sqrt(gcd)+1)):\\n\\n # If i is a factor of gcd\\n if (gcd % i == 0):\\n ans = ans + (1 << getFactorization(i))\\n if (gcd / i != i):\\n ans += (1\\n << getFactorization(gcd / i))\\n\\n # Print answer\\n print(ans, end=\" \")\\n\\n# Function to perform precomputation\\ndef preProcess(a, n):\\n build_spf()\\n buildSparseTable(a, n)\\n\\n# Driver Code\\narr = [1, 2, 2, 4, 5]\\nqueries = [[2, 4], [2, 4], [3, 4], [4, 4], [4, 5]]\\n\\npreProcess(arr, len(arr))\\nsolveQueries(arr, len(arr), queries)\\n\\n", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 2, "input": "arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}", "output": "3 3 9", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program for the above approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n#define MAXN 200001\\nint table[1001][1001];\\n\\n// Function to build sparse table\\nvoid buildSparseTable(vector<int> arr, int n)\\n{\\n // GCD of single element is\\n // the element itself\\n for (int i = 0; i < n; i++)\\n table[i][0] = arr[i];\\n\\n // Build sparse table\\n for (int j = 1; j <= log2(n); j++)\\n for (int i = 0; i <= n - (1 << j);\\n i++)\\n table[i][j]\\n = __gcd(table[i][j - 1],\\n table[i + (1 << (j - 1))][j - 1]);\\n}\\n\\n// Function to return the GCD of\\n// all elements in range [L, R]\\nint find_gcd(int L, int R)\\n{\\n // Highest power of 2 that is not\\n // more than count of elements\\n int j = (int)log2(R - L + 1);\\n\\n // Return GCD in range\\n return __gcd(table[L][j],\\n table[R - (1 << j) + 1][j]);\\n}\\n\\n// Smallest prime factors array\\nint spf[MAXN];\\n\\n// Function to build the smallest\\n// prime factor array using Sieve\\nvoid build_spf()\\n{\\n spf[1] = 1;\\n for (int i = 2; i < MAXN; i++)\\n spf[i] = i;\\n for (int i = 4; i < MAXN; i += 2)\\n spf[i] = 2;\\n\\n for (int i = 3; i * i < MAXN; i++) {\\n if (spf[i] == i) {\\n for (int j = i * i; j < MAXN;\\n j += i)\\n if (spf[j] == j)\\n spf[j] = i;\\n }\\n }\\n}\\n\\n// Function to find the count of\\n// distinct prime factors of x\\nint getFactorization(int x)\\n{\\n // Stores the required count\\n int ctr = 0;\\n\\n while (x != 1) {\\n ctr++;\\n\\n // Stores smallest prime\\n // factor of x\\n int p = spf[x];\\n\\n while (x % p == 0)\\n x = x / p;\\n }\\n // Return count\\n return ctr;\\n}\\n// Function to count of coprime pairs such\\n// that the product of the pair divides\\n// all integers of subarray in given range\\nvoid solveQueries(vector<int> a, int n,\\n vector<vector<int> > q)\\n{\\n // Loop to iterate over queries\\n for (int i = 0; i < q.size(); i++) {\\n int l = q[i][0];\\n int r = q[i][1];\\n l--;\\n r--;\\n\\n // Stores gcd in the range\\n int gcd = find_gcd(l, r);\\n\\n // Stores the required count\\n int ans = 0;\\n\\n // Count the pairs of co-primes\\n // integers in given format\\n for (int i = 1; i * i <= gcd; i++) {\\n\\n // If i is a factor of gcd\\n if (gcd % i == 0) {\\n ans = ans + (1 << getFactorization(i));\\n if (gcd / i != i)\\n ans += (1\\n << getFactorization(gcd / i));\\n }\\n }\\n // Print answer\\n cout << ans << \" \";\\n }\\n}\\n\\n// Function to perform precomputation\\nvoid preProcess(vector<int> a, int n)\\n{\\n build_spf();\\n buildSparseTable(a, n);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n vector<int> arr = { 1, 2, 2, 4, 5 };\\n vector<vector<int> > queries = {\\n { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }\\n };\\n\\n preProcess(arr, arr.size());\\n solveQueries(arr, arr.size(), queries);\\n\\n return 0;\\n}", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 2, "input": "arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}", "output": "3 3 9", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.util.*;\\n\\nclass InfobayAI{\\n\\n static final int MAXN = 200001;\\n static int [][]table = new int[1001][1001];\\n\\n // Function to build sparse table\\n static void buildSparseTable(int[] arr, int n)\\n {\\n\\n // GCD of single element is\\n // the element itself\\n for (int i = 0; i < n; i++)\\n table[i][0] = arr[i];\\n\\n // Build sparse table\\n for (int j = 1; j <= Math.log(n); j++)\\n for (int i = 0; i <= n - (1 << j);\\n i++)\\n table[i][j]\\n = __gcd(table[i][j - 1],\\n table[i + (1 << (j - 1))][j - 1]);\\n }\\n static int __gcd(int a, int b) \\n { \\n return b == 0? a:__gcd(b, a % b); \\n }\\n\\n // Function to return the GCD of\\n // all elements in range [L, R]\\n static int find_gcd(int L, int R)\\n {\\n\\n // Highest power of 2 that is not\\n // more than count of elements\\n int j = (int)Math.log(R - L + 1);\\n\\n // Return GCD in range\\n return __gcd(table[L][j],\\n table[R - (1 << j) + 1][j]);\\n }\\n\\n // Smallest prime factors array\\n static int []spf = new int[MAXN];\\n\\n // Function to build the smallest\\n // prime factor array using Sieve\\n static void build_spf()\\n {\\n spf[1] = 1;\\n for (int i = 2; i < MAXN; i++)\\n spf[i] = i;\\n for (int i = 4; i < MAXN; i += 2)\\n spf[i] = 2;\\n\\n for (int i = 3; i * i < MAXN; i++) {\\n if (spf[i] == i) {\\n for (int j = i * i; j < MAXN;\\n j += i)\\n if (spf[j] == j)\\n spf[j] = i;\\n }\\n }\\n }\\n\\n // Function to find the count of\\n // distinct prime factors of x\\n static int getFactorization(int x)\\n {\\n\\n // Stores the required count\\n int ctr = 0;\\n\\n while (x != 1) {\\n ctr++;\\n\\n // Stores smallest prime\\n // factor of x\\n int p = spf[x];\\n\\n while (x % p == 0)\\n x = x / p;\\n }\\n\\n // Return count\\n return ctr;\\n }\\n\\n // Function to count of coprime pairs such\\n // that the product of the pair divides\\n // all integers of subarray in given range\\n static void solveQueries(int [] a, int n,\\n int [][] q)\\n {\\n\\n // Loop to iterate over queries\\n for (int i = 0; i < q.length; i++) {\\n int l = q[i][0];\\n int r = q[i][1];\\n l--;\\n r--;\\n\\n // Stores gcd in the range\\n int gcd = find_gcd(l, r);\\n\\n // Stores the required count\\n int ans = 0;\\n\\n // Count the pairs of co-primes\\n // integers in given format\\n for (int j = 1; j * j <= gcd; j++) {\\n\\n // If i is a factor of gcd\\n if (gcd % j == 0) {\\n ans = ans + (1 << getFactorization(j));\\n if (gcd / j != j)\\n ans += (1\\n << getFactorization(gcd / j));\\n }\\n }\\n\\n // Print answer\\n System.out.print(ans+ \" \");\\n }\\n }\\n\\n // Function to perform precomputation\\n static void preProcess(int [] a, int n)\\n {\\n build_spf();\\n buildSparseTable(a, n);\\n }\\n\\n // Driver Code\\n public static void main(String[] args)\\n {\\n int[] arr = { 1, 2, 2, 4, 5 };\\n int [][]queries = {\\n { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }\\n };\\n\\n preProcess(arr, arr.length);\\n solveQueries(arr, arr.length, queries);\\n\\n }\\n}\\n\\n", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 5, "problem_slug": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries", "title": "Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries", "problem_statement": "Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.", "sample_sno": 2, "input": "arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}", "output": "3 3 9", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# python program for the above approach\\nimport math\\nMAXN = 200001\\n\\n# creating 2-D table of size 1001*1001\\ntable = []\\n\\nfor i in range(0, 1001):\\n table.append([])\\n for j in range(0, 1001):\\n table[i].append([])\\n\\n# Function to build sparse table\\ndef buildSparseTable(arr, n):\\n\\n # GCD of single element is\\n # the element itself\\n for i in range(0, n):\\n table[i][0] = arr[i]\\n\\n # Build sparse table\\n for j in range(1, (int)(math.log2(n))+1):\\n for i in range(0, n-(1 << j) + 1):\\n table[i][j] = math.gcd(\\n table[i][j - 1], table[i + (1 << (j - 1))][j - 1])\\n\\n# Function to return the GCD of\\n# all elements in range [L, R]\\ndef find_gcd(L, R):\\n \\n # Highest power of 2 that is not\\n # more than count of elements\\n j = (int)(math.log2(R - L + 1))\\n\\n # Return GCD in range\\n return math.gcd(table[L][j],\\n table[R - (1 << j) + 1][j])\\n\\n# Smallest prime factors array\\nspf = [0]*MAXN\\n\\n# Function to build the smallest\\n# prime factor array using Sieve\\ndef build_spf():\\n\\n spf[1] = 1\\n for i in range(2, MAXN):\\n spf[i] = i\\n for i in range(2, MAXN, 2):\\n spf[i] = 2\\n\\n for i in range(3, (int)(math.sqrt(MAXN))):\\n if (spf[i] == i):\\n for j in range(i*i, MAXN, i):\\n if (spf[j] == j):\\n spf[j] = i\\n\\n# Function to find the count of\\n# distinct prime factors of x\\ndef getFactorization(x):\\n \\n # Stores the required count\\n ctr = 0\\n\\n while (x != 1):\\n ctr += 1\\n\\n # Stores smallest prime\\n # factor of x\\n x = (int)(x)\\n p = spf[x]\\n\\n while (x % p == 0):\\n x = x // p\\n\\n # Return count\\n return ctr\\n\\n# Function to count of coprime pairs such\\n# that the product of the pair divides\\n# all integers of subarray in given range\\ndef solveQueries(a, n, q):\\n \\n # Loop to iterate over queries\\n for i in range(len(q)):\\n l = q[i][0]\\n r = q[i][1]\\n l -= 1\\n r -= 1\\n\\n # Stores gcd in the range\\n gcd = find_gcd(l, r)\\n\\n # Stores the required count\\n ans = 0\\n\\n # Count the pairs of co-primes\\n # integers in given format\\n for i in range(1, (int)(math.sqrt(gcd)+1)):\\n\\n # If i is a factor of gcd\\n if (gcd % i == 0):\\n ans = ans + (1 << getFactorization(i))\\n if (gcd / i != i):\\n ans += (1\\n << getFactorization(gcd / i))\\n\\n # Print answer\\n print(ans, end=\" \")\\n\\n# Function to perform precomputation\\ndef preProcess(a, n):\\n build_spf()\\n buildSparseTable(a, n)\\n\\n# Driver Code\\narr = [1, 2, 2, 4, 5]\\nqueries = [[2, 4], [2, 4], [3, 4], [4, 4], [4, 5]]\\n\\npreProcess(arr, len(arr))\\nsolveQueries(arr, len(arr), queries)\\n\\n", "source_json_file": "count-of-distinct-coprime-pairs-product-of-which-divides-all-elements-in-index-l-r-for-q-queries.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 1, "input": "Arr = {1, 2, 2, 3, 3}, K = 2", "output": "2\nSubsequences are - {2, 2} and {3, 3}", "solution_sno": 1, "solution_name": "sol1", "language": "cpp14", "code": "// C++ implementation\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Returns factorial of n\\nlong long fact(int n)\\n{\\n long long res = 1;\\n for (int i = 2; i <= n; i++)\\n res = res * i;\\n return res;\\n}\\n\\n// Returns nCr for the\\n// given values of r and n\\nlong long nCr(int n, int r)\\n{\\n return fact(n) / (1LL * fact(r)\\n * fact(n - r));\\n}\\n\\nlong long number_of_subsequences(int arr[],\\n int k,\\n int n)\\n{\\n\\n long long s = 0;\\n\\n // Map to store the frequencies\\n // of each elements\\n map<int, int> m;\\n\\n // Loop to store the\\n // frequencies of elements\\n // in the map\\n for (int i = 0; i < n; i++) {\\n m[arr[i]]++;\\n }\\n\\n for (auto j : m) {\\n\\n // Using nCR formula to\\n // calculate the number\\n // of subsequences of a\\n // given length\\n s = s + 1LL * nCr(j.second, k);\\n }\\n\\n return s;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n int arr[] = { 1, 1, 1, 1, 2, 2, 2 };\\n int k = 2;\\n int n = sizeof(arr) / sizeof(arr[0]);\\n\\n // Function calling\\n cout << number_of_subsequences(arr, k, n);\\n return 0;\\n}", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 1, "input": "Arr = {1, 2, 2, 3, 3}, K = 2", "output": "2\nSubsequences are - {2, 2} and {3, 3}", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java implementation for above approach\\nimport java.util.*;\\n \\nclass InfobayAI\\n{\\n\\n// Returns factorial of n\\nstatic long fact(int n)\\n{\\n long res = 1;\\n for (int i = 2; i <= n; i++)\\n res = res * i;\\n return res;\\n}\\n\\n// Returns nCr for the\\n// given values of r and n\\nstatic long nCr(int n, int r)\\n{\\n return fact(n) / (1 * fact(r) *\\n fact(n - r));\\n}\\n\\nstatic long number_of_subsequences(int arr[], \\n int k, int n)\\n{\\n long s = 0;\\n\\n // Map to store the frequencies\\n // of each elements\\n HashMap<Integer, \\n Integer> mp = new HashMap<Integer, \\n Integer>();\\n\\n // Loop to store the\\n // frequencies of elements\\n // in the map\\n for (int i = 0; i < n; i++)\\n {\\n if(mp.containsKey(arr[i]))\\n {\\n mp.put(arr[i], mp.get(arr[i]) + 1);\\n }\\n else\\n {\\n mp.put(arr[i], 1);\\n }\\n }\\n\\n for (Map.Entry<Integer, \\n Integer> j : mp.entrySet())\\n {\\n\\n // Using nCR formula to\\n // calculate the number\\n // of subsequences of a\\n // given length\\n s = s + 1 * nCr(j.getValue(), k);\\n }\\n\\n return s;\\n}\\n\\n// Driver Code\\nstatic public void main ( String []arg)\\n{\\n int arr[] = { 1, 1, 1, 1, 2, 2, 2 };\\n int k = 2;\\n int n = arr.length;\\n\\n // Function calling\\n System.out.println(number_of_subsequences(arr, k, n));\\n}\\n}\\n\\n", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 1, "input": "Arr = {1, 2, 2, 3, 3}, K = 2", "output": "2\nSubsequences are - {2, 2} and {3, 3}", "solution_sno": 3, "solution_name": "sol3", "language": "python3", "code": "# Python3 implementation of above approach\\n\\n# Returns factorial of n\\ndef fact(n):\\n res = 1\\n for i in range(2, n + 1):\\n res = res * i\\n return res\\n\\n# Returns nCr for the\\n# given values of r and n\\ndef nCr(n, r):\\n return fact(n) // (fact(r) * fact(n - r))\\n\\ndef number_of_subsequences(arr, k, n):\\n\\n s = 0\\n\\n # Map to store the frequencies\\n # of each elements\\n m = dict()\\n \\n # Loop to store the\\n # frequencies of elements\\n # in the map\\n for i in arr:\\n m[i] = m.get(i, 0) + 1\\n\\n for j in m:\\n\\n # Using nCR formula to\\n # calculate the number\\n # of subsequences of a\\n # given length\\n s = s + nCr(m[j], k)\\n\\n return s\\n\\n# Driver Code\\narr = [1, 1, 1, 1, 2, 2, 2]\\nk = 2\\nn = len(arr)\\n\\n# Function calling\\nprint(number_of_subsequences(arr, k, n))\\n\\n", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 2, "input": "Arr = {1, 1, 1, 1, 2, 2}, K = 3", "output": "4", "solution_sno": 1, "solution_name": "sol1", "language": "cpp14", "code": "// C++ implementation\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Returns factorial of n\\nlong long fact(int n)\\n{\\n long long res = 1;\\n for (int i = 2; i <= n; i++)\\n res = res * i;\\n return res;\\n}\\n\\n// Returns nCr for the\\n// given values of r and n\\nlong long nCr(int n, int r)\\n{\\n return fact(n) / (1LL * fact(r)\\n * fact(n - r));\\n}\\n\\nlong long number_of_subsequences(int arr[],\\n int k,\\n int n)\\n{\\n\\n long long s = 0;\\n\\n // Map to store the frequencies\\n // of each elements\\n map<int, int> m;\\n\\n // Loop to store the\\n // frequencies of elements\\n // in the map\\n for (int i = 0; i < n; i++) {\\n m[arr[i]]++;\\n }\\n\\n for (auto j : m) {\\n\\n // Using nCR formula to\\n // calculate the number\\n // of subsequences of a\\n // given length\\n s = s + 1LL * nCr(j.second, k);\\n }\\n\\n return s;\\n}\\n\\n// Driver Code\\nint main()\\n{\\n int arr[] = { 1, 1, 1, 1, 2, 2, 2 };\\n int k = 2;\\n int n = sizeof(arr) / sizeof(arr[0]);\\n\\n // Function calling\\n cout << number_of_subsequences(arr, k, n);\\n return 0;\\n}", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 2, "input": "Arr = {1, 1, 1, 1, 2, 2}, K = 3", "output": "4", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java implementation for above approach\\nimport java.util.*;\\n \\nclass InfobayAI\\n{\\n\\n// Returns factorial of n\\nstatic long fact(int n)\\n{\\n long res = 1;\\n for (int i = 2; i <= n; i++)\\n res = res * i;\\n return res;\\n}\\n\\n// Returns nCr for the\\n// given values of r and n\\nstatic long nCr(int n, int r)\\n{\\n return fact(n) / (1 * fact(r) *\\n fact(n - r));\\n}\\n\\nstatic long number_of_subsequences(int arr[], \\n int k, int n)\\n{\\n long s = 0;\\n\\n // Map to store the frequencies\\n // of each elements\\n HashMap<Integer, \\n Integer> mp = new HashMap<Integer, \\n Integer>();\\n\\n // Loop to store the\\n // frequencies of elements\\n // in the map\\n for (int i = 0; i < n; i++)\\n {\\n if(mp.containsKey(arr[i]))\\n {\\n mp.put(arr[i], mp.get(arr[i]) + 1);\\n }\\n else\\n {\\n mp.put(arr[i], 1);\\n }\\n }\\n\\n for (Map.Entry<Integer, \\n Integer> j : mp.entrySet())\\n {\\n\\n // Using nCR formula to\\n // calculate the number\\n // of subsequences of a\\n // given length\\n s = s + 1 * nCr(j.getValue(), k);\\n }\\n\\n return s;\\n}\\n\\n// Driver Code\\nstatic public void main ( String []arg)\\n{\\n int arr[] = { 1, 1, 1, 1, 2, 2, 2 };\\n int k = 2;\\n int n = arr.length;\\n\\n // Function calling\\n System.out.println(number_of_subsequences(arr, k, n));\\n}\\n}\\n\\n", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 6, "problem_slug": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf", "title": "Count the number of subsequences of length k having equal LCM and HCF", "problem_statement": "Given an array Arr and an integer K . The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.", "sample_sno": 2, "input": "Arr = {1, 1, 1, 1, 2, 2}, K = 3", "output": "4", "solution_sno": 3, "solution_name": "sol3", "language": "python3", "code": "# Python3 implementation of above approach\\n\\n# Returns factorial of n\\ndef fact(n):\\n res = 1\\n for i in range(2, n + 1):\\n res = res * i\\n return res\\n\\n# Returns nCr for the\\n# given values of r and n\\ndef nCr(n, r):\\n return fact(n) // (fact(r) * fact(n - r))\\n\\ndef number_of_subsequences(arr, k, n):\\n\\n s = 0\\n\\n # Map to store the frequencies\\n # of each elements\\n m = dict()\\n \\n # Loop to store the\\n # frequencies of elements\\n # in the map\\n for i in arr:\\n m[i] = m.get(i, 0) + 1\\n\\n for j in m:\\n\\n # Using nCR formula to\\n # calculate the number\\n # of subsequences of a\\n # given length\\n s = s + nCr(m[j], k)\\n\\n return s\\n\\n# Driver Code\\narr = [1, 1, 1, 1, 2, 2, 2]\\nk = 2\\nn = len(arr)\\n\\n# Function calling\\nprint(number_of_subsequences(arr, k, n))\\n\\n", "source_json_file": "count-the-number-of-subsequences-of-length-k-having-equal-lcm-and-hcf.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 1, "input": "arr[] = {4, 7, 5, 9, 3}, M = 4", "output": "808556639\nExplanation:\nThere are five possible sets. They are:\n{4, 7, 5, 9}. In the sorted order, this set becomes {4, 5, 7, 9}. In this set, index 1, 2 and 4 divides M completely. Therefore, arr[1] * arr[2] * arr[4] = 4 * 5 * 9 = 180.\nSimilarly, the remaining four sets along with their products are:\n{4, 7, 9, 3} -> 108\n{4, 5, 9, 3} -> 108\n{4, 7, 5, 3} -> 84\n{7, 5, 9, 3} -> 135\nThe total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program to find the product of\\n// all the combinations of M elements\\n// from an array whose index in the\\n// sorted order divides M completely\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\ntypedef long long int lli;\\nconst int m = 4;\\n\\n// Iterative Function to calculate\\n// (x^y)%p in O(log y)\\nlong long int power(lli x, lli y, lli p)\\n{\\n lli res = 1;\\n x = x % p;\\n\\n while (y > 0) {\\n\\n // If y is odd, multiply x with result\\n if (y & 1)\\n res = (res * x) % p;\\n\\n // y must be even now\\n y = y >> 1;\\n x = (x * x) % p;\\n }\\n return res;\\n}\\n\\n// Iterative Function to calculate\\n// (nCr)%p and save in f[n][r]\\n// C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p\\n// and C(n, 0) = C(n, n) = 1\\nvoid nCr(lli n, lli p, lli f[][m + 1])\\n{\\n for (lli i = 0; i <= n; i++) {\\n for (lli j = 0; j <= m; j++) {\\n\\n // If j>i then C(i, j) = 0\\n if (j > i)\\n f[i][j] = 0;\\n\\n // If i is equal to j then C(i, j) = 1\\n else if (j == 0 || j == i)\\n f[i][j] = 1;\\n\\n // C(i, j) = ( C(i-1, j) + C(i-1, j-1))%p\\n else\\n f[i][j] = (f[i - 1][j]\\n + f[i - 1][j - 1])\\n % p;\\n }\\n }\\n}\\n\\nvoid operations(lli arr[], lli n, lli f[][m + 1])\\n{\\n lli p = 1000000007;\\n nCr(n, p - 1, f);\\n\\n sort(arr, arr + n);\\n\\n // Initialize the answer\\n lli ans = 1;\\n\\n for (lli i = 0; i < n; i++) {\\n\\n // For every element arr[i],\\n // x is count of occurrence\\n // of arr[i] in different set\\n // such that index of arr[i]\\n // in those sets divides m completely.\\n long long int x = 0;\\n\\n for (lli j = 1; j <= m; j++) {\\n\\n // Finding the count of arr[i]\\n // by placing it at the index\\n // which divides m completely\\n if (m % j == 0)\\n\\n // Using fermat's little theorem\\n x = (x\\n + (f[n - i - 1][m - j]\\n * f[i][j - 1])\\n % (p - 1))\\n % (p - 1);\\n }\\n\\n // Multiplying with the count\\n ans = ((ans * power(arr[i],\\n x, p))\\n % p);\\n }\\n\\n cout << ans << endl;\\n}\\n\\n// Driver code\\nint main()\\n{\\n\\n lli arr[] = { 4, 5, 7, 9, 3 };\\n\\n lli n = sizeof(arr) / sizeof(arr[0]);\\n\\n lli f[n + 1][m + 1];\\n\\n operations(arr, n, f);\\n}", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 1, "input": "arr[] = {4, 7, 5, 9, 3}, M = 4", "output": "808556639\nExplanation:\nThere are five possible sets. They are:\n{4, 7, 5, 9}. In the sorted order, this set becomes {4, 5, 7, 9}. In this set, index 1, 2 and 4 divides M completely. Therefore, arr[1] * arr[2] * arr[4] = 4 * 5 * 9 = 180.\nSimilarly, the remaining four sets along with their products are:\n{4, 7, 9, 3} -> 108\n{4, 5, 9, 3} -> 108\n{4, 7, 5, 3} -> 84\n{7, 5, 9, 3} -> 135\nThe total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program to find the product of\\n// all the combinations of M elements\\n// from an array whose index in the\\n// sorted order divides M completely\\nimport java.util.*;\\n\\nclass InfobayAI{\\n\\nstatic int m = 4;\\n\\n// Iterative Function to calculate\\n// (x^y)%p in O(log y)\\nstatic long power(long x, long y, long p)\\n{\\n long res = 1;\\n x = x % p;\\n\\n while (y > 0)\\n {\\n\\n // If y is odd, multiply \\n // x with result\\n if (y % 2 == 1)\\n res = (res * x) % p;\\n\\n // y must be even now\\n y = y >> 1;\\n x = (x * x) % p;\\n }\\n return res;\\n}\\n\\n// Iterative Function to calculate\\n// (nCr)%p and save in f[n][r]\\n// C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p\\n// and C(n, 0) = C(n, n) = 1\\nstatic void nCr(int n, long p, int f[][])\\n{\\n for(int i = 0; i <= n; i++) \\n {\\n for(int j = 0; j <= m; j++)\\n {\\n \\n // If j>i then C(i, j) = 0\\n if (j > i)\\n f[i][j] = 0;\\n \\n // If i is equal to j \\n // then C(i, j) = 1\\n else if (j == 0 || j == i)\\n f[i][j] = 1;\\n \\n // C(i, j) = ( C(i-1, j) + C(i-1, j-1))%p\\n else\\n f[i][j] = (f[i - 1][j] + \\n f[i - 1][j - 1]) % (int)p;\\n }\\n }\\n}\\n\\nstatic void operations(int arr[], int n, int f[][])\\n{\\n long p = 1000000007;\\n nCr(n, p - 1, f);\\n\\n Arrays.sort(arr);\\n\\n // Initialize the answer\\n long ans = 1;\\n\\n for(int i = 0; i < n; i++)\\n {\\n \\n // For every element arr[i],\\n // x is count of occurrence\\n // of arr[i] in different set\\n // such that index of arr[i]\\n // in those sets divides m \\n // completely.\\n long x = 0;\\n \\n for(int j = 1; j <= m; j++)\\n {\\n \\n // Finding the count of arr[i]\\n // by placing it at the index\\n // which divides m completely\\n if (m % j == 0)\\n \\n // Using fermat's little theorem\\n x = (x + (f[n - i - 1][m - j] * \\n f[i][j - 1]) % \\n (p - 1)) % \\n (p - 1);\\n }\\n \\n // Multiplying with the count\\n ans = ((ans * power(arr[i], x, p)) % p);\\n }\\n System.out.print(ans + \"\\n\");\\n}\\n\\n// Driver code\\npublic static void main(String[] args)\\n{\\n int arr[] = { 4, 5, 7, 9, 3 };\\n int n = arr.length;\\n int [][]f = new int[n + 1][m + 1];\\n\\n operations(arr, n, f);\\n}\\n}\\n\\n", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 1, "input": "arr[] = {4, 7, 5, 9, 3}, M = 4", "output": "808556639\nExplanation:\nThere are five possible sets. They are:\n{4, 7, 5, 9}. In the sorted order, this set becomes {4, 5, 7, 9}. In this set, index 1, 2 and 4 divides M completely. Therefore, arr[1] * arr[2] * arr[4] = 4 * 5 * 9 = 180.\nSimilarly, the remaining four sets along with their products are:\n{4, 7, 9, 3} -> 108\n{4, 5, 9, 3} -> 108\n{4, 7, 5, 3} -> 84\n{7, 5, 9, 3} -> 135\nThe total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 program to find the product of\\n# all the combinations of M elements\\n# from an array whose index in the\\n# sorted order divides M completely\\nm = 4\\n\\n# Iterative Function to calculate\\n# (x^y)%p in O(log y)\\ndef power(x, y, p):\\n\\n res = 1\\n x = x % p\\n\\n while (y > 0):\\n\\n # If y is odd, multiply x with result\\n if (y & 1):\\n res = (res * x) % p\\n\\n # y must be even now\\n y = y >> 1\\n x = (x * x) % p\\n return res\\n\\n# Iterative Function to calculate\\n# (nCr)%p and save in f[n][r]\\n# C(n, r)%p = [ C(n-1, r-1)%p + \\n# C(n-1, r)%p ] % p\\n# and C(n, 0) = C(n, n) = 1\\ndef nCr(n, p, f):\\n\\n for i in range (n):\\n for j in range (m + 1):\\n\\n # If j>i then C(i, j) = 0\\n if (j > i):\\n f[i][j] = 0\\n\\n # If i is equal to j then \\n # C(i, j) = 1\\n elif (j == 0 or j == i):\\n f[i][j] = 1\\n\\n # C(i, j) = ( C(i-1, j) + \\n # C(i-1, j-1))%p\\n else:\\n f[i][j] = ((f[i - 1][j] + \\n f[i - 1][j - 1]) % p)\\n\\ndef operations(arr, n, f):\\n\\n p = 1000000007\\n nCr(n, p - 1, f)\\n\\n arr.sort()\\n\\n # Initialize the answer\\n ans = 1\\n \\n for i in range (n):\\n\\n # For every element arr[i],\\n # x is count of occurrence\\n # of arr[i] in different set\\n # such that index of arr[i]\\n # in those sets divides m completely.\\n x = 0\\n\\n for j in range (1, m + 1):\\n\\n # Finding the count of arr[i]\\n # by placing it at the index\\n # which divides m completely\\n if (m % j == 0):\\n\\n # Using fermat's little theorem\\n x = ((x + (f[n - i - 1][m - j] * \\n f[i][j - 1]) % (p - 1)) % \\n (p - 1))\\n \\n # Multiplying with the count\\n ans = ((ans * power(arr[i],\\n x, p)) % p)\\n \\n print (ans)\\n\\n# Driver code\\nif __name__ == \"__main__\":\\n arr = [4, 5, 7, 9, 3]\\n n = len(arr)\\n f = [[0 for x in range (m + 1)]\\n for y in range (n + 1)]\\n operations(arr, n, f)\\n\\n", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 2, "input": "arr[] = {7, 8, 9}, M = 2", "output": "254016", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// C++ program to find the product of\\n// all the combinations of M elements\\n// from an array whose index in the\\n// sorted order divides M completely\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\ntypedef long long int lli;\\nconst int m = 4;\\n\\n// Iterative Function to calculate\\n// (x^y)%p in O(log y)\\nlong long int power(lli x, lli y, lli p)\\n{\\n lli res = 1;\\n x = x % p;\\n\\n while (y > 0) {\\n\\n // If y is odd, multiply x with result\\n if (y & 1)\\n res = (res * x) % p;\\n\\n // y must be even now\\n y = y >> 1;\\n x = (x * x) % p;\\n }\\n return res;\\n}\\n\\n// Iterative Function to calculate\\n// (nCr)%p and save in f[n][r]\\n// C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p\\n// and C(n, 0) = C(n, n) = 1\\nvoid nCr(lli n, lli p, lli f[][m + 1])\\n{\\n for (lli i = 0; i <= n; i++) {\\n for (lli j = 0; j <= m; j++) {\\n\\n // If j>i then C(i, j) = 0\\n if (j > i)\\n f[i][j] = 0;\\n\\n // If i is equal to j then C(i, j) = 1\\n else if (j == 0 || j == i)\\n f[i][j] = 1;\\n\\n // C(i, j) = ( C(i-1, j) + C(i-1, j-1))%p\\n else\\n f[i][j] = (f[i - 1][j]\\n + f[i - 1][j - 1])\\n % p;\\n }\\n }\\n}\\n\\nvoid operations(lli arr[], lli n, lli f[][m + 1])\\n{\\n lli p = 1000000007;\\n nCr(n, p - 1, f);\\n\\n sort(arr, arr + n);\\n\\n // Initialize the answer\\n lli ans = 1;\\n\\n for (lli i = 0; i < n; i++) {\\n\\n // For every element arr[i],\\n // x is count of occurrence\\n // of arr[i] in different set\\n // such that index of arr[i]\\n // in those sets divides m completely.\\n long long int x = 0;\\n\\n for (lli j = 1; j <= m; j++) {\\n\\n // Finding the count of arr[i]\\n // by placing it at the index\\n // which divides m completely\\n if (m % j == 0)\\n\\n // Using fermat's little theorem\\n x = (x\\n + (f[n - i - 1][m - j]\\n * f[i][j - 1])\\n % (p - 1))\\n % (p - 1);\\n }\\n\\n // Multiplying with the count\\n ans = ((ans * power(arr[i],\\n x, p))\\n % p);\\n }\\n\\n cout << ans << endl;\\n}\\n\\n// Driver code\\nint main()\\n{\\n\\n lli arr[] = { 4, 5, 7, 9, 3 };\\n\\n lli n = sizeof(arr) / sizeof(arr[0]);\\n\\n lli f[n + 1][m + 1];\\n\\n operations(arr, n, f);\\n}", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 2, "input": "arr[] = {7, 8, 9}, M = 2", "output": "254016", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program to find the product of\\n// all the combinations of M elements\\n// from an array whose index in the\\n// sorted order divides M completely\\nimport java.util.*;\\n\\nclass InfobayAI{\\n\\nstatic int m = 4;\\n\\n// Iterative Function to calculate\\n// (x^y)%p in O(log y)\\nstatic long power(long x, long y, long p)\\n{\\n long res = 1;\\n x = x % p;\\n\\n while (y > 0)\\n {\\n\\n // If y is odd, multiply \\n // x with result\\n if (y % 2 == 1)\\n res = (res * x) % p;\\n\\n // y must be even now\\n y = y >> 1;\\n x = (x * x) % p;\\n }\\n return res;\\n}\\n\\n// Iterative Function to calculate\\n// (nCr)%p and save in f[n][r]\\n// C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p\\n// and C(n, 0) = C(n, n) = 1\\nstatic void nCr(int n, long p, int f[][])\\n{\\n for(int i = 0; i <= n; i++) \\n {\\n for(int j = 0; j <= m; j++)\\n {\\n \\n // If j>i then C(i, j) = 0\\n if (j > i)\\n f[i][j] = 0;\\n \\n // If i is equal to j \\n // then C(i, j) = 1\\n else if (j == 0 || j == i)\\n f[i][j] = 1;\\n \\n // C(i, j) = ( C(i-1, j) + C(i-1, j-1))%p\\n else\\n f[i][j] = (f[i - 1][j] + \\n f[i - 1][j - 1]) % (int)p;\\n }\\n }\\n}\\n\\nstatic void operations(int arr[], int n, int f[][])\\n{\\n long p = 1000000007;\\n nCr(n, p - 1, f);\\n\\n Arrays.sort(arr);\\n\\n // Initialize the answer\\n long ans = 1;\\n\\n for(int i = 0; i < n; i++)\\n {\\n \\n // For every element arr[i],\\n // x is count of occurrence\\n // of arr[i] in different set\\n // such that index of arr[i]\\n // in those sets divides m \\n // completely.\\n long x = 0;\\n \\n for(int j = 1; j <= m; j++)\\n {\\n \\n // Finding the count of arr[i]\\n // by placing it at the index\\n // which divides m completely\\n if (m % j == 0)\\n \\n // Using fermat's little theorem\\n x = (x + (f[n - i - 1][m - j] * \\n f[i][j - 1]) % \\n (p - 1)) % \\n (p - 1);\\n }\\n \\n // Multiplying with the count\\n ans = ((ans * power(arr[i], x, p)) % p);\\n }\\n System.out.print(ans + \"\\n\");\\n}\\n\\n// Driver code\\npublic static void main(String[] args)\\n{\\n int arr[] = { 4, 5, 7, 9, 3 };\\n int n = arr.length;\\n int [][]f = new int[n + 1][m + 1];\\n\\n operations(arr, n, f);\\n}\\n}\\n\\n", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 7, "problem_slug": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m", "title": "Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M", "problem_statement": "Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].\nNote: The product may be very large, take modulo to 109 + 7.", "sample_sno": 2, "input": "arr[] = {7, 8, 9}, M = 2", "output": "254016", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 program to find the product of\\n# all the combinations of M elements\\n# from an array whose index in the\\n# sorted order divides M completely\\nm = 4\\n\\n# Iterative Function to calculate\\n# (x^y)%p in O(log y)\\ndef power(x, y, p):\\n\\n res = 1\\n x = x % p\\n\\n while (y > 0):\\n\\n # If y is odd, multiply x with result\\n if (y & 1):\\n res = (res * x) % p\\n\\n # y must be even now\\n y = y >> 1\\n x = (x * x) % p\\n return res\\n\\n# Iterative Function to calculate\\n# (nCr)%p and save in f[n][r]\\n# C(n, r)%p = [ C(n-1, r-1)%p + \\n# C(n-1, r)%p ] % p\\n# and C(n, 0) = C(n, n) = 1\\ndef nCr(n, p, f):\\n\\n for i in range (n):\\n for j in range (m + 1):\\n\\n # If j>i then C(i, j) = 0\\n if (j > i):\\n f[i][j] = 0\\n\\n # If i is equal to j then \\n # C(i, j) = 1\\n elif (j == 0 or j == i):\\n f[i][j] = 1\\n\\n # C(i, j) = ( C(i-1, j) + \\n # C(i-1, j-1))%p\\n else:\\n f[i][j] = ((f[i - 1][j] + \\n f[i - 1][j - 1]) % p)\\n\\ndef operations(arr, n, f):\\n\\n p = 1000000007\\n nCr(n, p - 1, f)\\n\\n arr.sort()\\n\\n # Initialize the answer\\n ans = 1\\n \\n for i in range (n):\\n\\n # For every element arr[i],\\n # x is count of occurrence\\n # of arr[i] in different set\\n # such that index of arr[i]\\n # in those sets divides m completely.\\n x = 0\\n\\n for j in range (1, m + 1):\\n\\n # Finding the count of arr[i]\\n # by placing it at the index\\n # which divides m completely\\n if (m % j == 0):\\n\\n # Using fermat's little theorem\\n x = ((x + (f[n - i - 1][m - j] * \\n f[i][j - 1]) % (p - 1)) % \\n (p - 1))\\n \\n # Multiplying with the count\\n ans = ((ans * power(arr[i],\\n x, p)) % p)\\n \\n print (ans)\\n\\n# Driver code\\nif __name__ == \"__main__\":\\n arr = [4, 5, 7, 9, 3]\\n n = len(arr)\\n f = [[0 for x in range (m + 1)]\\n for y in range (n + 1)]\\n operations(arr, n, f)\\n\\n", "source_json_file": "find-product-of-all-elements-at-indexes-which-are-factors-of-m-for-all-possible-sorted-subsequences-of-length-m.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 1, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 1], [0, 1, 1, 1, 0]]", "output": "[0, 1, 2, 4, 3, 0]", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "#include <iostream>\\n#include <vector>\\n\\nusing namespace std;\\n\\n// Check if it's valid to place vertex at current position\\nbool isSafe(int vertex, vector<vector<int>> &adjMat, \\n vector<int> &path, int pos) {\\n \\n // The vertex must be adjacent to the previous vertex\\n if (!adjMat[path[pos - 1]][vertex]) {\\n return false;\\n }\\n\\n // The vertex must not already be in the path\\n for (int i = 0; i < pos; i++) {\\n if (path[i] == vertex) {\\n return false;\\n }\\n }\\n\\n return true;\\n}\\n\\n// Recursive backtracking to construct Hamiltonian Cycle\\nbool hamCycleUtil(vector<vector<int>> &adjMat, \\n vector<int> &path, int pos, int n) {\\n \\n // Base case: all vertices are in the path\\n if (pos == n) {\\n \\n // Check if there's an edge from last to first vertex\\n return adjMat[path[pos - 1]][path[0]];\\n }\\n\\n // Try all possible vertices as next candidate\\n for (int v = 1; v < n; v++) {\\n if (isSafe(v, adjMat, path, pos)) {\\n path[pos] = v;\\n\\n if (hamCycleUtil(adjMat, path, pos + 1, n)) {\\n return true;\\n }\\n\\n // Backtrack if v doesn't lead to a solution\\n path[pos] = -1;\\n }\\n }\\n\\n return false;\\n}\\n\\n// Initialize path and invoke backtracking function\\nvector<int> hamCycle(vector<vector<int>> &adjMat) {\\n int n = adjMat.size();\\n vector<int> path(n, -1);\\n\\n // Start path with vertex 0\\n path[0] = 0;\\n\\n if (!hamCycleUtil(adjMat, path, 1, n)) {\\n return {-1};\\n }\\n\\n return path;\\n}\\n\\n// Driver Code\\nint main() {\\n \\n vector<vector<int>> adjMat = {\\n {0, 1, 0, 1, 0}, \\n {1, 0, 1, 1, 1}, \\n {0, 1, 0, 0, 1}, \\n {1, 1, 0, 0, 1}, \\n {0, 1, 1, 1, 0}\\n };\\n\\n vector<int> path = hamCycle(adjMat);\\n \\n if(path[0] == -1) {\\n cout << \"Solution does not Exist\";\\n }\\n else {\\n for (int i = 0; i < path.size(); i++) {\\n cout << path[i] << \" \";\\n }\\n cout << path[0];\\n }\\n return 0;\\n}", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 1, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 1], [0, 1, 1, 1, 0]]", "output": "[0, 1, 2, 4, 3, 0]", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "import java.util.ArrayList;\\nimport java.util.List;\\n\\npublic class InfobayAI {\\n \\n // Check if it's valid to place vertex at current position\\n private static boolean isSafe(int vertex, int[][] adjMat, \\n List<Integer> path, int pos) {\\n \\n // The vertex must be adjacent to the previous vertex\\n if (adjMat[path.get(pos - 1)][vertex] == 0) {\\n return false;\\n }\\n\\n // The vertex must not already be in the path\\n for (int i = 0; i < pos; i++) {\\n if (path.get(i) == vertex) {\\n return false;\\n }\\n }\\n\\n return true;\\n }\\n\\n // Recursive backtracking to construct Hamiltonian Cycle\\n private static boolean hamCycleUtil(int[][] adjMat, \\n List<Integer> path, int pos, int n) {\\n \\n // Base case: all vertices are in the path\\n if (pos == n) {\\n \\n // Check if there's an edge from last to first vertex\\n return adjMat[path.get(pos - 1)][path.get(0)] == 1;\\n }\\n\\n // Try all possible vertices as next candidate\\n for (int v = 1; v < n; v++) {\\n if (isSafe(v, adjMat, path, pos)) {\\n \\n path.set(pos, v);\\n \\n if (hamCycleUtil(adjMat, path, pos + 1, n)) {\\n return true;\\n }\\n\\n // Backtrack if v doesn't lead to a solution\\n path.set(pos, -1);\\n }\\n }\\n\\n return false;\\n }\\n\\n // Initialize path and invoke backtracking function\\n public static List<Integer> hamCycle(int[][] adjMat) {\\n \\n int n = adjMat.length;\\n List<Integer> path = new ArrayList<>(n);\\n \\n for (int i = 0; i < n; i++) {\\n path.add(-1);\\n }\\n\\n // Start path with vertex 0\\n path.set(0, 0);\\n\\n if (!hamCycleUtil(adjMat, path, 1, n)) {\\n \\n List<Integer> noSolution = new ArrayList<>();\\n noSolution.add(-1);\\n return noSolution;\\n }\\n\\n return path;\\n }\\n\\n // Driver Code\\n public static void main(String[] args) {\\n int[][] adjMat = {\\n {0, 1, 0, 1, 0},\\n {1, 0, 1, 1, 1},\\n {0, 1, 0, 0, 1},\\n {1, 1, 0, 0, 1},\\n {0, 1, 1, 1, 0}\\n };\\n\\n List<Integer> path = hamCycle(adjMat);\\n\\n if (path.get(0) == -1) {\\n System.out.println(\"Solution does not Exist\");\\n } \\n else {\\n for (int i = 0; i < path.size(); i++) {\\n System.out.print(path.get(i) + \" \");\\n }\\n System.out.print(path.get(0));\\n }\\n }\\n}", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 1, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 1], [0, 1, 1, 1, 0]]", "output": "[0, 1, 2, 4, 3, 0]", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Check if it's valid to place vertex at current position\\ndef isSafe(vertex, adjMat, path, pos):\\n\\n # The vertex must be adjacent to the previous vertex\\n if adjMat[path[pos - 1]][vertex] == 0:\\n return False\\n\\n # The vertex must not already be in the path\\n for i in range(pos):\\n if path[i] == vertex:\\n return False\\n\\n return True\\n\\n\\n# Recursive backtracking to construct Hamiltonian Cycle\\ndef hamCycleUtil(adjMat, path, pos, n):\\n\\n # Base case: all vertices are in the path\\n if pos == n:\\n\\n # Check if there's an edge from last to first vertex\\n return adjMat[path[pos - 1]][path[0]] == 1\\n\\n # Try all possible vertices as next candidate\\n for v in range(1, n):\\n if isSafe(v, adjMat, path, pos):\\n\\n path[pos] = v\\n\\n if hamCycleUtil(adjMat, path, pos + 1, n):\\n return True\\n\\n # Backtrack if v doesn't lead to a solution\\n path[pos] = -1\\n\\n return False\\n\\n\\n# Initialize path and invoke backtracking function\\ndef hamCycle(adjMat):\\n\\n n = len(adjMat)\\n path = [-1] * n\\n\\n # Start path with vertex 0\\n path[0] = 0\\n\\n if not hamCycleUtil(adjMat, path, 1, n):\\n return [-1]\\n\\n return path\\n\\n\\nif __name__ == \"__main__\":\\n adjMat = [\\n [0, 1, 0, 1, 0],\\n [1, 0, 1, 1, 1],\\n [0, 1, 0, 0, 1],\\n [1, 1, 0, 0, 1],\\n [0, 1, 1, 1, 0]\\n ]\\n \\n path = hamCycle(adjMat)\\n \\n if path[0] == -1:\\n print(\"Solution does not Exist\")\\n else:\\n for v in path:\\n print(v, end=\" \")\\n print(path[0])", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 2, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0]]", "output": "\"Solution Does Not Exists\"", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "#include <iostream>\\n#include <vector>\\n\\nusing namespace std;\\n\\n// Check if it's valid to place vertex at current position\\nbool isSafe(int vertex, vector<vector<int>> &adjMat, \\n vector<int> &path, int pos) {\\n \\n // The vertex must be adjacent to the previous vertex\\n if (!adjMat[path[pos - 1]][vertex]) {\\n return false;\\n }\\n\\n // The vertex must not already be in the path\\n for (int i = 0; i < pos; i++) {\\n if (path[i] == vertex) {\\n return false;\\n }\\n }\\n\\n return true;\\n}\\n\\n// Recursive backtracking to construct Hamiltonian Cycle\\nbool hamCycleUtil(vector<vector<int>> &adjMat, \\n vector<int> &path, int pos, int n) {\\n \\n // Base case: all vertices are in the path\\n if (pos == n) {\\n \\n // Check if there's an edge from last to first vertex\\n return adjMat[path[pos - 1]][path[0]];\\n }\\n\\n // Try all possible vertices as next candidate\\n for (int v = 1; v < n; v++) {\\n if (isSafe(v, adjMat, path, pos)) {\\n path[pos] = v;\\n\\n if (hamCycleUtil(adjMat, path, pos + 1, n)) {\\n return true;\\n }\\n\\n // Backtrack if v doesn't lead to a solution\\n path[pos] = -1;\\n }\\n }\\n\\n return false;\\n}\\n\\n// Initialize path and invoke backtracking function\\nvector<int> hamCycle(vector<vector<int>> &adjMat) {\\n int n = adjMat.size();\\n vector<int> path(n, -1);\\n\\n // Start path with vertex 0\\n path[0] = 0;\\n\\n if (!hamCycleUtil(adjMat, path, 1, n)) {\\n return {-1};\\n }\\n\\n return path;\\n}\\n\\n// Driver Code\\nint main() {\\n \\n vector<vector<int>> adjMat = {\\n {0, 1, 0, 1, 0}, \\n {1, 0, 1, 1, 1}, \\n {0, 1, 0, 0, 1}, \\n {1, 1, 0, 0, 1}, \\n {0, 1, 1, 1, 0}\\n };\\n\\n vector<int> path = hamCycle(adjMat);\\n \\n if(path[0] == -1) {\\n cout << \"Solution does not Exist\";\\n }\\n else {\\n for (int i = 0; i < path.size(); i++) {\\n cout << path[i] << \" \";\\n }\\n cout << path[0];\\n }\\n return 0;\\n}", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 2, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0]]", "output": "\"Solution Does Not Exists\"", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "import java.util.ArrayList;\\nimport java.util.List;\\n\\npublic class InfobayAI {\\n \\n // Check if it's valid to place vertex at current position\\n private static boolean isSafe(int vertex, int[][] adjMat, \\n List<Integer> path, int pos) {\\n \\n // The vertex must be adjacent to the previous vertex\\n if (adjMat[path.get(pos - 1)][vertex] == 0) {\\n return false;\\n }\\n\\n // The vertex must not already be in the path\\n for (int i = 0; i < pos; i++) {\\n if (path.get(i) == vertex) {\\n return false;\\n }\\n }\\n\\n return true;\\n }\\n\\n // Recursive backtracking to construct Hamiltonian Cycle\\n private static boolean hamCycleUtil(int[][] adjMat, \\n List<Integer> path, int pos, int n) {\\n \\n // Base case: all vertices are in the path\\n if (pos == n) {\\n \\n // Check if there's an edge from last to first vertex\\n return adjMat[path.get(pos - 1)][path.get(0)] == 1;\\n }\\n\\n // Try all possible vertices as next candidate\\n for (int v = 1; v < n; v++) {\\n if (isSafe(v, adjMat, path, pos)) {\\n \\n path.set(pos, v);\\n \\n if (hamCycleUtil(adjMat, path, pos + 1, n)) {\\n return true;\\n }\\n\\n // Backtrack if v doesn't lead to a solution\\n path.set(pos, -1);\\n }\\n }\\n\\n return false;\\n }\\n\\n // Initialize path and invoke backtracking function\\n public static List<Integer> hamCycle(int[][] adjMat) {\\n \\n int n = adjMat.length;\\n List<Integer> path = new ArrayList<>(n);\\n \\n for (int i = 0; i < n; i++) {\\n path.add(-1);\\n }\\n\\n // Start path with vertex 0\\n path.set(0, 0);\\n\\n if (!hamCycleUtil(adjMat, path, 1, n)) {\\n \\n List<Integer> noSolution = new ArrayList<>();\\n noSolution.add(-1);\\n return noSolution;\\n }\\n\\n return path;\\n }\\n\\n // Driver Code\\n public static void main(String[] args) {\\n int[][] adjMat = {\\n {0, 1, 0, 1, 0},\\n {1, 0, 1, 1, 1},\\n {0, 1, 0, 0, 1},\\n {1, 1, 0, 0, 1},\\n {0, 1, 1, 1, 0}\\n };\\n\\n List<Integer> path = hamCycle(adjMat);\\n\\n if (path.get(0) == -1) {\\n System.out.println(\"Solution does not Exist\");\\n } \\n else {\\n for (int i = 0; i < path.size(); i++) {\\n System.out.print(path.get(i) + \" \");\\n }\\n System.out.print(path.get(0));\\n }\\n }\\n}", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 8, "problem_slug": "hamiltonian-cycle", "title": "Hamiltonian Cycle", "problem_statement": "A Hamiltonian Cycle or Circuit in a graph G is a cycle that visits each vertex of G exactly once and returns to the starting vertex.\nIf a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.\nFinding a Hamiltonian cycle is an NP-complete problem, meaning there's no known efficient solution for all graph types, but solutions exist for smaller or specific types.\nThe Hamiltonian Cycle problem has applications in logistics, network design, and computer science.\nGiven an undirected graph, the task is to determine if it contains a Hamiltonian cycle. If found, print the path; otherwise, print \"Solution does not exist\".", "sample_sno": 2, "input": "N=5, adjMat[][] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0]]", "output": "\"Solution Does Not Exists\"", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Check if it's valid to place vertex at current position\\ndef isSafe(vertex, adjMat, path, pos):\\n\\n # The vertex must be adjacent to the previous vertex\\n if adjMat[path[pos - 1]][vertex] == 0:\\n return False\\n\\n # The vertex must not already be in the path\\n for i in range(pos):\\n if path[i] == vertex:\\n return False\\n\\n return True\\n\\n\\n# Recursive backtracking to construct Hamiltonian Cycle\\ndef hamCycleUtil(adjMat, path, pos, n):\\n\\n # Base case: all vertices are in the path\\n if pos == n:\\n\\n # Check if there's an edge from last to first vertex\\n return adjMat[path[pos - 1]][path[0]] == 1\\n\\n # Try all possible vertices as next candidate\\n for v in range(1, n):\\n if isSafe(v, adjMat, path, pos):\\n\\n path[pos] = v\\n\\n if hamCycleUtil(adjMat, path, pos + 1, n):\\n return True\\n\\n # Backtrack if v doesn't lead to a solution\\n path[pos] = -1\\n\\n return False\\n\\n\\n# Initialize path and invoke backtracking function\\ndef hamCycle(adjMat):\\n\\n n = len(adjMat)\\n path = [-1] * n\\n\\n # Start path with vertex 0\\n path[0] = 0\\n\\n if not hamCycleUtil(adjMat, path, 1, n):\\n return [-1]\\n\\n return path\\n\\n\\nif __name__ == \"__main__\":\\n adjMat = [\\n [0, 1, 0, 1, 0],\\n [1, 0, 1, 1, 1],\\n [0, 1, 0, 0, 1],\\n [1, 1, 0, 0, 1],\\n [0, 1, 1, 1, 0]\\n ]\\n \\n path = hamCycle(adjMat)\\n \\n if path[0] == -1:\\n print(\"Solution does not Exist\")\\n else:\\n for v in path:\\n print(v, end=\" \")\\n print(path[0])", "source_json_file": "hamiltonian-cycle.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 1, "input": "arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8}", "output": "4\nExplanation:\nThe longest common prime subsequence present in both the arrays is {2, 3, 5, 7}.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// CPP implementation of the above approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to calculate the LCS\\nint recursion(vector<int> arr1,\\n vector<int> arr2, int i,\\n int j, map<pair<int, int>, \\n int> dp)\\n{\\n if (i >= arr1.size() or j >= arr2.size())\\n return 0;\\n pair<int, int> key = { i, j };\\n if (arr1[i] == arr2[j])\\n return 1 \\n + recursion(arr1, arr2, \\n i + 1, j + 1,\\n dp);\\n if (dp.find(key) != dp.end())\\n return dp[key];\\n\\n else\\n dp[key] = max(recursion(arr1, arr2, \\n i + 1, j, dp),\\n recursion(arr1, arr2, i, \\n j + 1, dp));\\n return dp[key];\\n}\\n\\n// Function to generate\\n// all the possible\\n// prime numbers\\nvector<int> primegenerator(int n)\\n{\\n int cnt = 0;\\n vector<int> primes(n + 1, true);\\n int p = 2;\\n while (p * p <= n)\\n {\\n for (int i = p * p; i <= n; i += p)\\n primes[i] = false;\\n p += 1;\\n }\\n return primes;\\n}\\n\\n// Function which returns the\\n// length of longest common\\n// prime subsequence\\nint longestCommonSubseq(vector<int> arr1, \\n vector<int> arr2)\\n{\\n\\n // Minimum element of\\n // both arrays\\n int min1 = *min_element(arr1.begin(), \\n arr1.end());\\n int min2 = *min_element(arr2.begin(),\\n arr2.end());\\n\\n // Maximum element of\\n // both arrays\\n int max1 = *max_element(arr1.begin(),\\n arr1.end());\\n int max2 = *max_element(arr2.begin(), \\n arr2.end());\\n\\n // Generating all primes within\\n // the max range of arr1\\n vector<int> a = primegenerator(max1);\\n\\n // Generating all primes within\\n // the max range of arr2\\n vector<int> b = primegenerator(max2);\\n\\n vector<int> finala;\\n vector<int> finalb;\\n\\n // Store precomputed values\\n map<pair<int, int>, int> dp;\\n\\n // Store all primes in arr1[]\\n for (int i = min1; i <= max1; i++) \\n {\\n if (find(arr1.begin(), arr1.end(), i) \\n != arr1.end()\\n and a[i] == true)\\n finala.push_back(i);\\n }\\n\\n // Store all primes of arr2[]\\n for (int i = min2; i <= max2; i++) \\n {\\n if (find(arr2.begin(), arr2.end(), i) \\n != arr2.end()\\n and b[i] == true)\\n finalb.push_back(i);\\n }\\n\\n // Calculating the LCS\\n return recursion(finala, finalb, 0, 0, dp);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n vector<int> arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };\\n vector<int> arr2 = { 2, 5, 6, 3, 7, 9, 8 };\\n \\n // Function Call\\n cout << longestCommonSubseq(arr1, arr2);\\n}", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 1, "input": "arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8}", "output": "4\nExplanation:\nThe longest common prime subsequence present in both the arrays is {2, 3, 5, 7}.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// JAVA implementation of the above approach\\nimport java.util.*;\\nimport java.io.*;\\nimport java.math.*;\\npublic class InfobayAI\\n{\\n\\n // Function to calculate the LCS\\n static int recursion(ArrayList<Integer> arr1,\\n ArrayList<Integer> arr2, int i,\\n int j, Map<ArrayList<Integer>, \\n Integer> dp)\\n {\\n if (i >= arr1.size() || j >= arr2.size())\\n return 0;\\n ArrayList<Integer> key = new ArrayList<>();\\n key.add(i);\\n key.add(j);\\n if (arr1.get(i) == arr2.get(j))\\n return 1 + recursion(arr1, arr2, \\n i + 1, j + 1,\\n dp);\\n if (dp.get(key) != dp.get(dp.size()-1))\\n return dp.get(key);\\n\\n else\\n dp.put(key,Math.max(recursion(arr1, arr2, \\n i + 1, j, dp),\\n recursion(arr1, arr2, i, \\n j + 1, dp)));\\n return dp.get(key);\\n }\\n\\n // Function to generate\\n // all the possible\\n // prime numbers\\n static ArrayList<Boolean> primegenerator(int n)\\n {\\n int cnt = 0;\\n ArrayList<Boolean> primes = new ArrayList<>();\\n for(int i = 0; i < n + 1; i++)\\n primes.add(true);\\n int p = 2;\\n while (p * p <= n)\\n {\\n for (int i = p * p; i <= n; i += p)\\n primes.set(i,false);\\n p += 1;\\n }\\n return primes;\\n }\\n\\n // Function that returns the Minimum element of an ArrayList\\n static int min_element(ArrayList<Integer> al)\\n {\\n int min = Integer.MAX_VALUE;\\n for(int i = 0; i < al.size(); i++)\\n {\\n min=Math.min(min, al.get(i));\\n }\\n return min;\\n }\\n\\n // Function that returns the Minimum element of an ArrayList\\n static int max_element(ArrayList<Integer> al)\\n {\\n int max = Integer.MIN_VALUE;\\n for(int i = 0; i < al.size(); i++)\\n {\\n max = Math.max(max, al.get(i));\\n }\\n return max;\\n }\\n\\n // Function which returns the\\n // length of longest common\\n // prime subsequence\\n static int longestCommonSubseq(ArrayList<Integer> arr1, \\n ArrayList<Integer> arr2)\\n {\\n\\n // Minimum element of\\n // both arrays\\n int min1 = min_element(arr1);\\n int min2 = min_element(arr2);\\n\\n // Maximum element of\\n // both arrays\\n int max1 = max_element(arr1);\\n int max2 = max_element(arr2);\\n\\n // Generating all primes within\\n // the max range of arr1\\n ArrayList<Boolean> a = primegenerator(max1);\\n\\n // Generating all primes within\\n // the max range of arr2\\n ArrayList<Boolean> b = primegenerator(max2);\\n ArrayList<Integer> finala = new ArrayList<>();\\n ArrayList<Integer> finalb = new ArrayList<>();\\n\\n // Store precomputed values\\n Map<ArrayList<Integer>,Integer> dp = \\n new HashMap <ArrayList<Integer>,Integer> ();\\n\\n // Store all primes in arr1[]\\n for (int i = min1; i <= max1; i++) \\n {\\n if (arr1.contains(i)\\n && a.get(i) == true)\\n finala.add(i);\\n }\\n\\n // Store all primes of arr2[]\\n for (int i = min2; i <= max2; i++) \\n {\\n if (arr2.contains(i)\\n && b.get(i) == true)\\n finalb.add(i);\\n }\\n\\n // Calculating the LCS\\n return recursion(finala, finalb, 0, 0, dp);\\n }\\n\\n // Driver Code\\n public static void main(String args[])\\n {\\n int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };\\n int a2[] = { 2, 5, 6, 3, 7, 9, 8 };\\n\\n // Converting into list\\n ArrayList<Integer> arr1 = new ArrayList<Integer>();\\n for(int i = 0; i < a1.length; i++)\\n arr1.add(a1[i]);\\n\\n ArrayList<Integer> arr2 = new ArrayList<Integer>();\\n for(int i = 0; i < a2.length; i++)\\n arr2.add(a2[i]);\\n\\n // Function Call\\n System.out.println(longestCommonSubseq(arr1, arr2));\\n }\\n}\\n\\n", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 1, "input": "arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8}", "output": "4\nExplanation:\nThe longest common prime subsequence present in both the arrays is {2, 3, 5, 7}.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python implementation of the above approach\\n\\n# Function to calculate the LCS\\n\\ndef recursion(arr1, arr2, i, j, dp):\\n if i >= len(arr1) or j >= len(arr2):\\n return 0\\n key = (i, j)\\n if arr1[i] == arr2[j]:\\n return 1 + recursion(arr1, arr2,\\n i + 1, j + 1, dp)\\n if key in dp:\\n return dp[key]\\n else:\\n dp[key] = max(recursion(arr1, arr2,\\n i + 1, j, dp),\\n recursion(arr1, arr2,\\n i, j + 1, dp))\\n return dp[key]\\n\\n# Function to generate\\n# all the possible\\n# prime numbers\\n\\n\\ndef primegenerator(n):\\n cnt = 0\\n primes = [True for _ in range(n + 1)]\\n p = 2\\n while p * p <= n:\\n for i in range(p * p, n + 1, p):\\n primes[i] = False\\n p += 1\\n return primes\\n\\n# Function which returns the\\n# length of longest common\\n# prime subsequence\\n\\n\\ndef longestCommonSubseq(arr1, arr2):\\n\\n # Minimum element of\\n # both arrays\\n min1 = min(arr1)\\n min2 = min(arr2)\\n\\n # Maximum element of\\n # both arrays\\n max1 = max(arr1)\\n max2 = max(arr2)\\n\\n # Generating all primes within\\n # the max range of arr1\\n a = primegenerator(max1)\\n\\n # Generating all primes within\\n # the max range of arr2\\n b = primegenerator(max2)\\n\\n finala = []\\n finalb = []\\n\\n # Store precomputed values\\n dp = dict()\\n\\n # Store all primes in arr1[]\\n for i in range(min1, max1 + 1):\\n if i in arr1 and a[i] == True:\\n finala.append(i)\\n\\n # Store all primes of arr2[]\\n for i in range(min2, max2 + 1):\\n if i in arr2 and b[i] == True:\\n finalb.append(i)\\n\\n # Calculating the LCS\\n return recursion(finala, finalb,\\n 0, 0, dp)\\n\\n\\n# Driver Code\\narr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]\\narr2 = [2, 5, 6, 3, 7, 9, 8]\\n\\n# Function Call\\nprint(longestCommonSubseq(arr1, arr2))", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 2, "input": "arr1[] = {1, 3, 5, 7, 9}, arr2[] = {2, 4, 6, 8, 10}", "output": "0\nExplanation:\nIn the above arrays, the prime subsequence of arr1[] is {1, 3, 5, 7} and arr2[] is {2}. Therefore, there is no common prime numbers which are present in both the arrays. Hence, the result is 0.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp", "code": "// CPP implementation of the above approach\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to calculate the LCS\\nint recursion(vector<int> arr1,\\n vector<int> arr2, int i,\\n int j, map<pair<int, int>, \\n int> dp)\\n{\\n if (i >= arr1.size() or j >= arr2.size())\\n return 0;\\n pair<int, int> key = { i, j };\\n if (arr1[i] == arr2[j])\\n return 1 \\n + recursion(arr1, arr2, \\n i + 1, j + 1,\\n dp);\\n if (dp.find(key) != dp.end())\\n return dp[key];\\n\\n else\\n dp[key] = max(recursion(arr1, arr2, \\n i + 1, j, dp),\\n recursion(arr1, arr2, i, \\n j + 1, dp));\\n return dp[key];\\n}\\n\\n// Function to generate\\n// all the possible\\n// prime numbers\\nvector<int> primegenerator(int n)\\n{\\n int cnt = 0;\\n vector<int> primes(n + 1, true);\\n int p = 2;\\n while (p * p <= n)\\n {\\n for (int i = p * p; i <= n; i += p)\\n primes[i] = false;\\n p += 1;\\n }\\n return primes;\\n}\\n\\n// Function which returns the\\n// length of longest common\\n// prime subsequence\\nint longestCommonSubseq(vector<int> arr1, \\n vector<int> arr2)\\n{\\n\\n // Minimum element of\\n // both arrays\\n int min1 = *min_element(arr1.begin(), \\n arr1.end());\\n int min2 = *min_element(arr2.begin(),\\n arr2.end());\\n\\n // Maximum element of\\n // both arrays\\n int max1 = *max_element(arr1.begin(),\\n arr1.end());\\n int max2 = *max_element(arr2.begin(), \\n arr2.end());\\n\\n // Generating all primes within\\n // the max range of arr1\\n vector<int> a = primegenerator(max1);\\n\\n // Generating all primes within\\n // the max range of arr2\\n vector<int> b = primegenerator(max2);\\n\\n vector<int> finala;\\n vector<int> finalb;\\n\\n // Store precomputed values\\n map<pair<int, int>, int> dp;\\n\\n // Store all primes in arr1[]\\n for (int i = min1; i <= max1; i++) \\n {\\n if (find(arr1.begin(), arr1.end(), i) \\n != arr1.end()\\n and a[i] == true)\\n finala.push_back(i);\\n }\\n\\n // Store all primes of arr2[]\\n for (int i = min2; i <= max2; i++) \\n {\\n if (find(arr2.begin(), arr2.end(), i) \\n != arr2.end()\\n and b[i] == true)\\n finalb.push_back(i);\\n }\\n\\n // Calculating the LCS\\n return recursion(finala, finalb, 0, 0, dp);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n vector<int> arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };\\n vector<int> arr2 = { 2, 5, 6, 3, 7, 9, 8 };\\n \\n // Function Call\\n cout << longestCommonSubseq(arr1, arr2);\\n}", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 2, "input": "arr1[] = {1, 3, 5, 7, 9}, arr2[] = {2, 4, 6, 8, 10}", "output": "0\nExplanation:\nIn the above arrays, the prime subsequence of arr1[] is {1, 3, 5, 7} and arr2[] is {2}. Therefore, there is no common prime numbers which are present in both the arrays. Hence, the result is 0.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// JAVA implementation of the above approach\\nimport java.util.*;\\nimport java.io.*;\\nimport java.math.*;\\npublic class InfobayAI\\n{\\n\\n // Function to calculate the LCS\\n static int recursion(ArrayList<Integer> arr1,\\n ArrayList<Integer> arr2, int i,\\n int j, Map<ArrayList<Integer>, \\n Integer> dp)\\n {\\n if (i >= arr1.size() || j >= arr2.size())\\n return 0;\\n ArrayList<Integer> key = new ArrayList<>();\\n key.add(i);\\n key.add(j);\\n if (arr1.get(i) == arr2.get(j))\\n return 1 + recursion(arr1, arr2, \\n i + 1, j + 1,\\n dp);\\n if (dp.get(key) != dp.get(dp.size()-1))\\n return dp.get(key);\\n\\n else\\n dp.put(key,Math.max(recursion(arr1, arr2, \\n i + 1, j, dp),\\n recursion(arr1, arr2, i, \\n j + 1, dp)));\\n return dp.get(key);\\n }\\n\\n // Function to generate\\n // all the possible\\n // prime numbers\\n static ArrayList<Boolean> primegenerator(int n)\\n {\\n int cnt = 0;\\n ArrayList<Boolean> primes = new ArrayList<>();\\n for(int i = 0; i < n + 1; i++)\\n primes.add(true);\\n int p = 2;\\n while (p * p <= n)\\n {\\n for (int i = p * p; i <= n; i += p)\\n primes.set(i,false);\\n p += 1;\\n }\\n return primes;\\n }\\n\\n // Function that returns the Minimum element of an ArrayList\\n static int min_element(ArrayList<Integer> al)\\n {\\n int min = Integer.MAX_VALUE;\\n for(int i = 0; i < al.size(); i++)\\n {\\n min=Math.min(min, al.get(i));\\n }\\n return min;\\n }\\n\\n // Function that returns the Minimum element of an ArrayList\\n static int max_element(ArrayList<Integer> al)\\n {\\n int max = Integer.MIN_VALUE;\\n for(int i = 0; i < al.size(); i++)\\n {\\n max = Math.max(max, al.get(i));\\n }\\n return max;\\n }\\n\\n // Function which returns the\\n // length of longest common\\n // prime subsequence\\n static int longestCommonSubseq(ArrayList<Integer> arr1, \\n ArrayList<Integer> arr2)\\n {\\n\\n // Minimum element of\\n // both arrays\\n int min1 = min_element(arr1);\\n int min2 = min_element(arr2);\\n\\n // Maximum element of\\n // both arrays\\n int max1 = max_element(arr1);\\n int max2 = max_element(arr2);\\n\\n // Generating all primes within\\n // the max range of arr1\\n ArrayList<Boolean> a = primegenerator(max1);\\n\\n // Generating all primes within\\n // the max range of arr2\\n ArrayList<Boolean> b = primegenerator(max2);\\n ArrayList<Integer> finala = new ArrayList<>();\\n ArrayList<Integer> finalb = new ArrayList<>();\\n\\n // Store precomputed values\\n Map<ArrayList<Integer>,Integer> dp = \\n new HashMap <ArrayList<Integer>,Integer> ();\\n\\n // Store all primes in arr1[]\\n for (int i = min1; i <= max1; i++) \\n {\\n if (arr1.contains(i)\\n && a.get(i) == true)\\n finala.add(i);\\n }\\n\\n // Store all primes of arr2[]\\n for (int i = min2; i <= max2; i++) \\n {\\n if (arr2.contains(i)\\n && b.get(i) == true)\\n finalb.add(i);\\n }\\n\\n // Calculating the LCS\\n return recursion(finala, finalb, 0, 0, dp);\\n }\\n\\n // Driver Code\\n public static void main(String args[])\\n {\\n int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };\\n int a2[] = { 2, 5, 6, 3, 7, 9, 8 };\\n\\n // Converting into list\\n ArrayList<Integer> arr1 = new ArrayList<Integer>();\\n for(int i = 0; i < a1.length; i++)\\n arr1.add(a1[i]);\\n\\n ArrayList<Integer> arr2 = new ArrayList<Integer>();\\n for(int i = 0; i < a2.length; i++)\\n arr2.add(a2[i]);\\n\\n // Function Call\\n System.out.println(longestCommonSubseq(arr1, arr2));\\n }\\n}\\n\\n", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 9, "problem_slug": "length-of-longest-common-prime-subsequence-from-two-given-arrays", "title": "Length of longest common prime subsequence from two given arrays", "problem_statement": "Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.", "sample_sno": 2, "input": "arr1[] = {1, 3, 5, 7, 9}, arr2[] = {2, 4, 6, 8, 10}", "output": "0\nExplanation:\nIn the above arrays, the prime subsequence of arr1[] is {1, 3, 5, 7} and arr2[] is {2}. Therefore, there is no common prime numbers which are present in both the arrays. Hence, the result is 0.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python implementation of the above approach\\n\\n# Function to calculate the LCS\\n\\ndef recursion(arr1, arr2, i, j, dp):\\n if i >= len(arr1) or j >= len(arr2):\\n return 0\\n key = (i, j)\\n if arr1[i] == arr2[j]:\\n return 1 + recursion(arr1, arr2,\\n i + 1, j + 1, dp)\\n if key in dp:\\n return dp[key]\\n else:\\n dp[key] = max(recursion(arr1, arr2,\\n i + 1, j, dp),\\n recursion(arr1, arr2,\\n i, j + 1, dp))\\n return dp[key]\\n\\n# Function to generate\\n# all the possible\\n# prime numbers\\n\\n\\ndef primegenerator(n):\\n cnt = 0\\n primes = [True for _ in range(n + 1)]\\n p = 2\\n while p * p <= n:\\n for i in range(p * p, n + 1, p):\\n primes[i] = False\\n p += 1\\n return primes\\n\\n# Function which returns the\\n# length of longest common\\n# prime subsequence\\n\\n\\ndef longestCommonSubseq(arr1, arr2):\\n\\n # Minimum element of\\n # both arrays\\n min1 = min(arr1)\\n min2 = min(arr2)\\n\\n # Maximum element of\\n # both arrays\\n max1 = max(arr1)\\n max2 = max(arr2)\\n\\n # Generating all primes within\\n # the max range of arr1\\n a = primegenerator(max1)\\n\\n # Generating all primes within\\n # the max range of arr2\\n b = primegenerator(max2)\\n\\n finala = []\\n finalb = []\\n\\n # Store precomputed values\\n dp = dict()\\n\\n # Store all primes in arr1[]\\n for i in range(min1, max1 + 1):\\n if i in arr1 and a[i] == True:\\n finala.append(i)\\n\\n # Store all primes of arr2[]\\n for i in range(min2, max2 + 1):\\n if i in arr2 and b[i] == True:\\n finalb.append(i)\\n\\n # Calculating the LCS\\n return recursion(finala, finalb,\\n 0, 0, dp)\\n\\n\\n# Driver Code\\narr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]\\narr2 = [2, 5, 6, 3, 7, 9, 8]\\n\\n# Function Call\\nprint(longestCommonSubseq(arr1, arr2))", "source_json_file": "length-of-longest-common-prime-subsequence-from-two-given-arrays.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 1, "input": "str = \"0000100001100001”, K = 4", "output": "0001100000011000\nExplanation: In the string \"0001100000011000\", every 2K length substring becomes a palindrome whenever a K-length prefix is removed, until string length reduces to K.", "solution_sno": 1, "solution_name": "sol1", "language": "cpp14", "code": "// C++ program for the above approach\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to count the number of\\n// zeroes present in the string\\nint count_zeroes(int n, string str)\\n{\\n int cnt = 0;\\n\\n // Traverse the string\\n for (int i = 0; i < str.size(); i++) {\\n if (str[i] == '0')\\n cnt++;\\n }\\n\\n // Return the count\\n return cnt;\\n}\\n\\n// Function to rearrange the string s.t\\n// the string can be reduced to a length\\n// K as per the given rules\\nstring kReducingStringUtil(\\n int n, int k, string str,\\n int no_of_zeroes)\\n{\\n\\n // Distribute the count of 0s and\\n // 1s in segment of length 2k\\n int zeroes_in_2k = ((no_of_zeroes)\\n * (2 * k))\\n / n;\\n\\n int ones_in_2k = 2 * k - zeroes_in_2k;\\n\\n // Store string that are initially\\n // have formed lexicographically\\n // smallest 2k length substring\\n string temp_str = \"\";\\n\\n for (int i = 0;\\n i < (zeroes_in_2k) / 2; i++) {\\n temp_str.push_back('0');\\n }\\n for (int i = 0; i < ones_in_2k; i++) {\\n temp_str.push_back('1');\\n }\\n for (int i = 0;\\n i < (zeroes_in_2k) / 2; i++) {\\n temp_str.push_back('0');\\n }\\n\\n // Store the lexicographically\\n // smallest string of length n\\n // that satisfy the condition\\n string final_str = \"\";\\n\\n // Insert temp_str into final_str\\n // (n/2k) times and add (n%2k)\\n // characters of temp_str at end\\n for (int i = 0;\\n i < n / (2 * k); i++) {\\n final_str += (temp_str);\\n }\\n\\n for (int i = 0;\\n i < n % (2 * k); i++) {\\n final_str.push_back(temp_str[i]);\\n }\\n\\n // Return the final string\\n return final_str;\\n}\\n\\n// Function to reduce the string to\\n// length K that follows the given\\n// conditions\\nstring kReducingString(int n, int k,\\n string str)\\n{\\n // If the string contains either\\n // 0s or 1s then it always be\\n // reduced into a K length string\\n int no_of_zeroes = count_zeroes(n, str);\\n\\n int no_of_ones = n - no_of_zeroes;\\n\\n // If the string contains only 0s\\n // 1s then it always reduces to\\n // a K length string\\n if (no_of_zeroes == 0\\n || no_of_zeroes == n) {\\n return str;\\n }\\n\\n // If K = 1\\n if (k == 1) {\\n if (no_of_zeroes == 0\\n || no_of_zeroes == n) {\\n return str;\\n }\\n else {\\n return \"Not Possible\";\\n }\\n }\\n\\n // Check whether the given string\\n // is K reducing string or not\\n bool check = 0;\\n\\n for (int i = (n / k);\\n i < n; i += (n / k)) {\\n\\n if (no_of_zeroes == i\\n || no_of_ones == i) {\\n check = 1;\\n break;\\n }\\n }\\n\\n if (check == 0) {\\n return \"Not Possible\";\\n }\\n\\n // Otherwise recursively find\\n // the string\\n return kReducingStringUtil(n, k,\\n str,\\n no_of_zeroes);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n string str = \"0000100001100001\";\\n int K = 4;\\n int N = str.length();\\n\\n // Function Call\\n cout << kReducingString(N, K, str);\\n\\n return 0;\\n}", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 1, "input": "str = \"0000100001100001”, K = 4", "output": "0001100000011000\nExplanation: In the string \"0001100000011000\", every 2K length substring becomes a palindrome whenever a K-length prefix is removed, until string length reduces to K.", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.io.*;\\n\\nclass InfobayAI{\\n\\n// Function to count the number of\\n// zeroes present in the string\\nstatic int count_zeroes(int n, String str)\\n{\\n int cnt = 0;\\n \\n // Traverse the string\\n for(int i = 0; i < str.length(); i++) \\n {\\n if (str.charAt(i) == '0')\\n cnt++;\\n }\\n\\n // Return the count\\n return cnt;\\n}\\n\\n// Function to rearrange the string s.t\\n// the string can be reduced to a length\\n// K as per the given rules\\nstatic String kReducingStringUtil(int n, int k, \\n String str,\\n int no_of_zeroes)\\n{\\n \\n // Distribute the count of 0s and\\n // 1s in segment of length 2k\\n int zeroes_in_2k = ((no_of_zeroes) * \\n (2 * k)) / n;\\n\\n int ones_in_2k = 2 * k - zeroes_in_2k;\\n\\n // Store string that are initially\\n // have formed lexicographically\\n // smallest 2k length substring\\n String temp_str = \"\";\\n\\n for(int i = 0; i < (zeroes_in_2k) / 2; i++) \\n {\\n temp_str += '0';\\n }\\n for(int i = 0; i < ones_in_2k; i++) \\n {\\n temp_str += '1';\\n }\\n for(int i = 0; i < (zeroes_in_2k) / 2; i++) \\n {\\n temp_str += '0';\\n }\\n \\n // Store the lexicographically\\n // smallest string of length n\\n // that satisfy the condition\\n String final_str = \"\";\\n\\n // Insert temp_str into final_str\\n // (n/2k) times and add (n%2k)\\n // characters of temp_str at end\\n for(int i = 0; i < n / (2 * k); i++) \\n {\\n final_str += (temp_str);\\n }\\n\\n for(int i = 0; i < n % (2 * k); i++) \\n {\\n final_str += temp_str.charAt(i);\\n }\\n\\n // Return the final string\\n return final_str;\\n}\\n\\n// Function to reduce the string to\\n// length K that follows the given\\n// conditions\\nstatic String kReducingString(int n, int k, \\n String str)\\n{\\n \\n // If the string contains either\\n // 0s or 1s then it always be\\n // reduced into a K length string\\n int no_of_zeroes = count_zeroes(n, str);\\n\\n int no_of_ones = n - no_of_zeroes;\\n\\n // If the string contains only 0s\\n // 1s then it always reduces to\\n // a K length string\\n if (no_of_zeroes == 0 || \\n no_of_zeroes == n) \\n {\\n return str;\\n }\\n\\n // If K = 1\\n if (k == 1) \\n {\\n if (no_of_zeroes == 0 || \\n no_of_zeroes == n)\\n {\\n return str;\\n }\\n else\\n {\\n return \"Not Possible\";\\n }\\n }\\n\\n // Check whether the given string\\n // is K reducing string or not\\n boolean check = false;\\n\\n for(int i = (n / k); i < n; i += (n / k)) \\n {\\n if (no_of_zeroes == i || \\n no_of_ones == i)\\n {\\n check = true;\\n break;\\n }\\n }\\n\\n if (check == false)\\n {\\n return \"Not Possible\";\\n }\\n\\n // Otherwise recursively find\\n // the string\\n return kReducingStringUtil(n, k, str,\\n no_of_zeroes);\\n}\\n\\n// Driver Code\\npublic static void main(String[] args)\\n{\\n String str = \"0000100001100001\";\\n int K = 4;\\n int N = str.length();\\n\\n // Function Call\\n System.out.println(kReducingString(N, K, str));\\n}\\n}\\n\\n", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 1, "input": "str = \"0000100001100001”, K = 4", "output": "0001100000011000\nExplanation: In the string \"0001100000011000\", every 2K length substring becomes a palindrome whenever a K-length prefix is removed, until string length reduces to K.", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 program for the above approach\\n\\n# Function to count the number of\\n# zeroes present in the string\\ndef count_zeroes(n, str):\\n \\n cnt = 0\\n\\n # Traverse the string\\n for i in range(0, len(str)):\\n if (str[i] == '0'):\\n cnt += 1\\n \\n # Return the count\\n return cnt\\n\\n# Function to rearrange the string s.t\\n# the string can be reduced to a length\\n# K as per the given rules\\ndef kReducingStringUtil(n, k, str, \\n no_of_zeroes):\\n \\n # Distribute the count of 0s and\\n # 1s in segment of length 2k\\n zeroes_in_2k = (((no_of_zeroes) * \\n (2 * k)) // n)\\n\\n ones_in_2k = 2 * k - zeroes_in_2k\\n\\n # Store string that are initially\\n # have formed lexicographically\\n # smallest 2k length substring\\n temp_str = \"\"\\n\\n for i in range(0, (zeroes_in_2k) // 2):\\n temp_str += '0'\\n \\n for i in range(0, (ones_in_2k)):\\n temp_str += '1'\\n \\n for i in range(0, (zeroes_in_2k) // 2):\\n temp_str += '0'\\n \\n # Store the lexicographically\\n # smallest string of length n\\n # that satisfy the condition\\n final_str = \"\"\\n\\n # Insert temp_str into final_str\\n # (n/2k) times and add (n%2k)\\n # characters of temp_str at end\\n for i in range(0, n // (2 * k)):\\n final_str += (temp_str)\\n\\n for i in range(0, n % (2 * k)):\\n final_str += (temp_str[i])\\n \\n # Return the final string\\n return final_str\\n\\n# Function to reduce the string to\\n# length K that follows the given\\n# conditions\\ndef kReducingString(n, k, str):\\n \\n # If the string contains either\\n # 0s or 1s then it always be\\n # reduced into a K length string\\n no_of_zeroes = count_zeroes(n, str)\\n\\n no_of_ones = n - no_of_zeroes\\n\\n # If the string contains only 0s\\n # 1s then it always reduces to\\n # a K length string\\n if (no_of_zeroes == 0 or \\n no_of_zeroes == n):\\n return str\\n\\n # If K = 1\\n if (k == 1):\\n if (no_of_zeroes == 0 or \\n no_of_zeroes == n):\\n return str\\n else:\\n return \"Not Possible\"\\n \\n # Check whether the given string\\n # is K reducing string or not\\n check = 0\\n\\n for i in range((n // k), n, (n // k)):\\n if (no_of_zeroes == i or no_of_ones == i):\\n check = 1\\n break\\n \\n if (check == 0):\\n return \"Not Possible\"\\n\\n # Otherwise recursively find\\n # the string\\n return kReducingStringUtil(n, k, str, \\n no_of_zeroes)\\n\\n# Driver Code\\nif __name__ == '__main__':\\n \\n str = \"0000100001100001\"\\n K = 4;\\n N = len(str)\\n\\n # Function Call\\n print(kReducingString(N, K, str))\\n \\n", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 2, "input": "str = \"100111\", K = 2", "output": "\"Not Possible\"", "solution_sno": 1, "solution_name": "sol1", "language": "cpp14", "code": "// C++ program for the above approach\\n\\n#include <bits/stdc++.h>\\nusing namespace std;\\n\\n// Function to count the number of\\n// zeroes present in the string\\nint count_zeroes(int n, string str)\\n{\\n int cnt = 0;\\n\\n // Traverse the string\\n for (int i = 0; i < str.size(); i++) {\\n if (str[i] == '0')\\n cnt++;\\n }\\n\\n // Return the count\\n return cnt;\\n}\\n\\n// Function to rearrange the string s.t\\n// the string can be reduced to a length\\n// K as per the given rules\\nstring kReducingStringUtil(\\n int n, int k, string str,\\n int no_of_zeroes)\\n{\\n\\n // Distribute the count of 0s and\\n // 1s in segment of length 2k\\n int zeroes_in_2k = ((no_of_zeroes)\\n * (2 * k))\\n / n;\\n\\n int ones_in_2k = 2 * k - zeroes_in_2k;\\n\\n // Store string that are initially\\n // have formed lexicographically\\n // smallest 2k length substring\\n string temp_str = \"\";\\n\\n for (int i = 0;\\n i < (zeroes_in_2k) / 2; i++) {\\n temp_str.push_back('0');\\n }\\n for (int i = 0; i < ones_in_2k; i++) {\\n temp_str.push_back('1');\\n }\\n for (int i = 0;\\n i < (zeroes_in_2k) / 2; i++) {\\n temp_str.push_back('0');\\n }\\n\\n // Store the lexicographically\\n // smallest string of length n\\n // that satisfy the condition\\n string final_str = \"\";\\n\\n // Insert temp_str into final_str\\n // (n/2k) times and add (n%2k)\\n // characters of temp_str at end\\n for (int i = 0;\\n i < n / (2 * k); i++) {\\n final_str += (temp_str);\\n }\\n\\n for (int i = 0;\\n i < n % (2 * k); i++) {\\n final_str.push_back(temp_str[i]);\\n }\\n\\n // Return the final string\\n return final_str;\\n}\\n\\n// Function to reduce the string to\\n// length K that follows the given\\n// conditions\\nstring kReducingString(int n, int k,\\n string str)\\n{\\n // If the string contains either\\n // 0s or 1s then it always be\\n // reduced into a K length string\\n int no_of_zeroes = count_zeroes(n, str);\\n\\n int no_of_ones = n - no_of_zeroes;\\n\\n // If the string contains only 0s\\n // 1s then it always reduces to\\n // a K length string\\n if (no_of_zeroes == 0\\n || no_of_zeroes == n) {\\n return str;\\n }\\n\\n // If K = 1\\n if (k == 1) {\\n if (no_of_zeroes == 0\\n || no_of_zeroes == n) {\\n return str;\\n }\\n else {\\n return \"Not Possible\";\\n }\\n }\\n\\n // Check whether the given string\\n // is K reducing string or not\\n bool check = 0;\\n\\n for (int i = (n / k);\\n i < n; i += (n / k)) {\\n\\n if (no_of_zeroes == i\\n || no_of_ones == i) {\\n check = 1;\\n break;\\n }\\n }\\n\\n if (check == 0) {\\n return \"Not Possible\";\\n }\\n\\n // Otherwise recursively find\\n // the string\\n return kReducingStringUtil(n, k,\\n str,\\n no_of_zeroes);\\n}\\n\\n// Driver Code\\nint main()\\n{\\n string str = \"0000100001100001\";\\n int K = 4;\\n int N = str.length();\\n\\n // Function Call\\n cout << kReducingString(N, K, str);\\n\\n return 0;\\n}", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 2, "input": "str = \"100111\", K = 2", "output": "\"Not Possible\"", "solution_sno": 2, "solution_name": "sol2", "language": "java", "code": "// Java program for the above approach\\nimport java.io.*;\\n\\nclass InfobayAI{\\n\\n// Function to count the number of\\n// zeroes present in the string\\nstatic int count_zeroes(int n, String str)\\n{\\n int cnt = 0;\\n \\n // Traverse the string\\n for(int i = 0; i < str.length(); i++) \\n {\\n if (str.charAt(i) == '0')\\n cnt++;\\n }\\n\\n // Return the count\\n return cnt;\\n}\\n\\n// Function to rearrange the string s.t\\n// the string can be reduced to a length\\n// K as per the given rules\\nstatic String kReducingStringUtil(int n, int k, \\n String str,\\n int no_of_zeroes)\\n{\\n \\n // Distribute the count of 0s and\\n // 1s in segment of length 2k\\n int zeroes_in_2k = ((no_of_zeroes) * \\n (2 * k)) / n;\\n\\n int ones_in_2k = 2 * k - zeroes_in_2k;\\n\\n // Store string that are initially\\n // have formed lexicographically\\n // smallest 2k length substring\\n String temp_str = \"\";\\n\\n for(int i = 0; i < (zeroes_in_2k) / 2; i++) \\n {\\n temp_str += '0';\\n }\\n for(int i = 0; i < ones_in_2k; i++) \\n {\\n temp_str += '1';\\n }\\n for(int i = 0; i < (zeroes_in_2k) / 2; i++) \\n {\\n temp_str += '0';\\n }\\n \\n // Store the lexicographically\\n // smallest string of length n\\n // that satisfy the condition\\n String final_str = \"\";\\n\\n // Insert temp_str into final_str\\n // (n/2k) times and add (n%2k)\\n // characters of temp_str at end\\n for(int i = 0; i < n / (2 * k); i++) \\n {\\n final_str += (temp_str);\\n }\\n\\n for(int i = 0; i < n % (2 * k); i++) \\n {\\n final_str += temp_str.charAt(i);\\n }\\n\\n // Return the final string\\n return final_str;\\n}\\n\\n// Function to reduce the string to\\n// length K that follows the given\\n// conditions\\nstatic String kReducingString(int n, int k, \\n String str)\\n{\\n \\n // If the string contains either\\n // 0s or 1s then it always be\\n // reduced into a K length string\\n int no_of_zeroes = count_zeroes(n, str);\\n\\n int no_of_ones = n - no_of_zeroes;\\n\\n // If the string contains only 0s\\n // 1s then it always reduces to\\n // a K length string\\n if (no_of_zeroes == 0 || \\n no_of_zeroes == n) \\n {\\n return str;\\n }\\n\\n // If K = 1\\n if (k == 1) \\n {\\n if (no_of_zeroes == 0 || \\n no_of_zeroes == n)\\n {\\n return str;\\n }\\n else\\n {\\n return \"Not Possible\";\\n }\\n }\\n\\n // Check whether the given string\\n // is K reducing string or not\\n boolean check = false;\\n\\n for(int i = (n / k); i < n; i += (n / k)) \\n {\\n if (no_of_zeroes == i || \\n no_of_ones == i)\\n {\\n check = true;\\n break;\\n }\\n }\\n\\n if (check == false)\\n {\\n return \"Not Possible\";\\n }\\n\\n // Otherwise recursively find\\n // the string\\n return kReducingStringUtil(n, k, str,\\n no_of_zeroes);\\n}\\n\\n// Driver Code\\npublic static void main(String[] args)\\n{\\n String str = \"0000100001100001\";\\n int K = 4;\\n int N = str.length();\\n\\n // Function Call\\n System.out.println(kReducingString(N, K, str));\\n}\\n}\\n\\n", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |
| {"problem_sno": 10, "problem_slug": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k", "title": "Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K", "problem_statement": "Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print \"Not Possible\".", "sample_sno": 2, "input": "str = \"100111\", K = 2", "output": "\"Not Possible\"", "solution_sno": 3, "solution_name": "sol3", "language": "python", "code": "# Python3 program for the above approach\\n\\n# Function to count the number of\\n# zeroes present in the string\\ndef count_zeroes(n, str):\\n \\n cnt = 0\\n\\n # Traverse the string\\n for i in range(0, len(str)):\\n if (str[i] == '0'):\\n cnt += 1\\n \\n # Return the count\\n return cnt\\n\\n# Function to rearrange the string s.t\\n# the string can be reduced to a length\\n# K as per the given rules\\ndef kReducingStringUtil(n, k, str, \\n no_of_zeroes):\\n \\n # Distribute the count of 0s and\\n # 1s in segment of length 2k\\n zeroes_in_2k = (((no_of_zeroes) * \\n (2 * k)) // n)\\n\\n ones_in_2k = 2 * k - zeroes_in_2k\\n\\n # Store string that are initially\\n # have formed lexicographically\\n # smallest 2k length substring\\n temp_str = \"\"\\n\\n for i in range(0, (zeroes_in_2k) // 2):\\n temp_str += '0'\\n \\n for i in range(0, (ones_in_2k)):\\n temp_str += '1'\\n \\n for i in range(0, (zeroes_in_2k) // 2):\\n temp_str += '0'\\n \\n # Store the lexicographically\\n # smallest string of length n\\n # that satisfy the condition\\n final_str = \"\"\\n\\n # Insert temp_str into final_str\\n # (n/2k) times and add (n%2k)\\n # characters of temp_str at end\\n for i in range(0, n // (2 * k)):\\n final_str += (temp_str)\\n\\n for i in range(0, n % (2 * k)):\\n final_str += (temp_str[i])\\n \\n # Return the final string\\n return final_str\\n\\n# Function to reduce the string to\\n# length K that follows the given\\n# conditions\\ndef kReducingString(n, k, str):\\n \\n # If the string contains either\\n # 0s or 1s then it always be\\n # reduced into a K length string\\n no_of_zeroes = count_zeroes(n, str)\\n\\n no_of_ones = n - no_of_zeroes\\n\\n # If the string contains only 0s\\n # 1s then it always reduces to\\n # a K length string\\n if (no_of_zeroes == 0 or \\n no_of_zeroes == n):\\n return str\\n\\n # If K = 1\\n if (k == 1):\\n if (no_of_zeroes == 0 or \\n no_of_zeroes == n):\\n return str\\n else:\\n return \"Not Possible\"\\n \\n # Check whether the given string\\n # is K reducing string or not\\n check = 0\\n\\n for i in range((n // k), n, (n // k)):\\n if (no_of_zeroes == i or no_of_ones == i):\\n check = 1\\n break\\n \\n if (check == 0):\\n return \"Not Possible\"\\n\\n # Otherwise recursively find\\n # the string\\n return kReducingStringUtil(n, k, str, \\n no_of_zeroes)\\n\\n# Driver Code\\nif __name__ == '__main__':\\n \\n str = \"0000100001100001\"\\n K = 4;\\n N = len(str)\\n\\n # Function Call\\n print(kReducingString(N, K, str))\\n \\n", "source_json_file": "lexicographically-smallest-permutation-of-a-string-that-can-be-reduced-to-length-k-by-removing-k-length-prefixes-from-palindromic-substrings-of-length-2k.json"} | |