r/programmingchallenges • u/iwip_eu • Oct 02 '16
C# find the index of the element with biggest difference to it’s neighbor in int array
I have an int array (ex. {7,12,5,3,11,6,10,2,9} I need to find an smallest number that has the biggest difference to it’s neighbor to the right. I know it’s element 3, but I have no clue how to get it programmatically.
Can anyone please help?
4
Upvotes
8
u/kc3w Oct 02 '16
4
u/GambitRS Oct 02 '16
how do you know it's 3?
1
u/Sharpieman20 Oct 03 '16
Are there any runtime restrictions?
1
u/_Mushy Oct 03 '16
You need to create an int the will hold the numbers highest difference. You also need a loop to compare the differences. So, each iteration of the loop will subtract to its right neighbor and compare it to the current highest stored difference.
Let me know if you need a better explanation.
1
u/09jlardinois Oct 07 '16
Eh. Progamming chores or not, I'm also interested in the answer. Could be useful for crypto.
I'd like to see everyone's different solutions to this.
I would say, psuedo code,
test equals index minus (index + 1) for length of array, check if index out of range if index minus (index + 1) is greater than test, test equals current index return test