Beautiful Array

  • + 1 comment

    Yes .. I am considering each element as median and trying to find the cost of coins and return minimum coins.
    For the above example It will first consider 3 at index 0 as median do the operation, will consider 3 at index 1 as median do the operation similarly if we take six it will return 15 and for 7 it will return 9.

    Current Median Index - 0, median value - 3, upperMedianDiff - 7, LowerMedian Diff - 0, cost of coins - 2147483647, cost of coins to convert to 3 - 0
    Current Median Index - 1, median value - 3, upperMedianDiff - 7, LowerMedian Diff - 0, cost of coins - 2147483647, cost of coins to convert to 3 - 0
    Current Median Index - 2, median value - 6, upperMedianDiff - 1, LowerMedian Diff - 6, cost of coins - 2147483647, cost of coins to convert to 6 - 15
    Current Median Index - 3, median value - 7, upperMedianDiff - 0, LowerMedian Diff - 9, cost of coins - 15, cost of coins to convert to 7 - 9.

    I handled this case but yet some problem with the soultion. If possible i am ready to upload the entire code. Can you please help me to find the mistake in this logic ??

    • Challenge Author
      + 1 comment

      Sorry, maybe I don't understand your solution... You must check all possible numbers in interval [0...maxAi] for 'median'. You can not check only elements from array. Example :

      2 1 1

      4 2

      Array is: 3 3

      Answer 1

      • + 1 comment

        Thank you. Yes I missed this point. Thank you very much.

        • + 0 comments

          Also you may check starting from the ceiling of the mean value as from A[0] to the mean value can't be a solution.