problem link I know the correct approach but … i don’t know whats wrong with this approach-> i first added the edge to the MST whose weight is nearest to K (means whose abs(k-edgewiight) is minimum) and then formed the MST… if there are edges in final mst whose weight is greater then k i add this to ans… if there are no edge greater the k in the final mst then i return k - maximum edge weight
Your logic is a little incorrect.
My approach:- First make ST (spanning tree) with only edges whose weight is less than or equal to K (wei <= K). and let’s assume its answer is ans1 ( = K - max(wei)). Then if you can form full ST (it have a total of N - 1 edges), then correct ans ( = min (ans1, (minimum weight just greater than K) - K)). or else if you cant form full ST (total edges is less than N - 1), then first sort the edges whose weight is strictly greater than K, then loop through them and calculate “cost” as (cost += wei - K (wei is here strictly greater than K)) until you found total of N - 1 edges (you can form full ST). this cost will be the answer in that case
my soln link