How to find three consecutive numbers with the largest sum in an array of integers in C

2 Answers

0 votes
#include <stdio.h>
#include <stddef.h>

/*
 * Finds the three consecutive integers with the largest sum.
 *
 * Parameters:
 *   array      - Input array
 *   length     - Number of elements in the array
 *   max_index  - Output: index of the first element
 *                in the best 3-element sequence
 *
 * Returns:
 *   Largest sum of any 3 consecutive elements.
 */
int find_max_sum_of_three(
    const int array[],
    size_t length,
    size_t *max_index)
{
    if (length < 3) {
        *max_index = 0;
        return 0;
    }

    /* Sum of the first window */
    int current_sum = array[0] + array[1] + array[2];
    int max_sum = current_sum;

    *max_index = 0;

    /* Slide the window across the array */
    for (size_t i = 3; i < length; i++) {
        current_sum += array[i];       /* Add new element */
        current_sum -= array[i - 3];   /* Remove old element */

        if (current_sum > max_sum) {
            max_sum = current_sum;
            *max_index = i - 2;
        }
    }

    return max_sum;
}

int main(void)
{
    int numbers[] = {4, 2, 8, 7, 5, 1, 3, 9, 6};

    size_t length = sizeof(numbers) / sizeof(numbers[0]);
    size_t index;

    int max_sum =
        find_max_sum_of_three(numbers, length, &index);

    printf("Largest sum: %d\n", max_sum);

    printf("Numbers: %d %d %d\n",
           numbers[index],
           numbers[index + 1],
           numbers[index + 2]);

    return 0;
}
 
 
/* 
run:
 
Largest sum: 20
Numbers: 8 7 5

*/

 



answered Oct 29 by avibootz
0 votes
#include <stdio.h>
#include <stddef.h>

/*
 * Finds the three consecutive integers with the largest sum.
 *
 * Parameters:
 *   array      - Input array
 *   length     - Number of elements in the array
 *   max_index  - Output: index of the first element
 *                in the best 3-element sequence
 *
 * Returns:
 *   Largest sum of any 3 consecutive elements.
 */
int find_max_sum_of_three(
    const int array[],
    size_t length,
    size_t *max_index,
    int total)
{
    if (length < total) {
        *max_index = 0;
        return 0;
    }

    /* Sum of the first window */
    int current_sum = array[0] + array[1] + array[2];
    int max_sum = current_sum;

    *max_index = 0;

    /* Slide the window across the array */
    for (size_t i = total; i < length; i++) {
        current_sum += array[i];       /* Add new element */
        current_sum -= array[i - total];   /* Remove old element */

        if (current_sum > max_sum) {
            max_sum = current_sum;
            *max_index = i - 2;
        }
    }

    return max_sum;
}

int main(void)
{
    int numbers[] = {4, 2, 8, 7, 5, 1, 3, 9, 6};

    size_t length = sizeof(numbers) / sizeof(numbers[0]);
    size_t index;

    int max_sum =
        find_max_sum_of_three(numbers, length, &index, 3);

    printf("Largest sum: %d\n", max_sum);

    printf("Numbers: %d %d %d\n",
           numbers[index],
           numbers[index + 1],
           numbers[index + 2]);

    return 0;
}
 
 
/* 
run:
 
Largest sum: 20
Numbers: 8 7 5

*/

 



answered Oct 29 by avibootz

Related questions

...