Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
kallolathome
Active Participant
0 Kudos

Introduction


This is part of the Easy way to write algorithms in ABAP: Series 01. For more algorithms, please check the main blog-post.

Problem


permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for arr = [1,2,3], the following are all the permutations of arr[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].


The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].

  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].

  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.


Given an array of integers numsfind the next permutation of nums.

The replacement must be in place and use only constant extra memory.

Example 1:
Input: nums = [1,2,3]
Output: [1,3,2]

Example 2:
Input: nums = [3,2,1]
Output: [1,2,3]

Example 3:
Input: nums = [1,1,5]
Output: [1,5,1]

Constraints:

  • 1 <= nums.length <= 100

  • 0 <= nums[i] <= 100


Solution


Time Complexity: O(n)

Space Complexity: O(1)

ABAP


CLASS zcl_algo_nxtpermut DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
* Mandatory declaration
INTERFACES if_oo_adt_classrun.

PROTECTED SECTION.
PRIVATE SECTION.
TYPES ty_nums TYPE STANDARD TABLE OF i WITH EMPTY KEY.

METHODS getNextPermutation
CHANGING lt_nums TYPE STANDARD TABLE.

METHODS reverseNumbers
CHANGING lt_nums TYPE ty_nums
lv_i TYPE i
lv_j TYPE i.

METHODS swapNumbers
CHANGING lt_nums TYPE ty_nums
lv_i TYPE i
lv_j TYPE i.

ENDCLASS.


CLASS zcl_algo_nxtpermut IMPLEMENTATION.

METHOD if_oo_adt_classrun~main.

* lt_nums = VALUE ty_nums( ( 3 ) ( 2 ) ( 1 ) ).
DATA(lt_nums) = VALUE ty_nums( ( 1 ) ( 3 ) ( 5 ) ( 4 ) ( 2 ) ).
* lt_nums = VALUE ty_nums( ( 1 ) ( 2 ) ( 3 ) ).

* calling the method
getNextPermutation( CHANGING lt_nums = lt_nums ).

out->write( |next permutation:------->| ).
out->write( lt_nums ).

FREE lt_nums.

ENDMETHOD.

METHOD getNextPermutation.

DATA(lv_i) = 1.
DATA(lv_j) = 1.

* step: 1
LOOP AT lt_nums ASSIGNING FIELD-SYMBOL(<lf_wa>) STEP -1 FROM lines( lt_nums ) - 1.
IF sy-tabix GE 1 AND <lf_wa> < lt_nums[ sy-tabix + 1 ].
lv_i = lines( lt_nums ) + 1 - sy-tabix.
EXIT.
ENDIF.
ENDLOOP.

UNASSIGN <lf_wa>.

* base condition
IF lv_i = 1.
lv_j = lines( lt_nums ).
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).

ELSE.
* step: 2
LOOP AT lt_nums ASSIGNING <lf_wa> FROM 1 TO lv_i.
IF <lf_wa> > lt_nums[ lv_i ].
lv_j = sy-tabix - 1.
EXIT.
ENDIF.
ENDLOOP.
* step: 3
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).
* step: 4: manipulations
lv_j += 1.
lv_i = lines( lt_nums ).
* step: 5
reverseNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_j
lv_j = lv_i ).
ENDIF.

UNASSIGN <lf_wa>.
FREE: lv_i, lv_j.

ENDMETHOD.

METHOD reverseNumbers.
WHILE lv_i < lv_j.
swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_i
lv_j = lv_j ).

lv_i += 1.
lv_j -= 1.
ENDWHILE.

ENDMETHOD.

METHOD swapNumbers.
DATA(lv_temp) = 0.

* swapping
lv_temp = lt_nums[ lv_i ].
lt_nums[ lv_i ] = lt_nums[ lv_j ].
lt_nums[ lv_j ] = lv_temp.

FREE lv_temp.
ENDMETHOD.

ENDCLASS.

Python


class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""

# index of the first element that is smaller than the element to its right
lv_index = -1

for i in range(len(nums) - 1, 0, -1):
if nums[i] > nums[i - 1]:
lv_index = i - 1
break

# Base condition
if lv_index == -1:
reverse(nums, 0, len(nums) - 1)
return

j = len(nums) - 1

for i in range(len(nums) - 1, lv_index, -1):
if nums[i] > nums[lv_index]:
j = i
break

# swapping
nums[lv_index], nums[j] = nums[j], nums[lv_index]
# reversing
reverse(nums, lv_index + 1, len(nums) - 1)

# reverse method
def reverse(nums, i, j):
while i < j:
# swapping
nums[i], nums[j] = nums[j], nums[i]
# incrementing
i += 1
j -= 1

JavaScript


/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function (nums) {

if ((nums === null) || (nums.length <= 1)) return;

var i = nums.length - 2;

while (i >= 0 && nums[i] >= nums[i + 1]) i--;
if (i >= 0) {
var j = nums.length - 1;
while (nums[j] <= nums[i]) j--;
swap_numbers(nums, i, j);
}
/** reversing the right side of the array */
reverse_numbers(nums, i + 1, nums.length - 1);

};

function swap_numbers(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}

function reverse_numbers(array, i, j) {
while (i < j) swap_numbers(array, i++, j--);
}

 

N.B: For ABAP, I am using SAP BTP ABAP Environment 2211 Release.

Happy Coding! 🙂
3 Comments
Labels in this area