You are not logged in. Please login at www.codechef.com to post your questions!

×

Finding sum of array without going through it

In SnackDown 2018, there is a question Spread the Word(Code SPREAD2). There you have to loop through an array of people(I cannot think of anything better) and sum the number of people they can tell about SnackDown iteratively. Here is my code which is getting a TLE. Please help.

#include <iostream> int main() { int t,n,know,to_know,days,sum,i; scanf("%d",&t); while(t--) { //n; scanf("%d",&n); int people[n]; for(int i = 0; i < n; ++i) { scanf("%d",&people[i]); } know = 1,to_know = people[0],days = 0; while(n-know) { sum = 0,i = know; for(; i < know+to_know&&i < n; ++i) { sum += people[i]; } know = i; to_know += sum; days++; } printf("%d\n",days); } return 0; }

asked 15 Oct '18, 19:33

jai_super123's gravatar image

2★jai_super123
11
accept rate: 0%

Well, I did this, but now SIGSEGV??? ` ... int t,n,know,to_know,days,sum; ... while(n-know) { sum = std::accumulate(people + know,people + (know+to_know < n? know+to_know : n) , to_know); know = (know + to_know < n ? know + to_know : n); to_know = sum; ++days; } ...

` PS Waiting for answer after 7:30 pm tomorrow. ;)

(15 Oct '18, 20:26) jai_super1232★

You'll get to know the correct approach after 7:30 pm tomorrow.

Till then, try hard. And avoid asking for hints on forums.You might get banned for this.

link

answered 15 Oct '18, 19:57

nileshjha19's gravatar image

2★nileshjha19
1365
accept rate: 0%

OK, thanks for the warning! I just saw std::accumulate(). Let's try that! ;) PS BTW, I can ask for answer/approach/hints if it is not part of a competition or competition is over, right?

(15 Oct '18, 20:13) jai_super1232★

@jal_super123 Yes. You can ask any query after contest is over.

(16 Oct '18, 13:46) babangain3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×37
×14
×9
×2
×2

question asked: 15 Oct '18, 19:33

question was seen: 295 times

last updated: 16 Oct '18, 13:46