Skip to main content

Command Palette

Search for a command to run...

Arranging Coins

Published
2 min read
Arranging Coins
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 have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the i<sup>th</sup> row has exactly i coins. The last row of the staircase may be incomplete.

Given the integer n, return the number of complete rows of the staircase you will build.

LeetCode Problem - 441: Link | Click Here

class Solution {
    // Method to calculate the number of full steps that can be arranged
    public int arrangeCoins(int n) {
        // Base cases for n less than or equal to 3
        if (n <= 1) {
            return n;
        }
        if (n <= 3) {
            return (n == 3) ? 2 : 1;
        }

        // Initialize the search range
        long start = 2;
        long end = n / 2;

        // Binary search loop to find the maximum number of full steps
        while (start <= end) {
            // Calculate the middle value of the current search range
            long mid = start + (end - start) / 2;

            // Calculate the number of coins filled with mid full steps
            long coinsFilled = mid * (mid + 1) / 2;

            // Check if the current configuration exactly fills the given number of coins
            if (coinsFilled == n) {
                return (int) mid;  // Return the number of full steps
            }

            // Adjust the search range based on the calculated coins filled
            if (coinsFilled < n) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }

        // Return the maximum number of full steps that can be arranged
        return (int) end;
    }
}

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.

Arranging Coins