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!

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 October 2026 (Oct 16–31)Sponsored by FLXBL

Contest runs October 16 - 31. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 16 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In