1 1 vote Algorithms algorithms sorting time-complexity made-easy-test-series + – KISHALAY DAS 855 views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Arjun commented Oct 23, 2016 reply Follow flag Sort it, return first 2 elements? 0 0 replyShare KISHALAY DAS commented Oct 23, 2016 reply Follow flag It is not always that min two elements have least difference Like 10,2,6,5..after sort 2,5,6,10...here least diff is 1 between pair(5,6) 1 1 replyShare Arjun commented Oct 23, 2016 reply Follow flag yes, I missed that. So, traverse the array and see adjacent elements each time. 0 0 replyShare KISHALAY DAS commented Oct 23, 2016 reply Follow flag But it could be between any pair...not necessaryly adjacent 0 0 replyShare Kapil commented Oct 23, 2016 reply Follow flag It is asking for minimum. Adjacent is enough . O(N Log N + N). 0 0 replyShare Please log in or register to add a comment.
Best answer 1 1 vote Sort the array. Now do a pass through the array similar to the inner loop of selection sort as follows: min = 0; for(int i = 1; i< n-1; i++) { if(diff(A[i], A[i+1]) < diff(A[min], A[min+1]) ) min = i; } Finally A[min], A[min+1] give our required numbers. Arjun answered Oct 23, 2016 • selected Oct 23, 2016 by KISHALAY DAS Arjun comment Share Follow 0 reply Please log in or register to add a comment.