//Array which has n elements in it. //1st element->a[k] ascending order. //a[k]->last element descending order. //Find largest element in this array. // //Example : //1 2 3 4 3 2 1 //Output 4 char calculateOrder(int first, int second, int third) { //desc if (second< first && second > third) { return 'd'; } else if (second > first && second < third) { return 'a'; } else { return '0'; } } int CompareThree(int first, int second, int third) { //max of three if (first>second && first > third) return first; else if (second> first && second > third) return second; else return third; } void kElement(int arrIntegers[], int left, int right) { int mid = (left + right) / 2; //Calculate order of mid-1, mid, mid+1 char order = calculateOrder(arrIntegers[mid - 1], arrIntegers[mid], arrIntegers[mid + 1]); if (order == 'd') //discard right { kElement(arrIntegers, left, mid); } else if (order == 'a') { kElement(arrIntegers, mid, right); } else { printf("%d", CompareThree(arrIntegers[left], arrIntegers[mid], arrIntegers[right])); return; } } int main() { int intArray[6] = { 1,2,3,4,3,2 }; kElement(intArray, 0, 5); return 1; }
Click like and share to be connected with us @ Facebook.
A place where we love to share and discuss hottest technologies, innovations and researches around.