Given a list of integers, for each element find the first element to its right that is strictly greater. If no greater element exists to the right, use -1. Return a list of the same size where each position contains the next greater element for that position. Return null for null input and an empty list for empty input.

Examples:

Input: nums = [4, 5, 2, 10] Output: [5, 10, 10, -1] Explanation: Next greater for 4 is 5, for 5 is 10, for 2 is 10, for 10 there is none so -1 Input: nums = [3, 2, 1] Output: [-1, -1, -1] Explanation: No element has a greater element to its right Input: nums = [1, 2, 3] Output: [2, 3, -1] Explanation: Next greater for 1 is 2, for 2 is 3, for 3 there is none so -1 Input: nums = null Output: null Explanation: Null input returns null Input: nums = [] Output: [] Explanation: Empty list returns empty list
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Mar/April 2026

Contest runs March 3 - April 30. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.