The question has been closed for the following reason "Duplicate Question" by divik544 27 Dec '17, 22:17
I think we can solve it using segment tree.Traverse from right to left,and add elements to segment tree ,so at index i,i have a segment tree for indices i+1 to n1,so store two things in segment tree ,first sum and count,we store these at the value ie if arr[]={3},segment[3:3]={1,3} (Count,sum),so for element ai,search for ai+2,int_max and int_min,ai2 ,so res+=sum1cnt1 * ai + cnt2 * ai  sum2, u could use cordinate compression if values are large. answered 27 Dec '17, 19:24

My idea is to first map input array to smaller values (to enable creation of frequency array). Now, read input, put value as key and its index in count table as value in map. Develop a count array simultaneously. Initialize another count array. Now, loop from 0 to N1, Deriving count of Number of elements Not belonging to set {A[i], A[i]1, A[i]+1} in left direction and in right direction, add ar[i]*(left  right) to Final answer and output. Calculation of Number of elements can be done as follows. During the loop, we are maintaining another count array NEWcount, which is count of elements In left side. So, if i1 = index of A[i]1, i2 = index of A[i]+1, and i3 = index of A[i] in count table, Then:
Add ar[i]*(V1V2) to sum for every i from 0 to N1. Have a look at This Submission answered 27 Dec '17, 20:36

Please Choose appropriate title for your problem.
PS: Writing an explanation for your problem.
Taking time to make implementation as simple as possible.
I remember answering this once: link
Oh yeah...
I too remember my incorrect solution. @meooow
@divik544, i don't think there would be need to another explanation, seeing that @meooow's solution is very clear.
Should you feel to need help, add comment.
@taran_1407 if your solution is different from the ones mentioned in that thread then please go ahead and share it.
Mine was to map every value with a smaller value so that we can make frequency array and from that, i will get number of Ai1 in left and right, Ai+1 in left and right. Rest is easy.
PS: Please help me too for this TLE https://www.codechef.com/viewsolution/16655060
@taran_1407 could u plz elabarote ur approach for 903D??
Indeed i must search properly before posting. btw Awesome solution @meooow much much clear than the posted editorial. you are great buddy :D