Given an array of numbers, how to find sum of adjacent two numbers

Hi,

I have an array of numbers. How do I find the sum of adjacent two numbers using Apache Spark?

Input array - 3,4,1,5,9, 10]
Output - [7, 6, 19]

How about this?

var r = sc.parallelize(Array(3,4,1,5,9, 10))
var s = r.zipWithIndex()
var t = s.map(x => (x._2/2, x._1))
var res3 = t.reduceByKey(_+_).map(_._2)
res3.collect()
//7, 6, 19

Yes, it is working. Thanks for the solution Sandeep

//Pyspark
data = [3,4,1,5,9, 10]
arrayData = sc.parallelize(data)
zipIdx = arrayData.zipWithIndex()
//[(3, 0), (4, 1), (1, 2), (5, 3), (9, 4), (10, 5)]
getUnqKey = zipIdx.map(lambda x : ((x[1]/2),x[0]))
//[(0, 3), (0, 4), (1, 1), (1, 5), (2, 9), (2, 10)]
redByKey = getUnqKey.reduceByKey(lambda a,b : a + b).map(lambda x : x[1])
redByKey.collect()
//[7, 6, 19]

1 Like

Hi @sachinkerala,

Thanks for sharing the Python version :slight_smile: