# Store max on left and start with sentinel.
maxToLeft = cell[0]
answer = -1
for checking = 1 to cell.lastIndex-1 inclusive:
switch on answer:
# Save if currently sentinel and item valid.
case -1:
if cell[checking] > maxToLeft:
answer = checking
# Set back to sentinel if saved answer is now invalid.
otherwise:
if cell[answer] >= cell[checking]:
answer = -1
# Ensure we have updated max on left.
if cell[checking] > maxToLeft:
maxToLeft = cell[checking]
# Final check against last cell.
if answer != -1:
if cell[cell.lastIndex] <= cell[answer]:
answer = -1
int arr1 = new int[source.length];
int highest = MIN_INT;
for (int i = 0; i < source.length; i++) {
arr1[i] = highest;
if (source[i] > highest) {
highest = source[i];
}
}
cell = [1, 3, 2, 6, 5, 7, 9, 8, 10, 8, 11]
maxToLeft = cell[0]
answer = -1
for checking in range(1, len(cell) - 1):
if answer == -1:
if cell[checking] > maxToLeft:
answer = checking
else:
if cell[answer] >=cell[checking]:
answer = -1
if cell[checking] > maxToLeft:
maxToLeft = cell[checking]
if answer != -1:
if cell[len(cell] - 1] <= cell[answer]:
answer = -1
if answer == -1:
print ("Not found")
else:
print("Found value", cell[answer], "at index", answer);
print(highLeft)
print(cell)
print(lowRight)
for idx in range(1, len(cell) - 1):
if cell[idx] > highLeft[idx] and cell[idx] < lowRight[idx]:
print("Found value", cell[idx], "at index", idx)
[0, 1, 3, 3, 6, 6, 7, 9, 9, 10, 10]
[1, 3, 2, 6, 5, 7, 9, 8, 10, 8, 11]
[2, 2, 5, 5, 7, 8, 8, 8, 8, 11, 0]
Found value 7 at index 5
int arr1 = new int[source.length];
int highest = MIN_INT;
for (int i = 0; i < source.length; i++) {
arr1[i] = highest;
if (source[i] > highest) {
highest = source[i];
}
}
arr2 = new int[source.length];
int lowest = MAX_INT;
for (int i = (source.length-1); i <= 0; i--) {
arr2[i] = lowest;
if (source[i] < lowest) {
lowest = source[i];
}
}
for (int i = 1; i < (source.length-1); i++) {
if ((arr1[i] < source[i]) && (source[i] < arr2[i])) {
return i; // or return source[i]
}
}
// int[] arr = { 10, 11, 1, 2, 12, 13, 14};
int[] arr = { 1, 3, 2, 6, 5, 7, 9, 8, 10, 8, 11};
Integer firstMax = null;
Integer overallMax = null;
for (int i = 1; i < arr.length - 1; i++) {
int currentElement = arr[i];
if (firstMax == null) {
if (overallMax == null) {
firstMax = currentElement;
} else if (overallMax != null && currentElement > overallMax) {
firstMax = currentElement;
}
}
if (overallMax == null || currentElement > overallMax) {
overallMax = currentElement;
}
if (firstMax != null && currentElement < firstMax) {
// We found a smaller element, so all max found so far is useless. Start fresh.
firstMax = null;
}
}
System.out.println(firstMax);
/**
* Problem:
* there is an array of number, find an element which is larer than elements before it, and smaller than elements after it,
* refer: http://stackoverflow.com/questions/41293848
*
* Solution:
* loop through array, remember max value of previous loopped elements, compare it to next element, to check whether the first condition is met,
* when found an element met the first condition, then loop elements after it to see whether second condition is met,
* if found, then that's it; if not found, say at position 'y' the condition is broken, then the next candidate must be after y, thus resume the loop from element after y,
* until found one or end of array,
*
* @author Eric Wang
* @date 2016-12-23 17:08
*/
#include <stdio.h>
// find first matched number, return its index on found, or -1 if not found,
extern int findFirstMidNum(int *arr, int len);
int findFirstMidNum(int *arr, int len) {
int i=0, j;
int max=arr[0];
while(i < len) {
printf("\n");
if(arr[i] <= max) {
printf("give up [%d]-th element {%d}, 1st condition not met\n", i, arr[i]);
i++;
continue;
}
max = arr[i]; // update max,
printf("checking [%d]-th element {%d}, for 2nd condition\n", i, arr[i]);
j = i+1;
while(j < len) {
if(arr[j] <= max) {
printf("give up [%d]-th element {%d}, 2nd condition not met\n", i, arr[i]);
break;
}
j++;
}
printf("position after 2nd check:\ti = %d, j = %d\n", i, j);
if(j==len && j>i+1) {
return i;
} else {
max = arr[j-1]; // adjust max before jump,
i = j+1; // jump
printf("position adjust to [%d], max adjust to value {%d}, after 2nd check\n", i, arr[j-1]);
}
}
return -1;
}
int main() {
int arr[] = {1, 3, 2, 6, 5, 7, 9, 8, 10, 8, 11};
int len = sizeof(arr)/sizeof(arr[0]);
printf("\n============ Input array ============\n");
printf("size:\t%d\n", len);
printf("elements:\t{");
int i;
for(i=0; i<len; i++) {
printf("%d, ", arr[i]);
}
printf("}\n\n");
printf("\n============ Running info ============\n");
int pos = findFirstMidNum(arr, len);
printf("\n============ Final result============\n");
if (pos < 0) {
printf("Element not found.\n");
} else {
printf("Element found at:\n\t position [%d], with value: {%d}\n", pos, arr[pos]);
}
printf("\n");
return 0;
}
============ Input array ============
size: 11
elements: {1, 3, 2, 6, 5, 7, 9, 8, 10, 8, 11, }
============ Running info ============
give up [0]-th element {1}, 1st condition not met
checking [1]-th element {3}, for 2nd condition
give up [1]-th element {3}, 2nd condition not met
position after 2nd check: i = 1, j = 2
position adjust to [3], max adjust to value {3}, after 2nd check
checking [3]-th element {6}, for 2nd condition
give up [3]-th element {6}, 2nd condition not met
position after 2nd check: i = 3, j = 4
position adjust to [5], max adjust to value {6}, after 2nd check
checking [5]-th element {7}, for 2nd condition
position after 2nd check: i = 5, j = 11
============ Final result============
Element found at:
position [5], with value: {7}