✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(With Sorting)\n1. check function: Sorts the given array and
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(With Sorting)***\n1. **check function:** Sorts the given array and checks if the elements form an arithmetic sequence by verifying if the difference between consecutive elements remains constant.\n1. **checkArithmeticSubarrays function:** Loops through the provided ranges `l` and `r` to extract subarrays from `nums`, then checks each subarray using the `check` function to determine if they form arithmetic sequences.\n1. **ans vector:** Stores the boolean results indicating whether each subarray is an arithmetic sequence or not.\n1. **Return:** Returns the vector containing results for each subarray\'s arithmetic sequence check.\n\n# Complexity\n- *Time complexity:*\n   $$O(m\u22C5n\u22C5logn)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    // Function to check if the given array forms an arithmetic sequence\n    bool check(vector<int>& arr) {\n        sort(arr.begin(), arr.end()); // Sort the array\n        int diff = arr[1] - arr[0]; // Calculate the difference between the first two elements\n        \n        // Check if the difference remains the same for consecutive elements\n        for (int i = 2; i < arr.size(); i++) {\n            if (arr[i] - arr[i - 1] != diff) {\n                return false; // If the difference changes, it\'s not an arithmetic sequence\n            }\n        }\n        \n        return true; // If all differences are the same, it\'s an arithmetic sequence\n    }\n    \n    // Function to check arithmetic subarrays in the given range\n    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n        vector<bool> ans; // Vector to store results\n        \n        // Iterate through the ranges and check each subarray\n        for (int i = 0; i < l.size(); i++) {\n            // Extract the subarray from nums using the given range [l[i], r[i]]\n            vector<int> arr(begin(nums) + l[i], begin(nums) + r[i] + 1);\n            ans.push_back(check(arr)); // Check if the subarray forms an arithmetic sequence\n        }\n        \n        return ans; // Return the results for each subarray\n    }\n};\n\n\n```\n```C []\nbool check(int arr[], int size) {\n    // Sort the array\n    // (Implementation of sorting is not provided here)\n\n    int diff = arr[1] - arr[0]; // Calculate the difference between the first two elements\n        \n    // Check if the difference remains the same for consecutive elements\n    for (int i = 2; i < size; i++) {\n        if (arr[i] - arr[i - 1] != diff) {\n            return false; // If the difference changes, it\'s not an arithmetic sequence\n        }\n    }\n\n    return true; // If all differences are the same, it\'s an arithmetic sequence\n}\n\n// Function to check arithmetic subarrays in the given range\nvoid checkArithmeticSubarrays(int nums[], int numsSize, int l[], int r[], int queriesSize) {\n    bool ans[queriesSize]; // Array to store results\n    \n    // Iterate through the ranges and check each subarray\n    for (int i = 0; i < queriesSize; i++) {\n        int subarraySize = r[i] - l[i] + 1;\n        int arr[subarraySize]; // Create a subarray\n        \n        // Extract the subarray from nums using the given range [l[i], r[i]]\n        int index = 0;\n        for (int j = l[i]; j <= r[i]; j++) {\n            arr[index++] = nums[j];\n        }\n        \n        ans[i] = check(arr, subarraySize); // Check if the subarray forms an arithmetic sequence\n    }\n    \n    // Printing the results\n    for (int i = 0; i < queriesSize; i++) {\n        if (ans[i]) {\n            printf("true ");\n        } else {\n            printf("false ");\n        }\n    }\n    printf("\\n");\n}\n\n\n```\n\n```Java []\nclass Solution {\n    public Boolean check(int[] arr) {\n        Arrays.sort(arr);\n        int diff = arr[1] - arr[0];\n        \n        for (int i = 2; i < arr.length; i++) {\n            if (arr[i] - arr[i - 1] != diff) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n    \n    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {\n        List<Boolean> ans = new ArrayList();\n        for (int i = 0; i < l.length; i++) {\n            int[] arr = new int[r[i] - l[i] + 1];\n            for (int j = 0; j < arr.length; j++) {\n                arr[j] = nums[l[i] + j];\n            }\n            \n            ans.add(check(arr));\n        }\n\n        return ans;\n    }\n}\n\n```\n\n```python3 []\n\nclass Solution:\n    def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:\n        def check(arr):\n            arr.sort()\n            diff = arr[1] - arr[0]\n            \n            for i in range(2, len(arr)):\n                if arr[i] - arr[i - 1] != diff:\n                    return False\n                \n            return True\n        \n        ans = []\n        for i in range(len(l)):\n            arr = nums[l[i] : r[i] + 1]\n            ans.append(check(arr))\n        \n        return ans\n\n```\n```javascript []\nfunction check(arr) {\n    arr.sort((a, b) => a - b); // Sort the array\n    let diff = arr[1] - arr[0]; // Calculate the difference between the first two elements\n\n    // Check if the difference remains the same for consecutive elements\n    for (let i = 2; i < arr.length; i++) {\n        if (arr[i] - arr[i - 1] !== diff) {\n            return false; // If the difference changes, it\'s not an arithmetic sequence\n        }\n    }\n\n    return true; // If all differences are the same, it\'s an arithmetic sequence\n}\n\nfunction checkArithmeticSubarrays(nums, l, r) {\n    const ans = [];\n\n    // Iterate through the ranges and check each subarray\n    for (let i = 0; i < l.length; i++) {\n        const arr = nums.slice(l[i], r[i] + 1); // Extract the subarray from nums using the given range [l[i], r[i]]\n        ans.push(check(arr)); // Check if the subarray forms an arithmetic sequence\n    }\n\n    return ans; // Return the results for each subarray\n}\n\n```\n\n---\n\n#### ***Approach 2(Without Sorting)***\n1. **Libraries included:**\n\n    - **<vector>:** To use the `vector` container for arrays.\n    - **<unordered_set>:** To utilize an unordered set for efficient element lookup.\n    - **<climits>:** For using `INT_MAX` and `INT_MIN` constants for integer limits.\n1. **Class Solution:**\n\n    - Contains two methods:\n        - **bool check(vector<int>& arr):** Checks if an array forms an arithmetic sequence.\n        - **vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r):** Checks arithmetic subarrays in the given range.\n1. **bool check(vector<int>& arr) function:**\n\n    - Calculates minimum and maximum elements in the array and stores them in `minElement` and `maxElement`.\n    - Populates an `unordered_set` named `arrSet` with elements from the array `arr`.\n    - Determines if the array elements can form an arithmetic sequence:\n        - Computes the expected common difference diff if the elements are in an arithmetic sequence.\n        - Validates the sequence by checking if the elements satisfy the arithmetic sequence condition:\n            - Verifies the evenly spaced property using the modulus operation.\n            - Validates if the expected elements are present by checking each one against the set.\n1. **vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) function:**\n\n    - Iterates through the ranges defined by vectors `l` and `r`.\n    - Extracts subarrays from `nums` based on the provided range.\n    - Applies the `check` function to each subarray and stores the result in the `ans` vector.\n1. **Overall Process:**\n\n    - The code first defines methods to verify if a given array is an arithmetic sequence and then applies this check to multiple subarrays within a range.\n\n# Complexity\n- *Time complexity:*\n   $$O(m\u22C5n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    // Function to check if the given array forms an arithmetic sequence\n    bool check(vector<int>& arr) {\n        int minElement = INT_MAX; // Variable to store the minimum element in the array\n        int maxElement = INT_MIN; // Variable to store the maximum element in the array\n        unordered_set<int> arrSet; // Set to store elements of the array\n        \n        // Finding minimum and maximum elements and populating the set\n        for (int num : arr) {\n            minElement = min(minElement, num);\n            maxElement = max(maxElement, num);\n            arrSet.insert(num);\n        }\n        \n        // Check if the elements can form an arithmetic sequence\n        if ((maxElement - minElement) % (arr.size() - 1) != 0) {\n            return false; // If not evenly spaced, it\'s not an arithmetic sequence\n        }\n        \n        int diff = (maxElement - minElement) / (arr.size() - 1); // Calculate the common difference\n        int curr = minElement + diff; // Initialize current element\n        \n        // Check for each expected element in the sequence\n        while (curr < maxElement) {\n            if (arrSet.find(curr) == arrSet.end()) {\n                return false; // If any element is missing, it\'s not an arithmetic sequence\n            }\n            curr += diff; // Move to the next expected element\n        }\n        \n        return true; // If all conditions satisfy, it\'s an arithmetic sequence\n    }\n    \n    // Function to check arithmetic subarrays in the given range\n    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n        vector<bool> ans; // Vector to store results\n        \n        // Iterate through the ranges and check each subarray\n        for (int i = 0; i < l.size(); i++) {\n            // Extract the subarray from nums using the given range [l[i], r[i]]\n            vector<int> arr(begin(nums) + l[i], begin(nums) + r[i] + 1);\n            ans.push_back(check(arr)); // Check if the subarray forms an arithmetic sequence\n        }\n        \n        return ans; // Return the results for each subarray\n    }\n};\n\n\n\n```\n```C []\n\nint check(int arr[], int size) {\n    int minElement = INT_MAX; // Variable to store the minimum element in the array\n    int maxElement = INT_MIN; // Variable to store the maximum element in the array\n    int arrSet[MAX_SIZE] = {0}; // Array to store elements of the array\n    \n    // Finding minimum and maximum elements and populating the set\n    for (int i = 0; i < size; i++) {\n        minElement = (arr[i] < minElement) ? arr[i] : minElement;\n        maxElement = (arr[i] > maxElement) ? arr[i] : maxElement;\n        arrSet[arr[i]] = 1;\n    }\n    \n    // Check if the elements can form an arithmetic sequence\n    if ((maxElement - minElement) % (size - 1) != 0) {\n        return 0; // If not evenly spaced, it\'s not an arithmetic sequence\n    }\n    \n    int diff = (maxElement - minElement) / (size - 1); // Calculate the common difference\n    int curr = minElement + diff; // Initialize current element\n    \n    // Check for each expected element in the sequence\n    while (curr < maxElement) {\n        if (arrSet[curr] == 0) {\n            return 0; // If any element is missing, it\'s not an arithmetic sequence\n        }\n        curr += diff; // Move to the next expected element\n    }\n    \n    return 1; // If all conditions satisfy, it\'s an arithmetic sequence\n}\n\n// Function to check arithmetic subarrays in the given range\nint* checkArithmeticSubarrays(int* nums, int* l, int* r, int size) {\n    int* ans = (int*)malloc(size * sizeof(int)); // Array to store results\n    \n    // Iterate through the ranges and check each subarray\n    for (int i = 0; i < size; i++) {\n        int subArraySize = r[i] - l[i] + 1;\n        int subArray[subArraySize];\n        \n        // Extract the subarray from nums using the given range [l[i], r[i]]\n        for (int j = l[i], k = 0; j <= r[i]; j++, k++) {\n            subArray[k] = nums[j];\n        }\n        \n        ans[i] = check(subArray, subArraySize); // Check if the subarray forms an arithmetic sequence\n    }\n    \n    return ans; // Return the results for each subarray\n}\n\n\n```\n\n```Java []\nclass Solution {\n    public Boolean check(int[] arr) {\n        int minElement = Integer.MAX_VALUE;\n        int maxElement = Integer.MIN_VALUE;\n        Set<Integer> arrSet = new HashSet();\n        \n        for (int num : arr) {\n            minElement = Math.min(minElement, num);\n            maxElement = Math.max(maxElement, num);\n            arrSet.add(num);\n        }\n        \n        if ((maxElement - minElement) % (arr.length - 1) != 0) {\n            return false;\n        }\n        \n        int diff = (maxElement - minElement) / (arr.length - 1);\n        int curr = minElement + diff;\n        \n        while (curr < maxElement) {\n            if (!arrSet.contains(curr)) {\n                return false;\n            }\n            \n            curr += diff;\n        }\n        \n        return true;\n    }\n    \n    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {\n        List<Boolean> ans = new ArrayList();\n        for (int i = 0; i < l.length; i++) {\n            int[] arr = new int[r[i] - l[i] + 1];\n            for (int j = 0; j < arr.length; j++) {\n                arr[j] = nums[l[i] + j];\n            }\n            \n            ans.add(check(arr));\n        }\n\n        return ans;\n    }\n}\n\n```\n\n```python3 []\nclass Solution:\n    def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:\n        def check(arr):\n            min_element = min(arr)\n            max_element = max(arr)\n\n            if (max_element - min_element) % (len(arr) - 1) != 0:\n                return False\n\n            diff = (max_element - min_element) / (len(arr) - 1)\n            \n            arr_set = set(arr)\n            curr = min_element + diff\n            while curr < max_element:\n                if curr not in arr_set:\n                    return False\n                \n                curr += diff\n            \n            return True\n\n        ans = []\n        for i in range(len(l)):\n            arr = nums[l[i] : r[i] + 1]\n            ans.append(check(arr))\n        \n        return ans\n\n\n```\n```javascript []\nclass Solution {\n    check(arr) {\n        let minElement = Number.MAX_SAFE_INTEGER; // Variable to store the minimum element in the array\n        let maxElement = Number.MIN_SAFE_INTEGER; // Variable to store the maximum element in the array\n        let arrSet = new Set(); // Set to store elements of the array\n        \n        // Finding minimum and maximum elements and populating the set\n        arr.forEach(num => {\n            minElement = Math.min(minElement, num);\n            maxElement = Math.max(maxElement, num);\n            arrSet.add(num);\n        });\n        \n        // Check if the elements can form an arithmetic sequence\n        if ((maxElement - minElement) % (arr.length - 1) !== 0) {\n            return false; // If not evenly spaced, it\'s not an arithmetic sequence\n        }\n        \n        let diff = (maxElement - minElement) / (arr.length - 1); // Calculate the common difference\n        let curr = minElement + diff; // Initialize current element\n        \n        // Check for each expected element in the sequence\n        while (curr < maxElement) {\n            if (!arrSet.has(curr)) {\n                return false; // If any element is missing, it\'s not an arithmetic sequence\n            }\n            curr += diff; // Move to the next expected element\n        }\n        \n        return true; // If all conditions satisfy, it\'s an arithmetic sequence\n    }\n    \n    checkArithmeticSubarrays(nums, l, r) {\n        let ans = []; // Array to store results\n        \n        // Iterate through the ranges and check each subarray\n        for (let i = 0; i < l.length; i++) {\n            // Extract the subarray from nums using the given range [l[i], r[i]]\n            let arr = nums.slice(l[i], r[i] + 1);\n            ans.push(this.check(arr)); // Check if the subarray forms an arithmetic sequence\n        }\n        \n        return ans; // Return the results for each subarray\n    }\n}\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'C', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']