7 views

Uploaded by x404

save

- Decidability_Complexity.theory_Syntactic.sugars .pdf
- 1615f4e4b2bae3548400845819a85b52 02 Greedy Algorithms Problems
- Important question of DS
- 2012-01-25--hash_tables.pdf
- Informix 4GL info.docx
- PCMSolver- An Open-Source Library for Solvation Modeling
- Lecture 2 - LP Duality, Simplex Method Complexity, Ellipsoid Method
- A Quick Introduction to Arrays and Array Formulas
- C___ an Introductory Guide for - Francis John Thottungal
- ITC-2017
- Binary
- Gossip-based Distribution Estimation in Peer-to-Peer Networks
- Khandake-IJERTV2IS3431_1
- Tut1
- Data Structures Succinctly Part 1
- Analysis
- robotics
- Examples of Stack
- JSON Data Set Sample
- Readme
- Event Module Discription
- Excel
- Analysis
- CLxPerf
- Sample Exam for CLAD
- TOQCv1_2
- Array List
- perl
- 10 Data Storage in Data Blocks
- Data Structures
- Yes Please
- The Unwinding: An Inner History of the New America
- Sapiens: A Brief History of Humankind
- The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- Dispatches from Pluto: Lost and Found in the Mississippi Delta
- Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
- John Adams
- The Prize: The Epic Quest for Oil, Money & Power
- The Emperor of All Maladies: A Biography of Cancer
- Grand Pursuit: The Story of Economic Genius
- This Changes Everything: Capitalism vs. The Climate
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
- The New Confessions of an Economic Hit Man
- Team of Rivals: The Political Genius of Abraham Lincoln
- The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
- Smart People Should Build Things: How to Restore Our Culture of Achievement, Build a Path for Entrepreneurs, and Create New Jobs in America
- Rise of ISIS: A Threat We Can't Ignore
- The World Is Flat 3.0: A Brief History of the Twenty-first Century
- Bad Feminist: Essays
- Angela's Ashes: A Memoir
- Steve Jobs
- How To Win Friends and Influence People
- Extremely Loud and Incredibly Close: A Novel
- The Sympathizer: A Novel (Pulitzer Prize for Fiction)
- The Silver Linings Playbook: A Novel
- Leaving Berlin: A Novel
- The Light Between Oceans: A Novel
- The Incarnations: A Novel
- You Too Can Have a Body Like Mine: A Novel
- The Love Affairs of Nathaniel P.: A Novel
- Life of Pi
- Brooklyn: A Novel
- The Flamethrowers: A Novel
- The Blazing World: A Novel
- The Rosie Project: A Novel
- The First Bad Man: A Novel
- We Are Not Ourselves: A Novel
- The Master
- Bel Canto
- A Man Called Ove: A Novel
- The Kitchen House: A Novel
- Beautiful Ruins: A Novel
- Interpreter of Maladies
- The Wallcreeper
- Wolf Hall: A Novel
- The Art of Racing in the Rain: A Novel
- The Cider House Rules
- A Prayer for Owen Meany: A Novel
- Lovers at the Chameleon Club, Paris 1932: A Novel
- The Bonfire of the Vanities: A Novel
- The Perks of Being a Wallflower
- The Constant Gardener: A Novel

You are on page 1of 2

Preﬁx sums

Preﬁx sums provide a simple yet powerful technique that allows for the fast calculation of

sums of elements in given contiguous segments of arrays. Preﬁx sums are deﬁned as the

consecutive totals of the ﬁrst 0, 1, 2, . . . , n elements of an array.

a

0

a

1

a

2

. . . a

n−1

p

0

= 0 p

1

= a

0

p

2

= a

0

+ a

1

p

3

= a

0

+ a

1

+ a

2

. . . p

n

= a

0

+ a

1

+ . . . + a

n−1

We can easily calculate the preﬁx sums in O(n) time complexity. Notice that the total p

k

equals p

k−1

+ a

k−1

, so each consecutive value can be calculated in a constant time.

3.1: Counting preﬁx sums.

1 def prefix_sums(A):

2 n = len(A)

3 p = [0]

*

(n + 1)

4 for k in xrange(1, n + 1):

5 p[k] = p[k - 1] + A[k - 1]

6 return p

Similarly, we can calculate suﬃx sums, which are the totals of the k last values. Using preﬁx

(or suﬃx) sums allows us to calculate the total of any slice (contiguous segments) of the

array very quickly. For example, you are asked about the totals of m slices (x, y) such that

0 x y < n, where the total is the sum a

x

+ a

x+1

+ . . . + a

y−1

+ a

y

.

The simplest approach is to iterate through the whole array for each result separately;

however, that requires of O(n · m) time. The better approach is to use preﬁx sums. If we

calculate the preﬁx sums then we can answer each question directly in constant time. Let’s

subtract p

x

from the value p

y+1

.

p

y+1

a

0

a

1

. . . a

x−1

a

x

a

x+1

. . . a

y−1

a

y

p

x

a

0

a

1

. . . a

x−1

p

y+1

−p

x

a

x

a

x+1

. . . a

y−1

a

y

We have calculated the total of a

x

+a

x+1

+. . . +a

y−1

+a

y

in O(1) time. Using this approach,

the total time complexity is O(n + m).

Copyright 2014 by Codility Limited. All Rights Reserved. Unauthorized copying or publication prohibited.

1

3.1. Exercise

Problem: You are given a non-empty, zero-indexed array A of n integers: a

0

, a

1

, . . . , a

n−1

such that (0 a

i

1 000), and integers k and m such that 0 k, m < n 100 000.

A robot is at position k in array A and should perform m moves. One move moves

a robot to an adjacent cell of the array. In every cell, the robot collects the value in the cell

and replaces it with 0. The sum is the total of all collected values, and the goal is to calculate

the maximum sum that the robot can collect in m moves.

For example, consider array A such that:

2 3 1 5 1 3 9

0 1 2 3 4 5 6

The robot starts at position k = 4 and should perform m = 4 moves. The robot might

move to cells (3, 4, 5, 6) and thereby collect the values 1 + 5 + 0 + 3 + 9 = 18. This is the

maximal sum that the robot can collect.

Solution: Note that the best option is to move in one direction optionally followed by some

moves in the opposite direction. The robot should not change direction more than once. With

this observation you can ﬁnd the simplest solution. Make the ﬁrst p = 0, 1, 2, . . . , m moves

in one direction, then the next m− p moves in the opposite direction. This is just a simple

simulation of the moves of the robot, and requires O(m

2

) time.

A better approach is to use preﬁx sums. If we make p moves in one direction, we can

calculate the maximal opposite location of the robot. The robot collects all values between

these extremes. We can calculate the total collected values in constant time by using preﬁx

sums, and the total time complexity, at such a solution is O(n + m).

Every lesson will provide you with programming tasks at http://codility.com/train.

2

- Decidability_Complexity.theory_Syntactic.sugars .pdfUploaded byVlad-Andrei Ursu
- 1615f4e4b2bae3548400845819a85b52 02 Greedy Algorithms ProblemsUploaded byamarjeet
- Important question of DSUploaded byPritam Saha
- 2012-01-25--hash_tables.pdfUploaded byfakhrirf
- Informix 4GL info.docxUploaded byanon_243945930
- PCMSolver- An Open-Source Library for Solvation ModelingUploaded byJexia
- Lecture 2 - LP Duality, Simplex Method Complexity, Ellipsoid MethodUploaded bygdenunzio
- A Quick Introduction to Arrays and Array FormulasUploaded bysi.paok
- C___ an Introductory Guide for - Francis John ThottungalUploaded byMohamed Taha
- ITC-2017Uploaded byVineetPandey
- BinaryUploaded byAfeefa
- Gossip-based Distribution Estimation in Peer-to-Peer NetworksUploaded byNghĩa Zer
- Khandake-IJERTV2IS3431_1Uploaded bybhaveshlib
- Tut1Uploaded bydreamylove_tn
- Data Structures Succinctly Part 1Uploaded byihackn3wton
- AnalysisUploaded bysfernando
- roboticsUploaded byఅయ్యలసోమయాజుల సాయి శివ
- Examples of StackUploaded byIBRAHIM ALNEAMI
- JSON Data Set SampleUploaded byghar_dash
- ReadmeUploaded byLitheron
- Event Module DiscriptionUploaded byNauman Khan
- ExcelUploaded byMamun Sirajul Majid
- AnalysisUploaded byKiran Kumar Valaboju
- CLxPerfUploaded byMarcos Souza
- Sample Exam for CLADUploaded bymaxout9
- TOQCv1_2Uploaded byHexa Perry
- Array ListUploaded bySean CarMa
- perlUploaded byPritam Modak
- 10 Data Storage in Data BlocksUploaded byefasaravanan
- Data StructuresUploaded byMaharshi Shukla