carlie kroz
carlie kroz

Reputation: 9

The sum of middle elements of an array using a C program

How do I find the sum of middle elements of an array or check conditions if its sum is 15?

For example an array {3, 2, 10, 4, 1, 6, 9} is centered-15 because the sequence 10, 4, 1 sums to 15 and the sequence is preceded by two elements (3, 2) and followed by two elements(6, 9).

So return 1 because the sequence 10, 4, 1 sums to 15 and is preceded by two elements and followed by two elements. Note that there is another sequence that sums to 15 (6, 9). It is okay for the array to have more than one sequence that sums to 15 as long as at least one of them is centered.

Similarly, for array {2, 10, 4, 1, 6, 9} return 0 because (10, 4, 1) is preceded by one element but followed by two. (9, 6) is preceded by five elements but followed by none. Hence neither qualify as centered.

int isCentered5(int a[ ], int len) {
    int sum=0;

    for(int i=0;i<len;i++){
        sum+=a[i];
    }

    //check if sum less than 15
    if(sum<15){
        printf("Sum of all elements are less than 15");
    }

    //check if even terms- we need odd terms to find middle sum
    elseif(len%2==0){
        printf("There should be odd lenght of array to find middle sum");
    }
    else{
        //check if middle sum of array is 15?
        //can you help me here,how can i check is middle term sum is 15 or not
        for(int i=0;i<len;i++){

        }
    }
}

int isCentered5(int a[ ], int len);//function declaration

int main() {
}

Upvotes: 1

Views: 2425

Answers (2)

fjardon
fjardon

Reputation: 7996

We should clarify the terms we use:

A sequence is centered if there are as many elements on its right and on its left

The length of the input array A is n

If we use these definitions, we should only consider the sequences defined by:

  1. max_p = (n-1)/2
  2. p in [0..max_p]
  3. sequence is elements from input array whose index belongs to [p, (n-1)-p]

It should be straightforward to write a program that loops on the possible values for p and then sum the array elements from index p to (n-1)-p.

This would lead to at least O(n²) operations.

You can also remark that the sum of elements from index i to j, we call it S(i,j) is the sum of elements from i+1 to j-1 plus A[i]+A[j].

S(i,j) = S(i+1,j-1) + A[i] + A[j]

This observation should lead you to an algorithm using O(n) operations and a single loop.

int is_centered_15(int A[], int n) {
    int max_p = (n-1)/2;
    int sum = 0;
    int p = max_p;   // left index
    int q = (n-1)-p; // right index

    // if initially p==q we will sum twice the same element
    // we need to correct the sum by canceling the extra
    // addition
    if(p==q)
        sum -= A[p];

    for(; p>=0; --p, ++q) {
        sum += A[p]+A[q];
        if(15 == sum) {
            // do what we should do if we find a sequence
            ...
            return 1;
        }
    }
    return 0;
}

Please note that this algorithm can handle positive and negative values in the input array.

Upvotes: 3

user5940189
user5940189

Reputation:

Assuming we're talking about middle three, From a design perspective: check for odd number, if not return 0. Then subtract 3 from the whole array, and divide the remainder by two. you then start from this number of elements in the array in, adding for three times, before doing your check.

Code example of what I've just said:

{
    if(len%2==0)
    {
        return 0;
    }
    int a,b=0,sum=0;
    a=len-3;
    a/=2;
    for (;b<3;b++)
    {
        next=array[a+b];
        sum+=next;
    }
    if (sum==15)
    {
        return 1;
    }
    return 0;
}

Upvotes: 0

Related Questions