// SOLVING THIS WITH AN AI ASSISTANT (2026)
If you are working through this problem with an AI coding assistant — Claude, ChatGPT, Cursor chat, Gemini, GitHub Copilot, Aider, or any agent — the goal isn’t to ask for the answer. It is to use the tool to understand the pattern. The prompt sequence I’d run:
The pattern matters more than the answer. If the agent just hands you optimized code, you’ve trained yourself to lose interviews.
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
Solution 1:
class Solution {
public void nextPermutation(int[] nums) {
if(nums == null || nums.length <=1) return;
int permIndex = -1;
for(int i=nums.length-1;i>=1;i--){
if(nums[i-1] <nums[i]){
permIndex = i-1;
break;
}
}
//reverse if permIndex = -1
if(permIndex == -1){
reverse(nums, 0, nums.length-1);
return;
}
//if permIndex+1 = nums.length-1 swap n-1,n
if(permIndex!=-1 && permIndex+1 == nums.length-1){
swap(nums, permIndex, permIndex+1);
return;
}
int tempValue = nums[permIndex];
int nextGreaterElementIndex = Integer.MIN_VALUE;
for(int i=nums.length-1;i>=permIndex+1;i--){
if(nums[i]>nums[permIndex]){
nextGreaterElementIndex = i;
break;
}
}
swap(nums, permIndex, nextGreaterElementIndex);
reverse(nums, permIndex+1, nums.length-1);
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void reverse(int[] nums, int i, int j){
while(i<j){
swap(nums, i++, j--);
}
return;
}
} For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.
HackerHeap is back: a multi-platform resource for working developers building with AI coding agents. We…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// BUILDING THIS WITH AN AI AGENT (2026)Whether you are using Claude Code, Cursor, Windsurf,…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…
This website uses cookies.