In: Computer Science
Foundation of Computer Science
Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it. Write its corresponding program using your favorite programming language
Please find below code and algorithm for getting largest difference. Don't forget to give a Like.
Code:
def algorithm(list1): list1.sort() #sorting the algorithm from small to largest #largest difference is largest/last element - first element #largest element - smallest element will gives largest difference largest_dif=list1[len(list1)-1]-list1[0] second_largest=list1[len(list1)-2]-list1[1] print("Largest difference is",largest_dif) print("Second larget difference is", second_largest) list1=list(map(int,input("Enter numbers with list").split())) algorithm(list1)
Output: