class LargestRangeOfArray { /* * Given an array of integers, return the largest range, inclusive, of integers that are all included in the array. For example: * given the array [9, 6, 1, 3, 8, 10, 12, 11], return (8, 12) since 8, 9, 10, 11, and 12 are all in the array. * */ fun largestRangeOfArray(array:IntArray):String{ var left =0 var right =0 array.sort() var max = 0 //1, 3, 6, 8, 9, 10, 11,12 var size = array.size val result = HashMap<Int,ArrayList<Int>>() while (left<size){ right = left+1 if (right>=size-1){ right = size-1 } val rangeArray = ArrayList<Int>() var maxLength = 0 while (array[right]-array[left]==1){ rangeArray.add(array[left]) rangeArray.add(array[right]) left++ right++ maxLength++ if (right==size){ break } } max = Math.max(max,maxLength) result.put(maxLength,rangeArray) //System.out.println("left:$left") left++ } val resultList = result.get(max) resultList?.sort() return "(${resultList?.get(0)},${resultList?.get(resultList.size-1)})" } }