Cheapest And Costliest Fibre Network Linking Warehouses By Grade Gap

Company: Flipkart_12nov

Difficulty: medium

Problem Statement

Problem Description A logistics company runs n warehouses, and warehouse i has an automation grade g[i] . The company wants to lay fibre links so that every warehouse can exchange data with every other one, directly or through other warehouses, while using as few links as possible. With n warehouses that means exactly n - 1 links forming a connected network (a spanning tree). Laying a link between warehouses u and v costs |g[u] - g[v]| . Any pair of warehouses may be linked. Implement cableCostRange(g) , which returns two numbers: the smallest possible total cost of such a network and the largest possible total cost of such a network. Input Format The first line holds n , the number of warehouses. The second line holds the n grades g[1] to g[n] , separated by spaces. Output Format Print two integers on one line separated by a space: the minimum total cost, then the maximum total cost. A single warehouse needs no links, so both values are 0 . Use 64-bit integers. Constraints 1 <= n &