Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Next Greater Element II problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Next Greater Element II problem solution, we have given a circular integer array nums (i.e., the next element of nums[nums.length – 1] is nums[0]), return the next greater number for every element in nums.

The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn’t exist, return -1 for this number.

Leetcode Next Greater Element II problem solution

Leetcode Next Greater Element II problem solution in Python.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        output = [-1] * len(nums)
        # print(nums)
        for idx in range(0, len(nums)):
            idx2 = 0 if (idx == len(nums) -1) else idx + 1
            # print ('current Idx', idx, nums[idx])
            while (idx2 != idx):
                if (nums[idx] < nums[idx2]):
                    output[idx] = nums[idx2]
                    idx2 = idx
                    break
                idx2 = 0 if (idx2 == len(nums) -1) else idx2 + 1
        print('output', output)
        return output

Next Greater Element II problem solution in Java.

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int arr[] =new int[nums.length];
        boolean b = false;
        for(int i=0;i<nums.length;i++){
            b = false;
            for(int j=i+1;j<nums.length;j++){
                if(nums[j]>nums[i]){
                    arr[i] = nums[j];
                    b = true;
                    break;
                }                
            }
            if(!b){
            for(int j=0;j<i;j++){
                if(nums[j]>nums[i]){
                    arr[i] = nums[j];
                    b = true;
                    break;
                }       
            }
            }
            if(b == false)
                arr[i] = -1;
        }
        return arr;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        if(nums.size() == 0) return {};
        vector<int> ans(nums.size(), -1);
        int maxElem = nums[0];
        for(int i = 0; i<nums.size(); i++){
            if(maxElem < nums[i]) maxElem = nums[i];
        }
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] != maxElem){
                int j = i + 1;
                while( j != i){
                    if(j == nums.size()) j = 0;
                    else{
                        if(nums[j] > nums[i]){
                            ans[i] = nums[j];
                            break;
                        }
                        else j++;
                    }
                }
            }
        }
        return ans;
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes