Cumulative Differences
EasyPractice library · +10 XPprefix sumsdifference arrays
Given an array of N integers, compute a new array where each element is the difference between the current element and the previous element. The first element of the new array is the same as the first element of the original array. Print the elements of this new array.
Input
The first line contains an integer N. The second line contains N space-separated integers representing the array elements.
Output
Print N space-separated integers, the elements of the new array.
Constraints
1 <= N <= 1000. Each array element is between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 10 12 15 13 18
Output
10 2 3 -2 5
Differences: 10, 12-10=2, 15-12=3, 13-15=-2, 18-13=5. Output: 10 2 3 -2 5
Example 2
Input
3 5 5 5
Output
5 0 0
Differences: 5, 5-5=0, 5-5=0. Output: 5 0 0
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 10 12 15 13 18
Example 2 input
3 5 5 5