Solved

# Test for super-increasing sequence

Posted on 2007-10-18
520 Views
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

0
Question by:dandeliondream

LVL 7

Assisted Solution

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

LVL 84

Assisted Solution

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

LVL 8

Accepted Solution

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

Assisted Solution

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

### Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.