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)