Skip to main content

Command Palette

Search for a command to run...

Arithmetic Subarrays

Published
2 min read
Arithmetic Subarrays
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.

For example, these are arithmetic sequences:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic:

1, 1, 2, 5, 7

You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the i<sup>th</sup> query is the range [l[i], r[i]]. All the arrays are 0-indexed.

Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.

LeetCode Problem - 1630

class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        // Initialize the result list to store boolean values
        List<Boolean> result = new ArrayList<>();

        // Iterate through each query defined by l and r
        for (int i = 0; i < l.length; i++) {
            // Extract the subarray based on the current range [l[i], r[i]]
            int[] subArray1 = Arrays.copyOfRange(nums, l[i], r[i] + 1);
            // Check if the subarray is arithmetic and add the result to the list
            result.add(checkArithmetic(subArray1));
        }
        return result; // Return the list of boolean results
    }

    // Method to check if a given array is arithmetic
    public boolean checkArithmetic(int[] subArray) {
        // Sort the subarray to check for arithmetic sequence
        Arrays.sort(subArray);
        // Calculate the common difference of the arithmetic sequence
        int flag = subArray[1] - subArray[0];
        // Iterate through the sorted subarray to check for consistent difference
        for (int i = 1; i < subArray.length - 1; i++) {
            if (flag != (subArray[i + 1] - subArray[i])) {
                return false; // Return false if the difference is not consistent
            }
        }
        return true; // Return true if the array is arithmetic
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.