Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a method that returns the index
- * of the first element in array that is
- * bigger than its neighbors, or -1, if
- * thereโs no such element.
- */
- using System;
- class FirstBiggerThenNeighbors
- {
- static int BiggerNeighborFinder(int[] array)
- {
- int index = 0;
- for (int i = 1; i < array.Length - 1; i++)
- {
- if ((array[i] > array[i + 1]) && (array[i] > array[i - 1]))
- {
- index = i;
- break;
- }
- else
- {
- index = -1;
- }
- }
- return index;
- }
- static void Main()
- {
- int[] myArray = { 1,2,3,4,5,4,7,8 };
- Console.WriteLine(BiggerNeighborFinder(myArray));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement