Find the difference between the alternate numbers in the set of numbers and find the index position of the smallest number with the largest difference

By Super Admin | Dec 19, 2020 | PHP
Share : Whatsapp

https://www.fundaofwebit.com/post/find-the-difference-between-the-alternate-numbers-in-the-set-of-numbers-and-find-the-index-position-of-the-smallest-number-with-the-largest-difference

Problem : 

Sheela is crazy on mathematics. Since decided to play a game by using a set of numbers.

The game is to find the difference between the alternate numbers in the set of numbers and find the index position of the smallest number with the largest difference. If more than one pair has the same largest difference consider the first occurrence. If the size of a set is less than 5 and greater than 10, Display "invalid array size".

Solution :

arr = [] 
ans = []
a = input("Enter the no of values:")

if(int(a) < 5 or int(a) >10):
    print("Invalid array size")
    exit(0)
else :
    print("Enter the Values")

for x in range(int(a)):
    arr.append(int(input()))

i = 0
while(i < len(arr)-2):
    ans.append(arr[i] - arr[i+2])
    i += 1

print("")
print(str(max(ans)) + " Greatest difference")

s1 = ans.index(max(ans))
s2 = ans.index(max(ans)) + 2

print(str(min(arr[s1],arr[s2])) + " is the smallest parent")
print(str(max(arr[s1],arr[s2])) + " is the greatest parent")


When we execute the above python code, it will prompt the user for an input (given below)

Enter the no of values:

Here i will give the input as 6.

Then it will prompt the user to enter 6 values. So the ouput terminal will look like :

Enter the no of values:6
Enter the Values

Enter the values you want to test it for. (Example given below)

65
45
54
23
25
24

Once we enter all the values, we will get the following output as response and the output terminal will look like:

Enter the no of values:6
Enter the Values
65
45
54
23
25
24

29 Greatest difference
25 is the smallest parent
54 is the greatest parent


Tags : How to find difference between alternate numbers, find difference between alternate numbers .