• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

Test for super-increasing sequence

A super-increasing sequence of numbers is a sequence in which each number is larger or equal to the sum of all previous numbers in the sequence. Write an algorithm in pseudocode to test if a given sequence of numbers is super-increasing ssequence.

1  2  4  8  16  22  40 is not an super-increasing sequence
1  2  4  8  16  31  65 is super-inceasing sequence.

This is what i have done so far:

BEGIN testSuperIncreasing
    SET maxNum=20
    FOR i=0 to maxNum-1
        GET num
        SET arr[i]=num
    ENDFOR
    SET i=0
    SET sum=0
    IF arr[i+1] < arr[i]
        PRINT "Not super-increasing sequence"
    WHILE (i < maxNum)
        sum= arr[i] + arr[i+1]
        IF arr[i+2] >= sum
            sum = sum + arr[i+2]
        ELSEIF (i = maxNum-1)
           PRINT "This is super-increasing sequence"
        ELSE
            PRINT "Not super-increasing sequence"
        ENDIF
        INCREMENT i
    ENDWHILE
END  testSuperIncreasing

I know there is something wrong with my pseudocode. Please help.
0
dandeliondream
Asked:
dandeliondream
4 Solutions
 
UrosVidojevicCommented:
For example,

when i = maxNum-1 you will get some "ArrayIndexOutOfBounds" errors in these lines.

        sum= arr[i] + arr[i+1]
        IF arr[i+2] >= sum
            sum = sum + arr[i+2]
0
 
ozoCommented:
       sum= arr[i] + arr[i+1]
        IF arr[i+2] >= sum
            sum = sum + arr[i+2]
sum is the sum of the last two or three numbers
you want it to be the sum of all previous numbers
0
 
MikkkCommented:
I think it has to be simplest:

BEGIN testSuperIncreasing
SET arr=[1  2  4  8  16  31  65]
SET i=0
SET sum=0
WHILE (i < len(arr) AND arr[i]>=sum )
 sum=sum + arr[i]
 INCREMENT i
ENDWHILE
IF i=len(arr)
     PRINT "This is super-increasing sequence"
ELSE
     PRINT "Not super-increasing sequence"
END  testSuperIncreasing
0
 
nthnlCommented:
A bit easier to understand:

BEGIN testSuperIncreasing
SET arr=[1  2  4  8  16  31  65]
SET i=0
SET sum=0
SET superIncr = TRUE
WHILE (i < len(arr) AND superIncr)
 IF arr[i] < sum
     superIncr = FALSE
 ELSE
     sum=sum + arr[i]
  ENDIF
 INCREMENT i
ENDWHILE

IF superIncr
         PRINT "Super-increasing"
ELSE
         PRINT "Not super-increasing"
ENDIF

END testSuperIncreasing

END  testSuperIncreasing
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now