Sunday, 24 December 2017

Question :

An Super array is defined to be an array in which each element is greater than sum of all elements before that. See examples below:

{2, 3, 6, 13} is a Super array. Note that 2 < 3, 2+3 < 6, 2 + 3 + 6 < 13.

{2, 3, 5, 11} is a NOT a Super array. Note that 2 + 3 not less than 5.


Write a function named isSuper  that returns 1 if its array argument is a isSuper array, otherwise it returns 0.

If you are programming in Java or C#, the function signature is:
   int isSuper (int [ ] a)

If you are programming in C or C++, the function signature is:

   int isSuper (int a[ ], int len) where len is the number of elements in the array.

Solution :
public static int isSuper(int[] a)
        {
            int sum = 0, rtnVal = 1;
            for (int i = 0; i < a.Length - 1; i++)
            {
                sum += a[i];
                if (sum >= a[i + 1])
                {
                    rtnVal = 0;
                    break;
                }  
            }
            return rtnVal;
        }

0 comments:

Post a Comment

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page