Element Appearing More Than 25% in Sorted Array.

Gulshan KumarGulshan Kumar
1 min read

Q - Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

LeetCode Problem: Link | Click Here

class Solution {
    public int findSpecialInteger(int[] arr) {
        int n = arr.length; // Find the length of the array
        int percent25 = n / 4; // Calculate the threshold value, a quarter of the array length
        int result = 0; // Initialize the result variable to store the special integer

        Arrays.sort(arr); // Sort the array in ascending order

        // Loop through the array with a step of 'percent25'
        // Ensure enough elements are available for comparison by looping until (n - percent25)
        for (int i = 0; i < n - percent25; i++) {
            // Check if the current element is equal to an element 'percent25' positions ahead
            if (arr[i] == arr[i + percent25]) {
                result = arr[i]; // If found, update the 'result' variable with the special integer
            }
        }

        return result; // Return the special integer found
    }
}
0
Subscribe to my newsletter

Read articles from Gulshan Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gulshan Kumar
Gulshan Kumar

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.