0% found this document useful (0 votes)
5 views

Data_Structures_and_Algorithms_in_ABAP_1691932213

The document discusses the use of data structures and algorithms (DSA) in SAP ABAP, highlighting common data structures like strings and internal tables. It presents problem-solving techniques, including brute force and optimized approaches such as the sliding window and two-pointer techniques, to find maximum sums and target sums in arrays. Sample ABAP code is provided for each approach, demonstrating practical applications of DSA in ABAP programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data_Structures_and_Algorithms_in_ABAP_1691932213

The document discusses the use of data structures and algorithms (DSA) in SAP ABAP, highlighting common data structures like strings and internal tables. It presents problem-solving techniques, including brute force and optimized approaches such as the sliding window and two-pointer techniques, to find maximum sums and target sums in arrays. Sample ABAP code is provided for each approach, demonstrating practical applications of DSA in ABAP programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Data Structures and Algorithms in SAP ABAP

We would be aware of various Data Structures (DS) available in


computer science e.g. Arrays, Linked List, Stacks, Queue, Graphs
etc. These data structures are widely used in every programming
language and ABAP is not an exception. In our day-to-day life
with ABAP programming, we use these DS directly or sometimes
unknowingly.

Also other programming language developers use the abbreviation DSA. What
actually is DSA? It is nothing but the title of this series, Data Structure and
Algorithm. If you visit leetcode or geekforgeeks you would find tons and tons of
contents on DSA. Java, Python, JavaScript etc has been using DSA from ages and
DSA is one of the core questions in interviews and selections process. In ABAP
also, we have DSA but we ABAP Developers do not use those Fancy terminologies.
We would rather try to understand the clients business and find the solution. After
all ABAP is Business Application Programming.

Some common Data Structures used in ABAP are:


1. String – Arrays of characters
2. Internal Table – Arrays with multiple rows and columns

Really, strings and internal tables are DSAs. Why should non-ABAP
developers have all the fun?
In this blog series, we will see the usage of the above data structures to
solve some problems using known algorithms and sometimes, we will
implement our own algorithm by using some widely known problem solving
techniques.
Problem: Given an array of integers of size ‘n’, find the
maximum sum of ‘k’ consecuCve elements in the array.
Input : arr[ ] = { 5, 1, 2, 6, 4, 3 } , k = 2
Output: 10

If you appear interview of FAANG companies (oops, these days it is MAANG, M for
Meta and not Microsoft ), you might not get SAP Business related problem
solving. You might be asked questions like above. Or may be find the prime
numbers. Seriously..

Brute Force Approach:


A simple and consistent approach to solve any programming problem which would
find a solution with all possible choices. Since it executes all possible options,
time complexity would often be very high (do remember this terminology,
time complexity and in future series we will also utter space complexity). For
above problem, we can think of following solution:

1. Loop through all the elements as a starting point for a group.


2. Inner loop for every group of K elements.
3. Calculate the sum and check if it’s greater than the sum of the previous
iteration. If yes, assign it as Maximum sum.
4. After all iterations, Return the maximum sum value to the output.

Let’s see how many Iterations will be needed to loop through all elements in the
input array.

We can observe that all Iterations for outer loop would not be required but It can
be derived by simple observation by considering the no. of places for consecutive
elements in the problem statement.
No. of Iterations required : N + 1 – K
N : size of array
K : no. of consecutive elements

Sample ABAP code :


" Brute Force Approach
" Data and Type Declara5on TYPES: tyt_input TYPE TABLE
OF i WITH EMPTY KEY. DATA: lv_k TYPE i,
lv_max_sum TYPE i. "Input Array
DATA(lt_input) = VALUE tyt_input( ( 5 ) ( 1 ) ( 2 ) ( 6 ) ( 4 ) ( 3 ) ). "Calculate length of array
DATA(lv_lines) = lines( lt_input ).
"Input for no. of consecu5ve elements cl_demo_input=>add_field( CHANGING field
= lv_k ). cl_demo_input=>request( ).
DO lv_lines + 1 - lv_k TIMES. "Outer Loop for total required Itera5ons DATA(lv_idx1) = sy-index.
DATA(lv_idx2) = sy-index + lv_k - 1.
"Inner loop to calculate sum of each group with K elements
DATA(lv_sum) = REDUCE i( INIT i = 0
FOR lv_input IN lt_input FROM lv_idx1 TO lv_idx2 NEXT i += lv_input
). "Derive maximum sum of current and prev Itera5ons lv_max_sum = nmax( val1 = lv_sum val2 =
lv_max_sum ).
CLEAR: lv_sum.
ENDDO. cl_demo_output=>display(
lv_max_sum ).

OpCmized Approach:
This problem can be optimized using the famous sliding window technique.
Now we are also getting fancy.

The Sliding Window is a problem-solving technique of data structure and algorithm


for problems that apply arrays or lists. These problems are painless to solve using
a brute force approach in O(n²) or O(n³). However, the Sliding Window
Technique can reduce the time complexity to O(n). In a nutshell, it can reduce
the nested loops and execute the program in an efficient way.

Applying Sliding Window Technique :


1. We compute the sum of first k elements out of n terms using a linear loop and
store the sum in variable current_sum.
2. Then we will graze linearly over the array till it reaches the end and
simultaneously keep track of maximum_sum.
3. To get the current sum of blocks of k elements just subtract the first element
from the previous block and add the last element of the current block .

Consider an array arr[] = { 5, 1, 2, 6, 4, 3 } and value of k = 2 and n = 6 This


is the initial phase where we have calculated the initial current sum starting
from index

1 . At this stage the window current_sum is 6. Now, we set the maximum_sum as


current_sum i.e. 6.
Now, we slide our window by a unit index. Therefore, now it discards 5
from the window and adds 2 to the window. Hence, we will get our new
window sum by subtracting 5 and then adding 2 to it. So, our window
current_sum now becomes 3. Now, we will compare this window
current_sum with the maximum_sum. As it is smaller we won’t change the
maximum_sum.

Similarly, now once again we slide our window by a unit index and obtain the new
window sum to be 8. Again we check if this current window sum is greater than
the maximum_sum till now. Since it is greater, we will change the maximum_sum
with current_sum i.e. 8.

Now once again we slide our window by a unit index and obtain the new window
sum to be
10. Again we check if this current window sum is greater than the
maximum_sum till now. Since it is greater, we will change the maximum_sum
with current_sum i.e. 10.

Similarly, now once again we slide our window by a unit index and obtain the new
window sum to be 7. Again we check if this current window sum is greater than
the maximum_sum till now. As it is smaller so we don’t change the
maximum_sum.

Since we reach the end of the elements, we will stop searching for maximum sum
and assign the calculated maximum_sum to output.
No. of Iterations required : N – K
N : size of array
K : no. of consecutive elements

Sample ABAP code :


" Sliding Window Technique
" Data and Type Declara5on TYPES: tyt_input TYPE TABLE OF
i WITH EMPTY KEY.
DATA: lv_k TYPE i.
"Input Array
DATA(lt_input) = VALUE tyt_input( ( 5 ) ( 1 ) ( 2 ) ( 6 ) ( 4 ) ( 3 ) ).
"Input for no. of consecu5ve elements cl_demo_input=>add_field( CHANGING field
= lv_k ). cl_demo_input=>request( ).
"Calculate sum of first k elements star5ng from 1st index
DATA(lv_current_sum) = REDUCE i( INIT result = 0
FOR lv_input IN lt_input FROM 1 TO lv_k NEXT result +=
lv_input ).
"Assign current sum in the max sum DATA(lv_max_sum) =
lv_current_sum.
"Itera5on from (K + 1)th element 5ll end of elements LOOP AT lt_input INTO DATA(lv_input1) FROM
lv_k + 1 TO lines( lt_input ). lv_current_sum += lv_input1 - lt_input[ sy-tabix - lv_k ]. lv_max_sum =
nmax( val1 = lv_current_sum val2 = lv_max_sum ).
ENDLOOP.
cl_demo_output=>display( lv_max_sum ).
Problem: Given an array of integers, return the indices
of the two numbers whose sum is equal to a given
target.
Input : arr[ ] = [ 5, 1, 2, 6, 4, 3 } , k = 10
Output: [ 4,5 ] as arr[4] + arr[5] = 10
Brute Force Approach:
For the above problem, the naïve approach is to just use two nested For loops and
check if the sum of any two elements in the array is equal to the given target.

1. Outer Loop through all the elements starting from the 1st index.
2. Inner loop through all the elements starting from the next index till end of
array .
3. Calculate the sum and check if it’s equal to the target. If yes, append the
index from outer and inner loops in the result table and EXIT.

1st Iteration on outer loop:

As we have seen that after all inner iterations, we couldn’t find the valid pair
which would provide the target sum. Therefore, Go to next Iteration on Outer
Loop and start iterating the array from next index till end in inner loop. 2nd
Iteration on outer loop:
Again after all iterations of the inner loop, we couldn’t get the required pair so
iterated to the next element in the Outer Loop.
3rd Iteration on outer loop:

We will see that now in 4th Iteration of outer loop, we will get the required result:
4th Iteration on outer loop:
Now lets come back to our topic of discussion, as soon as we get the exact pair
whose sum equals the target value, we will return the indexes from outer and
inner loops and display the output. Please note that we don’t need the outer loop
till all elements. Since we always need to find 2 integers for output, It would be
enough to iterate the outer loop by N – 1.

No. of Iterations required : (N – 1) * N


N : size of array

Time complexity will be O(N ^ N) since there are 2 nested loops iterated till end.
Although we may not need to iterate through all the elements in the outer loop.

Now lets try to show ABAP code in OLD and NEW syntaxes separately so that
readers can easily observe the usage of new syntax.

Sample Old ABAP Syntax:


Sample Source Code:
TYPES: tyt_input TYPE TABLE OF i WITH EMPTY KEY.
DATA: lv_k TYPE i.
“Input Array
DATA(lt_input) = VALUE tyt_input( ( 7 ) ( 10 ) ( 30 ) ( 9 ) ( 3 ) ).
"Input for target sum cl_demo_input=>add_field( CHANGING
field = lv_k ). cl_demo_input=>request( ).
" Brute Force
" Old ABAP syntax
DATA: lt_output TYPE tyt_input, lv_exit TYPE
abap_bool.
“Outer Loop 5ll last required element LOOP AT lt_input INTO DATA(lv_input1) TO lines(
lt_input ) - 1.
“ Current index
DATA(lv_tabix1) = sy-tabix.
“ Inner loop star5ng from next index
LOOP AT lt_input INTO DATA(lv_input2) FROM lv_tabix1 + 1.
DATA(lv_tabix2) = sy-tabix.
“ Condi5on to check if sum of values at current indexes equals to target
IF lv_input1 + lv_input2 = lv_k. APPEND : lv_tabix1 TO lt_output,
lv_tabix1 TO lt_output.
“ Exit flag lv_exit =
abap_true.
ENDIF.
“ EXIT from inner loop if target is found
IF ( lv_exit = abap_true ).
EXIT.
ENDIF.
ENDLOOP.
“ EXIT from outer loop if target is found
IF ( lv_exit = abap_true ).
EXIT.
ENDIF. ENDLOOP.
cl_demo_output=>display( lt_output ).

Sample New ABAP Syntax:

Sample Source Code:


DATA(lt_output) = VALUE tyt_input( FOR lv_input1 IN lt_input INDEX INTO lv_index1
TO lines( lt_input ) - 1
" Inner loop star5ng from next index FOR lv_input2 IN
lt_input INDEX INTO lv_index2 FROM lv_index1 + 1
( LINES OF COND #( WHEN lv_input1 + lv_input2 = lv_k
THEN VALUE #( ( lv_index1 ) ( lv_index2 ) ) ) ) ).

OpCmized Approach:
This problem can be optimized using the famous two-pointer technique.

It’s the use of two different pointers (usually to keep track of array or string
indices) to solve a problem involving said indices with the benefit of saving time
and space. However, the technique basically uses the fact that the input array is
sorted.
Applying two-pointer technique :
1. Sort the input array( if it’s not sorted already in the problem statement )
2. Initialize start pointer with the first index and end pointer with the last index.
3. Move the left pointer ‘i’ towards right when the sum of Arr[i] and Arr[j] is less
than K.
4. Move the right pointer ‘j’ towards the left when the sum of Arr[i] and Arr[j] is
greater than K.
5. Return the indices when the sum is found.
Consider an array arr[] = { 5, 1, 2, 6, 4, 3 } and value of k = 10. We need to
store the indices of the input array since It will be changed after sorting.

Sort the array:

Now, Initialize the start pointer with Arr[1] (highlighted in red) and end pointer
with Arr[6] (highlighted in green) as below:

Since the sum of these pointers are less than the target value(10), Move the left
pointer towards the right and keep the right pointer as it is.

Since the sum of these pointers are less than the target value(10), Move the left
pointer towards the right and keep the right pointer as it is.
Since the sum of these pointers are less than the target value(10), Move the left
pointer towards the right and keep the right pointer as it is.

Wonderful!! We have got the right pair whose sum equals the target.
So, we will prepare the output table and return the relevant indexes i.e. [5,4]

No. of Iterations required : N – 1

N : size of array

Time complexity for this algorithm will be linear since there is only one LOOP .
However, we have to sort the input array so time complexity will be : O(nlogn)
also known as loglinear complexity.
Note:- If the input array is sorted already in the problem statement, then time
complexity will be O(n) also known as linear time complexity. However, This
approach is still optimized as compared to brute force as we have successfully
avoided the nested loop.
Sample Source Code:
TYPES: tyt_input TYPE TABLE OF i WITH EMPTY KEY,
BEGIN OF tys_value, index TYPE i, value TYPE i, END OF
tys_value, tyt_index_input TYPE TABLE OF tys_value WITH EMPTY KEY. DATA:
lv_k TYPE i.
“ Input Array
DATA(lt_input) = VALUE tyt_input( ( 7 ) ( 10 ) ( 30 ) ( 9 ) ( 3 ) ).
"Input for target sum cl_demo_input=>add_field( CHANGING
field = lv_k ). cl_demo_input=>request( ).
“Prepare new table with indexes
DATA(lt_index) = VALUE tyt_index_input( FOR lv_value IN lt_input INDEX INTO lv_index
( index = lv_index value = lv_value ) ).
“Sort the new table with input values (Keep-in mind that indexes will also be changed “when
sorted that’s why we used another internal table to preserve the original indexes SORT lt_index BY
VALUE.
“Ini5alize start and end pointers DATA(lv_start)
= 1.
DATA(lv_end) = lines( lt_input ).
“Itera5on through the Input array LOOP AT lt_index
INTO DATA(lv_input).
“ Calculate the sum of the values at start and end pointers DATA(lv_sum) = lt_index[ lv_start ]value
+ lt_index[ lv_end ]-value.
“ When sum is more than the target value, move the end pointer to the len
IF lv_sum > lv_k. lv_end -= 1.
“ When sum is less than the target value, move the start pointer to the right
ELSEIF lv_sum < lv_k. lv_start +=
1.
ELSE.
“ Return the original indexes at start and end pointer when sum equals target
DATA(lt_output) = VALUE tyt_input( ( lt_index[ lv_start ]-index ) (
lt_index[ lv_end ]-index ) ). “ Exit from the loop EXIT.
ENDIF. ENDLOOP.
cl_demo_output=>display( lt_output ).

Alternate Approach:
There is another solution to use a temporary array(internal table) as a Hash Map
table. This
HashMap should store the complement of the previous iterated values. For every
new Iteration, we should first check if its complement already exists in the
HashMap. If found, we can collect the indices for the current iteration and get the
index of its complement from the input array to generate the final output and exit
from the loop.
Consider an array arr[] = { 5, 1, 2, 6, 4, 3 } and value of k = 10.
Complement of the array element against the target value would be : k –
arr[i] Initially, Input Array and HashMap will be shown as:

1st Iteration:
Arr[1] NOT IN HashMap[ ] so HashMap[1] = 10 – 5 = 5

2nd Iteration:
Arr[2] NOT IN HashMap[ ] so HashMap[2] = 10 – 1 = 9

3rd Iteration:
Arr[3] NOT IN HashMap[ ] so HashMap[3] = 10 – 2 = 8
4th Iteration:
Arr[4] NOT IN HashMap[ ] so HashMap[4] = 10 – 6 = 4

5th Iteration:
Arr[5] IN HashMap[ ] so we found the match.

Wonderful!! We have got the right pair whose sum equals the target. So we
will prepare the output table and return the relevant indexes. One would be
the current index and another would be the index of its
complement in the array.

No. of Iterations required : N

N : size of array
Time complexity for this algorithm will be linear since there is only one LOOP and
unlike the previous method, we don’t need to sort the input array.
Time complexity would be : O(N)
Sample ABAP code can be found as follow:
Sample Source Code:
TYPES: tyt_input TYPE TABLE OF i WITH EMPTY KEY, DATA:
lv_k TYPE i.
" Input Array
DATA(lt_input) = VALUE tyt_input( ( 7 ) ( 10 ) ( 30 ) ( 9 ) ( 3 ) ).
" Reading hash table with index DATA(lt_hash) = VALUE
tyt_input( ).
“Itera5on through the Input array LOOP AT lt_input
INTO DATA(lv_input).
DATA(lv_tabix) = sy-tabix.
“ When current element is not found in hashmap, add it’s complement into hashmap
IF ( line_index( lt_hash[ table_line = lv_input ] ) = 0 ). APPEND ( lv_k - lv_input ) TO
lt_hash.
ELSE.
“ When current element is found in hashmap, append the current index and the
index of its complement into result table DATA(lt_output) = VALUE
tyt_input( ( lv_tabix )
( line_index( lt_input[ table_line =
lv_k - lv_input ] ) ) ).
EXIT.
ENDIF.
ENDLOOP.
Eight different sort algorithms implemented in ABAP
33 15 21,420

Bucket Sort
Bubble Sort
Merge Sort
Quick Sort
Selec8on Sort
Inser8on Sort
Heap Sort
Shell Sort
A very draD performance comparison
Sleep Sort in JavaScript

Some applica8on developers think that it is enough to know SORT keyword and how to use sorted table in ABAP for their
daily work without knowing how SORT is done internally. For me I can not say this assump8on is wrong. I personal
preference is to know something more thoroughly. We have learned various sort algorithms in the university, here I just list
my implementa8on on some of them using ABAP for my personal study purpose.
For each sort algorithm I will create a sta8c public class with a sort method which accepts an internal table with unsorted
Integer and an output table which are sorted. For simplifica8on reason the element in the internal table only consists of
unsigned integers ( >= 0 )

Bucket Sort ( In China we prefer to call it Hash Sort )


Algorithm introduc8on

In ABAP the internal table is a perfect choice for bucket collec8on A small trap here is, array in most program language has
start index as 0, however in ABAP for internal table it is 1. So be careful about the possibility that 0 appears in the input
internal table.
Jerry’s ABAP implementa8on
And I also implement a version using JavaScript which can support nega8ve integer in Bucket Sort as well. See source code
here.

Bubble Sort
Algorithm introduc8on
I have implemented two variants, the only difference between them:

Variant 1 uses two nested DO LOOP, while variant 2 uses WHILE as inner LOOP.
Variant 1 uses tradi8onal keyword MODIFY itab FROM workarea INDEX index to swap the two adjacent element, while
variant 2 uses new grammar itab[ index ].

Source code for both variants.

Merge Sort
Algorithm introduc8on
Again I have implemented two variants.
Variant1 – use Recursive
Callstack could be found below:

Variant 2 – non recursive version


This variant is implemented in a non-recursive way.
Source code of both variants.

Quick Sort
Algorithm introduc8on

My implementa8on

SelecCon Sort
Algorithm introduc8on

My implementa8on

InserCon Sort
Algorithm introduc8on

My implementa8on

Heap Sort
Algorithm Introduc8on
My implementa8on

Shell Sort

Algorithm Introduc8on
My implementa8on

A very draF performance comparison


Since each sort algorithm has different 8me complexity – best case, worst case and average case according to different data
distribu8on, here below I only make a very draD comparison by genera8ng some random integers in ABAP via
cl_abap_random_int:

DATA: lv_seed TYPE i. lv_seed = sy-8mlo.


DATA(lo_ran) = cl_abap_random_int=>create( min = 1 max = 1000 seed = lv_seed ).
DO iv_num TIMES.
APPEND lo_ran->get_next( ) TO rv_table.
ENDDO.

Meanwhile I am especially curious about how ABAP keyword SORT will behave against these eight sort algorithms, so I
create another two sort approaches.

The ninth sort approach


Pregy simple, just use ABAP keyword SORT to sort the table.

method SORT. rv_table = iv_table.


SORT rv_table. endmethod.

The tenth sort approach


I just loop the original table and put each element to a sorted table with line item as INT4.

DATA: lt_sorted TYPE ZTSORTED_INT4.


LOOP AT iv_table ASSIGNING FIELD-SYMBOL(<item>).
INSERT <item> INTO table lt_sorted.
ENDLOOP.
APPEND LINES OF lt_sorted TO rv_table.

The table type ZTSORTED_INT4 has line type INT4 with sorted table type.
Test code:

DATA(lt_test_data) = zcl_sort_helper=>generate_data( 3000 ).

zcl_sort_helper=>start_measure( ).
DATA(lt_bubble) = zcl_bubblesort=>sort( lt_test_data ). WRITE: / 'Bubble Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_hashsort) = zcl_hashsort=>sort( lt_test_data ). WRITE: / 'Hash Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_heapsort) = zcl_hashsort=>sort( lt_test_data ). WRITE: / 'Heap Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_insertsort) = zcl_insertsort=>sort( lt_test_data ). WRITE: / 'Insert Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_mergesort) = zcl_mergesort=>sort( lt_test_data ). WRITE: / 'Merge Sort dura8on:'
, zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_quicksort) = zcl_quicksort=>sort( lt_test_data ). WRITE: / 'Quick Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_selectsort) = zcl_selectsort=>sort( lt_test_data ). WRITE: / 'Select Sort dura8on:' ,

zcl_sort_helper=>stop( ). zcl_sort_helper=>start_measure( ).
DATA(lt_shellsort) = zcl_shellsort=>sort( lt_test_data ). WRITE: / 'Shell Sort dura8on:' ,
zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_sort_keyword) = zcl_sort_via_keyword=>sort( lt_test_data ). WRITE: / 'ABAP Sort keyword
dura8on:' , zcl_sort_helper=>stop( ).

zcl_sort_helper=>start_measure( ).
DATA(lt_sort_table) = zcl_abap_sorgable=>sort( lt_test_data ).
WRITE: / 'ABAP Sorted table dura8on:' , zcl_sort_helper=>stop( ).
ASSERT lt_bubble = lt_hashsort.
ASSERT lt_hashsort = lt_heapsort.
ASSERT lt_heapsort = lt_insertsort.
ASSERT lt_insertsort = lt_mergesort. ASSERT lt_mergesort =
lt_quicksort.
ASSERT lt_quicksort = lt_selectsort.
ASSERT lt_shellsort = lt_selectsort.
ASSERT lt_sort_keyword = lt_shellsort.
ASSERT lt_sort_table = lt_sort_keyword.

The test result ( unit: microsecond )

The ABAP SORT keyword and SORTED TABLE did a really good job here The
complete source code for this blog could be found from my github.

Sleep Sort in JavaScript


Last but not least, the super cool “Sleep Sort” done in JavaScript, which does not need any comparison against two elements
in the array.

const num = [1,5,6,11,2,3,4,8,7,14]; num.forEach( num => {


setTimeout( () => { console.log(num)}, num); });

Test output:
WriCng algorithms in ABAP (SAP BTP ABAP Environment 2211 Release)

Kadane’s algorithm(Maximum Subarray) in SAP BTP ABAP(Steampunk), JS &


Python
Problem
Given an integer array nums, find the subarray which has the largest sum and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]

Output: 6

Explana8on: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]


Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]


Output: 23

Constraints:

5
1 <= nums.length <= 10

4 4
-10 <= nums[i] <= 10
Solu:on
Time Complexity: O(n)

Space Complexity: O(1)

ABAP

CLASS zcl_algo_kadane DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
* Mandatory declara8on
INTERFACES if_oo_adt_classrun.

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

METHODS kadaneAlgorithm
IMPORTING lt_nums TYPE STANDARD TABLE RETURNING
VALUE(rv_msf) TYPE i.

ENDCLASS.
CLASS zcl_algo_kadane IMPLEMENTATION.

METHOD if_oo_adt_classrun~main. out->write( |{ kadaneAlgorithm( VALUE ty_nums( ( 5 ) ( 4 ) ( -1 ) (


7 ) ( 8 ) ENDMETHOD.
METHOD kadaneAlgorithm.

local variables
DATA : lv_meh TYPE i VALUE 0, "max ending here
lv_temp TYPE i VALUE 0, "sum lv_start TYPE i

VALUE 0, "start lv_end TYPE i VALUE 0. "end rv_msf

= VALUE #( lt_nums[ 1 ] OPTIONAL ). LOOP AT lt_nums

ASSIGNING FIELD-SYMBOL(<lf_wa>). lv_meh += <lf_wa>.

IF ( rv_msf < lv_meh ).


rv_msf = lv_meh.
lv_start = lv_temp.
lv_end = ( sy-tabix - 1 ). ENDIF.

IF ( lv_meh < 0 ).
lv_meh = 0. lv_temp
= sy-tabix.
ENDIF. ENDLOOP.
UNASSIGN <lf_wa>.
FREE : lv_meh, lv_temp, lv_start, lv_end.

ENDMETHOD.
ENDCLASS.

JavaScript

Python
class Solu8on: def maxSubArray(self, nums: List[int]) -> int:

# local variables lv_msf =


nums[0] lv_meh = 0 lv_start
=0 lv_end = 0 lv_temp = 0
# running the loop for i in range(0,
len(nums)): lv_meh += nums[i]

if (lv_msf < lv_meh): lv_msf = lv_meh


lv_start = lv_temp lv_end = i

if(lv_meh < 0): lv_meh = 0


lv_temp = i + 1 return lv_msf

Perform Stock Buy And Sell algorithm in SAP BTP


ABAP(Steampunk), JS & Python
Problem
ith
You are given an array prices where prices[i] is the price of a given stock on the day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in
the future to sell that stock.

Return the maximum profit you can achieve from this transac8on. If you cannot achieve any profit, return
0.

Example 1:

Input: prices = [7,1,5,3,6,4]


Output: 5
Explana8on: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 Note that buying on day 2 and selling on
day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]


Output: 0 Explana8on: In this case, no transac8ons are done and the max profit = 0.

Constraints:

5
1 <= prices.length <= 10
4
0 <= prices[i] <= 10
Solu:on
Time Complexity: O(n)

Space Complexity: O(1)

ABAP
CLASS zcl_stock DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.

* Mandatory declara8on
INTERFACES if_oo_adt_classrun.

PROTECTED SECTION. PRIVATE


SECTION.

TYPES ty_prices TYPE STANDARD TABLE OF i WITH DEFAULT KEY.

METHODS StockBuySell
IMPORTING lt_prices TYPE ty_prices RETURNING VALUE(rv_max_profit)
TYPE i.

ENDCLASS.

CLASS zcl_stock IMPLEMENTATION.

METHOD if_oo_adt_classrun~main. out->write( |{ StockBuySell( VALUE #( ( 7 ) ( 1 ) ( 5 ) ( 3 ) ( 6 ) ( 4 ) ) )


ENDMETHOD.

METHOD StockBuySell.

DATA(lv_min_price) = lt_prices[ 1 ].

LOOP AT lt_prices ASSIGNING FIELD-SYMBOL(<lfs_wa>). lv_min_price = nmin( val1 =


lv_min_price val2 = <lfs_wa> ). rv_max_profit = nmax( val1 = rv_max_profit
val2 = ( <lfs_wa> - lv_min_price ) ). ENDLOOP.

UNASSIGN <lfs_wa>. FREE


lv_min_price.

ENDMETHOD.
ENDCLASS.
JavaScript

var maxProfit = func8on(prices) {

var lv_min_price = prices[0], lv_max_profit = 0;

for(let i = 0; i < prices.length; i++){


lv_min_price = Math.min(lv_min_price, prices[i]);
lv_max_profit = Math.max(lv_max_profit, (prices[i] - lv_min_price));
}

return lv_max_profit;

};

Python

class Solu8on: def maxProfit(self, prices: List[int]) -> int:

lv_min_price = prices[0] lv_max_profit = 0

for i in range(0, len(prices)): lv_min_price = min(lv_min_price, prices[i]) lv_max_profit =


max(lv_max_profit, (prices[i] - lv_min_price)) return lv_max_profit

Perform sorCng of 0s, 1s and 2s | Dutch NaConal Flag


problem in SAP BTP ABAP(Steampunk), JS & Python
Problem
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the
same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respec8vely.

You must solve this problem without using the library’s sort func8on.

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

Example 2:

Input: nums = [2,0,1]


Output: [0,1,2]

Constraints:

n ==
nums.len
gth 1 <=
n <= 300
nums[i] is either 0, 1, or 2.

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

Solu:on
Time Complexity: O(n)

Space Complexity: O(1)

ABAP
CLASS zcl_algo_dnf DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
* Mandatory declara8on
INTERFACES if_oo_adt_classrun.

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

METHODS sortNumbers
CHANGING lt_nums TYPE STANDARD TABLE.

METHODS swapNumbers
CHANGING lt_nums TYPE STANDARD TABLE lv_i
TYPE i lv_j TYPE i.

ENDCLASS.

CLASS zcl_algo_dnf IMPLEMENTATION.

METHOD if_oo_adt_classrun~main.

DATA(lo_obj) = NEW zcl_algo_dnf( ).

* data lt_nums = VALUE ty_nums( ( 2 ) ( 0 ) ( 2 ) ( 1 ) ( 1 ) ( 0 ) ).


* calling the method lo_obj->sortNumbers( CHANGING lt_nums = lt_nums ).

out->write( |aDer sor8ng:------->| ). out->write( lt_nums ).

FREE lt_nums.

ENDMETHOD.

METHOD sortNumbers.

* indexes
DATA(lv_low) = 1.
DATA(lv_mid) = 1.
DATA(lv_high) = lines( lt_nums ).
WHILE lv_mid <= lv_high.
CASE lt_nums[ lv_mid ].
WHEN 0. swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_low lv_j = lv_mid ). lv_low += 1. lv_mid
+= 1. WHEN 1.
lv_mid += 1.
WHEN 2. swapNumbers( CHANGING lt_nums = lt_nums
lv_i = lv_mid lv_j = lv_high ). lv_high -= 1.
ENDCASE. ENDWHILE.

FREE: lv_low, lv_mid, lv_high.

ENDMETHOD.

METHOD swapNumbers.

* local variable 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.

JavaScript

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = func8on (nums) {
var lv_low = 0, lv_mid = 0,
lv_high = nums.length - 1;
while (lv_mid <= lv_high) { switch (nums[lv_mid]) { case 0:
swapNumbers(nums, lv_low, lv_mid);
lv_low += 1; lv_mid += 1; break; case 1:
lv_mid += 1; break; case 2: swapNumbers(nums,
lv_mid, lv_high);
lv_high -= 1; break;
}
} };

func8on swapNumbers(nums, i, j) { var temp =


0; temp = nums[i]; nums[i] = nums[j];
nums[j] = temp;
}

Python

class Solu8on: def sortColors(self, nums: List[int]) -> None:


"""
Do not return anything, modify nums in -place instead.
"""
lv_low = lv_mid = 0 lv_high = len(nums) -
1

while lv_mid <= lv_high: if nums[lv_mid] == 0:


swapNumbers(nums, lv_low, lv_mid)
lv_low += 1 lv_mid += 1 elif nums[lv_mid] == 1:
lv_mid += 1 else: swapNumbers(nums, lv_mid, lv_high)
lv_high -= 1 def swapNumbers(nums, i, j) -> None:
lv_temp = 0
lv_temp =
nums[i]
nums[i] =
nums[j]
nums[j] =
lv_temp

Perform Next PermutaCon in SAP BTP ABAP(Steampunk), JS


& Python
Problem
A permuta8on 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 permuta8ons of arr: [1,2,3], [1,3,2], [2, 1, 3], [2,
3, 1], [3,1,2], [3,2,1].
The next permuta8on of an array of integers is the next lexicographically greater permuta8on of its
integer. More formally, if all the permuta8ons of the array are sorted in one container according to their
lexicographical order, then the next permuta8on of that array is the permuta8on 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 permuta8on of arr = [1,2,3] is [1,3,2].


Similarly, the next permuta8on of arr = [2,3,1] is [3,1,2].
While the next permuta8on 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 nums, find the next permuta8on 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

Solu:on
Time Complexity: O(n)

Space Complexity: O(1)


ABAP

CLASS zcl_algo_nxtpermut DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.
* Mandatory declara8on
INTERFACES if_oo_adt_classrun.

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

METHODS getNextPermuta8on
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.
* step: 4: manipula8ons 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 Solu8on: def nextPermuta8on(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 condi8on 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]
# incremen8ng i += 1
j -= 1
JavaScript

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/ var nextPermuta8on = func8on (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);

};

func8on swap_numbers(array, i, j) { var temp =


array[i]; array[i] = array[j]; array[j] = temp;
}

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

You might also like