Skip to main content

Command Palette

Search for a command to run...

Can Place Flowers

Published
2 min read
Can Place Flowers
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 a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.

LeetCode Problem: Link | Click Here

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        // Initialize variables to keep track of the previous, current, and count of planted flowers
        int lp = 0; // previous flowerbed status
        int cp = flowerbed[0]; // current flowerbed status
        int count = 0; // count of flowers planted

        // Iterate through the flowerbed array starting from index 1
        for (int i = 1; i < flowerbed.length; i++) {
            // Check if the current, previous, and next flowerbed status allow planting a flower
            if (cp == 0 && cp == lp && cp == flowerbed[i]) {
                count++; // increment the count of planted flowers
                lp = 1; // update the previous flowerbed status to indicate a planted flower
            } else {
                lp = cp; // update the previous flowerbed status to the current flowerbed status
            }
            cp = flowerbed[i]; // update the current flowerbed status to the next element in the array
        }

        // Check if the last and second-to-last flowerbed statuses allow planting a flower
        if (lp == 0 && lp == cp) {
            count++; // increment the count if the last position allows planting a flower
        }

        // Check if the count of planted flowers is greater than or equal to the required count 'n'
        return count >= n;
    }
}

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.