' Suffix Sum — computes running sum from end of array
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL arr(1000) AS LONG
    LOCAL suffix(1000) AS LONG
    LOCAL n AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL total AS LONG
    LOCAL lineStr AS STRING
    LOCAL elemStr AS STRING
    n = 40
    PRINT "============================================"
    PRINT "          SUFFIX SUM (Reverse Cumulative)"
    PRINT "============================================"
    PRINT ""
    PRINT "Original array:"
    PRINT ""
    FOR i = 0 TO n - 1
        arr(i) = (i * 11 + 5) MOD 25
    NEXT i
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(arr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    total = 0
    FOR i = n - 1 TO 0 STEP -1
        total = total + arr(i)
        suffix(i) = total
    NEXT i
    PRINT "Suffix sum (from end):"
    PRINT ""
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(suffix(i))
        DO WHILE LEN(elemStr) < 4
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "Verification: suffix(0) should equal total sum"
    IF suffix(0) = total THEN
        PRINT "PASS: suffix(0) = " + STR$(suffix(0)) + " = total = " + STR$(total)
    ELSE
        PRINT "FAIL: suffix(0) = " + STR$(suffix(0)) + " != total = " + STR$(total)
    END IF
    PRINT ""
    PRINT "Suffix range sums:"
    PRINT ""
    FOR i = 0 TO 9
        j = i * 4
        IF j < n THEN
            PRINT "suffix(" + STR$(j) + " to end) = " + STR$(suffix(j))
        END IF
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     PREFIX vs SUFFIX SUM COMPARISON"
    PRINT "============================================"
    PRINT ""
    LOCAL prefix(1000) AS LONG
    total = 0
    FOR i = 0 TO n - 1
        total = total + arr(i)
        prefix(i) = total
    NEXT i
    PRINT "Comparing prefix and suffix arrays:"
    PRINT ""
    PRINT "Index  Prefix  Suffix  Prefix+Suffix"
    FOR i = 0 TO n - 1
        IF i < 10 OR i > n - 5 THEN
            elemStr = STR$(i)
            DO WHILE LEN(elemStr) < 5
                elemStr = " " + elemStr
            LOOP
            PRINT elemStr + " " + STR$(prefix(i)) + " " + STR$(suffix(i))
        END IF
    NEXT i
    PRINT ""
    PRINT "Property: prefix(i) + suffix(i+1) = total"
    PRINT ""
    FOR i = 0 TO 4
        j = i * 8
        IF j < n - 1 THEN
            total = prefix(j) + suffix(j + 1)
            PRINT "prefix(" + STR$(j) + ") + suffix(" + STR$(j + 1) + ") = " + STR$(total)
        END IF
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     REVERSE SUFFIX SUM (building from end)"
    PRINT "============================================"
    PRINT ""
    LOCAL revArr(1000) AS LONG
    LOCAL revSuffix(1000) AS LONG
    FOR i = 0 TO n - 1
        revArr(i) = arr(n - 1 - i)
    NEXT i
    total = 0
    FOR i = 0 TO n - 1
        total = total + revArr(i)
        revSuffix(i) = total
    NEXT i
    PRINT "Reversed array suffix sum (same as forward prefix):"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(revSuffix(i))
        DO WHILE LEN(elemStr) < 4
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "    SUFFIX SUM FOR QUERYING TAIL SUMS"
    PRINT "============================================"
    PRINT ""
    LOCAL midSum AS LONG
    LOCAL tailSum AS LONG
    PRINT "Quick suffix queries:"
    PRINT ""
    FOR i = 0 TO 7
        j = n - 1 - i * 5
        IF j >= 0 THEN
            tailSum = suffix(j)
            PRINT "Last " + STR$(n - j) + " elements (from index " + STR$(j) + "): " + STR$(tailSum)
        END IF
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      MIN/MAX SUFFIX (running extrema)"
    PRINT "============================================"
    PRINT ""
    LOCAL minSuffix(1000) AS LONG
    LOCAL maxSuffix(1000) AS LONG
    LOCAL minVal AS LONG
    LOCAL maxVal AS LONG
    minVal = arr(n - 1)
    maxVal = arr(n - 1)
    FOR i = n - 1 TO 0 STEP -1
        IF arr(i) < minVal THEN
            minVal = arr(i)
        END IF
        IF arr(i) > maxVal THEN
            maxVal = arr(i)
        END IF
        minSuffix(i) = minVal
        maxSuffix(i) = maxVal
    NEXT i
    PRINT "Suffix minimum:"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(minSuffix(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "Suffix maximum:"
    lineStr = ""
    FOR i = 0 TO n - 1
        elemStr = STR$(maxSuffix(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
        IF (i + 1) MOD 10 = 0 THEN
            PRINT lineStr
            lineStr = ""
        END IF
    NEXT i
    IF lineStr <> "" THEN
        PRINT lineStr
    END IF
    PRINT ""
    PRINT "Suffix sum operations complete"
    FUNCTION = 0
END FUNCTION
