Array sum of difference of first smallest k elment
Posted
by
prateeak ojha
on Programmers
See other posts from Programmers
or by prateeak ojha
Published on 2013-11-12T18:57:02Z
Indexed on
2013/11/12
22:02 UTC
Read the original article
Hit count: 158
Hi i come across this challenge on an online programing challange the task is so simple for ex
WE have to variable N and K where N is where N is lenght of array and we have to find sum of duiffrence of K smallest element of array as
Sample Input
10
4
1
2
3
4
10
20
30
40
100
200
Sample Output
10
Explanation #0 We have 10 as N size of array and K is 4 here the next N values are array's elments in order to find sum of diffrences we will takek smallest values from array which are 1,2,3,4 then will perform operation(sum of difference) as |1-2| + |1-3| + |1-4| + |2-3| + |2-4| + |3-4| = 10
I tried solving the problem and found a way through which i can solve the problem wit N^3 complexity but my solution is rejected i need a approach to solve the problem with n complexity i still can't figure out a way ..
looked at some solution but coud'nt find the exact way to approach. if anybody have a better idea and would like to share it would be appreciable thanks in advance
© Programmers or respective owner