Please read the new rule regarding the restriction on the use of AI tools. ×

Interesting problem

Revision en1, by _Chaitanya1999, 2024-10-05 20:30:02

I recently encountered a problem which states : There is an array A which contains n integers. In one operation we can swap Ai with one of its neighbours i.e., swap(Ai,Ai+1) or swap(Ai,Ai-1) provided any element in the array can be swapped atmax once. After doing some optimal number of operations, we have to find the max value of summation of i*Ai for i = 1 to n.

For ex — A : [2,1,4,3] n = 4

Ans : 30 Swap elements at index 1,2 and elements at 3,4 that gives us the final array as [1,2,3,4]. The sum is 1*1 + 2*2 + 3*3 + 4*4 = 1+4+9+16 = 30

Can someone help me with the ideas to solve this problem ?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English _Chaitanya1999 2024-10-05 20:30:02 651 Initial revision (published)