Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse

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.

Input: a = 5, b = 4

Output: 0.0722389

// 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}


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.

Input: a = 5, b = 4

Output: 0.0722389

// 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


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.

Input: a = 5, b = 4

Output: 0.0722389

# 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


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.

Input: a = 7, b = 11

Output: 0.0202076

// 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}


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.

Input: a = 7, b = 11

Output: 0.0202076

// 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


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.

Input: a = 7, b = 11

Output: 0.0202076

# 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


Check if a king can move a valid move or not when N nights are there in a modified chessboard

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.

Input: a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}

Output:
Yes
The king cannot make any move as it has been check mate.

// 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}


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.

Input: a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}

Output:
Yes
The king cannot make any move as it has been check mate.

// 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


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.

Input: a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}

Output:
Yes
The king cannot make any move as it has been check mate.

# 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


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.

Input: a[] = { {1, 1} } king -> {3, 4}

Output:
No
The king can make valid moves.

// 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}


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.

Input: a[] = { {1, 1} } king -> {3, 4}

Output:
No
The king can make valid moves.

// 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


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.

Input: a[] = { {1, 1} } king -> {3, 4}

Output:
No
The king can make valid moves.

# 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


Check if Array has at least M non-overlapping Subarray with gcd G

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.

Input: A[] = {1, 2, 3, 4, 5}, M = 3

Output:
Yes
?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.
We can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where
gcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.
Thus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.

// 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


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.

Input: A[] = {1, 2, 3, 4, 5}, M = 3

Output:
Yes
?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.
We can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where
gcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.
Thus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.

// 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}


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.

Input: A[] = {1, 2, 3, 4, 5}, M = 3

Output:
Yes
?Explanation: Here, G = gcd(1, 2, 3, 4, 5) = 1.
We can choose 3 non overlapping subarrays {[1], [2, 3], [4, 5]} where
gcd(1) = 1, gcd(2, 3) = 1, and gcd(4, 5) = 1.
Thus, the average = (1 + 1 + 1)/3 = 1. Hence, we can have 3 such subarrays.

# 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


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.

Input: A[] = {6, 12, 18, 24}

Output: No

// 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


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.

Input: A[] = {6, 12, 18, 24}

Output: No

// 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}


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.

Input: A[] = {6, 12, 18, 24}

Output: No

# 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


Count arrays having at least K elements exceeding XOR of all given array elements by X given operations

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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {10, 2, 10, 5}, X = 3, K = 3

Output:
1
Explanation:
XOR 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.

// 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}


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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {10, 2, 10, 5}, X = 3, K = 3

Output:
1
Explanation:
XOR 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.

// 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


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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {10, 2, 10, 5}, X = 3, K = 3

Output:
1
Explanation:
XOR 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.

# 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


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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {3, 3, 4}, X = 3, K = 2

Output: 3

// 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}


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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {3, 3, 4}, X = 3, K = 2

Output: 3

// 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


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.
Select either first or last element from the given array.
Either increment the selected element by 1 or delete the selected element.

Input: arr[] = {3, 3, 4}, X = 3, K = 2

Output: 3

# 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


Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries

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.

Input: arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}

Output:
3 3 3 5 1
Explanation: For 1st query [2, 3], the subarray is {2, 2}.
The pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.
For 2nd query [2, 4], the subarray is {2, 2, 4}.
The pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.
Similarly, proceed for the further queries.

// 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}


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.

Input: arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}

Output:
3 3 3 5 1
Explanation: For 1st query [2, 3], the subarray is {2, 2}.
The pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.
For 2nd query [2, 4], the subarray is {2, 2, 4}.
The pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.
Similarly, proceed for the further queries.

// 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


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.

Input: arr[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}

Output:
3 3 3 5 1
Explanation: For 1st query [2, 3], the subarray is {2, 2}.
The pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.
For 2nd query [2, 4], the subarray is {2, 2, 4}.
The pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.
Similarly, proceed for the further queries.

# 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


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.

Input: arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}

Output: 3 3 9

// 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}


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.

Input: arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}

Output: 3 3 9

// 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


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.

Input: arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}

Output: 3 3 9

# 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


Count the number of subsequences of length k having equal LCM and HCF

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.

Input: Arr = {1, 2, 2, 3, 3}, K = 2

Output:
2
Subsequences are - {2, 2} and {3, 3}

// 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}


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.

Input: Arr = {1, 2, 2, 3, 3}, K = 2

Output:
2
Subsequences are - {2, 2} and {3, 3}

// 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


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.

Input: Arr = {1, 2, 2, 3, 3}, K = 2

Output:
2
Subsequences are - {2, 2} and {3, 3}

# 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


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.

Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3

Output: 4

// 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}


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.

Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3

Output: 4

// 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


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.

Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3

Output: 4

# 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


Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M

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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {4, 7, 5, 9, 3}, M = 4

Output:
808556639
Explanation:
There are five possible sets. They are:
{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.
Similarly, the remaining four sets along with their products are:
{4, 7, 9, 3} -> 108
{4, 5, 9, 3} -> 108
{4, 7, 5, 3} -> 84
{7, 5, 9, 3} -> 135
The total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639

// 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}


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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {4, 7, 5, 9, 3}, M = 4

Output:
808556639
Explanation:
There are five possible sets. They are:
{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.
Similarly, the remaining four sets along with their products are:
{4, 7, 9, 3} -> 108
{4, 5, 9, 3} -> 108
{4, 7, 5, 3} -> 84
{7, 5, 9, 3} -> 135
The total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639

// 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


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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {4, 7, 5, 9, 3}, M = 4

Output:
808556639
Explanation:
There are five possible sets. They are:
{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.
Similarly, the remaining four sets along with their products are:
{4, 7, 9, 3} -> 108
{4, 5, 9, 3} -> 108
{4, 7, 5, 3} -> 84
{7, 5, 9, 3} -> 135
The total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639

# 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


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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {7, 8, 9}, M = 2

Output: 254016

// 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}


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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {7, 8, 9}, M = 2

Output: 254016

// 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


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[].
Note: The product may be very large, take modulo to 109 + 7.

Input: arr[] = {7, 8, 9}, M = 2

Output: 254016

# 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


Hamiltonian Cycle

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.
If a graph has a Hamiltonian cycle, it's a Hamiltonian graph; otherwise, it's non-Hamiltonian.
Finding 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.
The Hamiltonian Cycle problem has applications in logistics, network design, and computer science.
Given 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".

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]

#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}