Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:22:57+00:00 2026-06-13T12:22:57+00:00

Suppose that I have an array consisting of n elements. 1 2 3 4

  • 0

Suppose that I have an array consisting of n elements.

1 2 3 4 5 6 ... n

I need to find a way to extract the sums of consecutive elements in this array using C++.
Like this:

1, 2, 3,...n, 1+2, 2+3, 3+4,...(n-1)+n, 1+2+3, 2+3+4,...(n-2)+(n-1)+n,...1+2+3...n

So far I figured out I need to iterate through this array by summing certain number of elements on each run. I am not sure if it is possible to implement the algorithm I explained above. There might be a better solution but this is the best I could come up with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:22:58+00:00Added an answer on June 13, 2026 at 12:22 pm

    Let’s inspect case with 4 elements:

    {1,3,4,5, // from original array
     4,7,9, // sum of 2 consecutive elements
     8,12, // sum of 3
     13} // sum of 4
    

    As you can see every part for N sum array is of size lower from original array by (N-1). So you need target array of size: N + (N-1) + (N-2) + … 1 – which is N*(1+N)/2

    int* createSumArray(int* arr, int size)
    {
       int ti = 0; // target index
       int* ta = new int[size*(size+1)/2];
       for (int s = 1; s <= size; ++s) // how many elements to sum
       {
          for (int si = 0; si < size + 1 - s; ++si)
          {
              ta[ti] = 0;
              for (int i = si; i < si + s; ++i)
                ta[ti] += arr[i];
              ++ti;
          } 
       }
       return ta;
    }
    

    See test on ideone

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
The question is simple, let's suppose that I have an array like: $array =
I have an array that looks like this: Array ( [0] => Array (
This is a general question. Using an example below, suppose that I have a
My question is very simple, suppose that I have an array like array =
I have a question about Quantifiers. Suppose that I have an array and I
Suppose I have an array that mimics a database table. Each array element represents
suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
I know that this question might have been asked like 100 times, but, believe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.