Skip to main content

Command Palette

Search for a command to run...

Special Array With X Elements Greater Than or Equal X

Published
2 min read
Special Array With X Elements Greater Than or Equal X
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.

You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.

Notice that x does not have to be an element in nums.

Return x if the array is special*, otherwise, return -1. It can be proven that if nums is special, the value for x is *unique.

LeetCode Problem - 1608

class Solution {
    public int specialArray(int[] nums) {
        // Initialize the result variable to -1
        int result = -1;
        // Iterate through each possible value of the special array
        for (int i=0; i<=nums.length; i++){
            // Initialize a counter to count the number of elements greater than or equal to the current value of i
            int count = 0;
            // Iterate through each element in the input array
            for (int num : nums) {
                // If the element is greater than or equal to the current value of i, increment the count
                if (num >= i) {
                    count++;
                } 
                // If the count exceeds the current value of i, break out of the loop
                else if (count > i) break;
            }
            // If the count equals the current value of i, return i
            if (count==i){
                return count;
            }
        }
        // If no special array is found, return the initialized result (-1)
        return result;
    }
}

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.