Wednesday, March 23, 2011

Find Maximum in Partially Increasing & Partially Decresing Array

There is an integer array. The array represent a graph which increases till one point and then start decreasing. WAP to find the maximum number in that array. Write production quality code which handles all the scenarios. He also checked various combinations of number with my code.


#include
#include

/* Standard Binary Search function*/
//binarySearch(arr, 0,7)

int binarySearch(int arr[], int low, int high)
{

if(high >= low)
{
int mid = (low + high)/2; /*low + (high - low)/2;*/

if(arr[mid-1]arr[mid+1])
return arr[mid];
if(arr[mid-1] return binarySearch(arr, (mid + 1), high);
else if(arr[mid-1]>arr[mid] && arr[mid]>arr[mid+1])
return binarySearch(arr, low, (mid -1));
}
/*Return -1 if element is not found*/
return -1;
}

/* Driver program to check above functions */
int main()
{
int arr[] = {3, 4, 5, 6,7,1, 2};
printf("Maximum Element in Array is %d", binarySearch(arr, 0,5));
getchar();
return 0;
}


Run Here https://ideone.com/JOzXP

No comments :