Skip to main content

Command Palette

Search for a command to run...

Remove One Element to Make the Array Strictly Increasing

Published
2 min read
Remove One Element to Make the Array Strictly Increasing
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.

Given a 0-indexed integer array nums, return true if it can be made strictly increasing after removing exactly one element, or false otherwise. If the array is already strictly increasing, return true.

The array nums is strictly increasing if nums[i - 1] < nums[i] for each index (1 <= i < nums.length).

LeetCode Problem - 1909

class Solution {
    // Method to check if an array can be made strictly increasing by removing at most one element
    public boolean canBeIncreasing(int[] nums) {
        // Converting the array to a list for easy manipulation
        List<Integer> list = new ArrayList<>();
        for (int e : nums) {
            list.add(e);
        }

        // Iterating through each element of the array
        for (int i = 0; i < nums.length; i++) {
            // Removing the current element to test if the remaining list is sorted
            list.remove(i);
            // Checking if the modified list is sorted
            boolean flag = checkSortedOrNot(list);
            // If not sorted, re-add the removed element to restore the original list
            if (!flag) {
                list.add(i, nums[i]);
            } else {
                // If the list is sorted after removing an element, it can be made strictly increasing
                return true;
            }
        }
        // If no combination leads to a strictly increasing list, return false
        return false;
    }

    // Method to check if a list is sorted in strictly increasing order
    public boolean checkSortedOrNot(List<Integer> list) {
        for (int i = 1; i < list.size(); i++) {
            // If any element is not greater than the previous one, return false
            if (!(list.get(i - 1) < list.get(i))) {
                return false;
            }
        }
        // If all elements are in strictly increasing order, return true
        return true;
    }
}

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.