Question
. Define an array to be cumulative if the nth (n > 0) element of the array is the sum of the first n elements
. Define an array to be cumulative if the nth (n > 0) element of the array is the sum of the first n elements of the array. So {1, 1, 2, 4, 8} is cumulative because
- a[1] == 1 == a[0]
- a[2] == 2 == a[0] + a[1]
- a[3] == 4 == a[0] + a[1] + a[2]
- a[4] == 8 == a[0] + a[1] + a[2] + a[4]
And {1, 1, 2, 5, 9} is not cumulative because a[3] == 5 != a[0] + a[1] + a[2]
Write a function named isCumulative that accepts an array of integers and returns 1 if the array is cumulative and 0 otherwise.
If you are programming in Java or C#, the function signature is int isCumulative(int[ ] a)
If you are programming in C or C++, the function signature is int isCumulative(int a[ ], int len) where len is the number of elements in the array
Some other examples:
if the input array is | isCumulative should return |
{1} | 0 (array must contain at least 2 elements) |
{0,0,0,0,0,0} | 1 |
{1, 1, 1, 1, 1, 1} | 0 |
{3, 3, 6, 12, 24} | 1 |
{-3, -3, -6, -12, -24} | 1 |
{-3, -3, 6, 12, 24} | 0 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started