DEV Community

Cover image for 1431. Kids With the Greatest Number of Candies
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on

1431. Kids With the Greatest Number of Candies

Introduction

Image description

So, here we are given an array. At first we have to figure out the max value of the array. Now we have to add the extraCandies value with index value and check whether it is larger or equal to max or not. If small store false otherwise true.

Examples

Image description

Steps

  1. Run a for loop and find the max value
  2. In the second loop check, if(candies[i] + extraCandies ≥ maximum) set true.
  3. Else set false

Hints

Image description

JavaCode

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> list = new ArrayList<Boolean>();
        int max = 0;
        for(int i = 0; i<candies.length; i++){
            if(max < candies[i]){
                max = candies[i];
            }
        }
        for(int i = 0; i<candies.length; i++){
                if(candies[i] + extraCandies >= max){
                    list.add(true);
                }
                else{
                    list.add(false);
                }
            }
            return list;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.