Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:Peak element in a mountain array #6708

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
@@ -0,0 +1,32 @@
#include <stdio.h>

int findPeakUtil(int arr[], int low, int high, int n)
{
int mid = low + (high - low)/2;

if ((mid == 0 || arr[mid-1] <= arr[mid]) &&
(mid == n-1 || arr[mid+1] <= arr[mid]))
return mid;


else if (mid > 0 && arr[mid-1] > arr[mid])
return findPeakUtil(arr, low, (mid -1), n);


else return findPeakUtil(arr, (mid + 1), high, n);
}


int findPeak(int arr[], int n)
{
return findPeakUtil(arr, 0, n-1, n);
}


int main()
{
int arr[] = {1, 3, 20, 4, 1, 0};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Index of a peak point is %d", findPeak(arr, n));
return 0;
}
@@ -0,0 +1,31 @@
#include<iostream>
#include<vector>

using namespace std;

int peakIndexInMountainArray(std::vector<int>& arr) {
int n=arr.size();
int s=0;
int e = n-1;
int mid = s + (e-s)/2;
while(s<e){
if(arr[mid]<arr[mid+1]){
s=mid+1;
}
else{
e=mid;
}
mid = s + (e-s)/2;
}
return s;
}
int main(){
vector<int> arr;
arr.push_back(0);
arr.push_back(1);
arr.push_back(4);


int ans = peakIndexInMountainArray(arr);
cout<<ans<<endl;
}
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.peak</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Binary file not shown.
@@ -0,0 +1,9 @@
---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove binary and non-code files.

Only keep code files peak.c, cpp, py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done @AdiChat

triple: 'x86_64-apple-darwin'
binary-path: '/Users/admin/Desktop/cosmos/code/algorithm_applications/src/binary_search/peak_element_in_a_mountain_array/peak'
relocations:
- { offsetInCU: 0x26, offset: 0x26, size: 0x8, addend: 0x0, symName: _findPeakUtil, symObjAddr: 0x0, symBinAddr: 0x100003D90, symSize: 0x110 }
- { offsetInCU: 0x41, offset: 0x41, size: 0x8, addend: 0x0, symName: _findPeakUtil, symObjAddr: 0x0, symBinAddr: 0x100003D90, symSize: 0x110 }
- { offsetInCU: 0xA1, offset: 0xA1, size: 0x8, addend: 0x0, symName: _findPeak, symObjAddr: 0x110, symBinAddr: 0x100003EA0, symSize: 0x30 }
- { offsetInCU: 0xD7, offset: 0xD7, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x140, symBinAddr: 0x100003ED0, symSize: 0x87 }
...
@@ -0,0 +1,99 @@



# the peak element in the array
def findPeak(arr):


left = 0


right = len(arr) - 1

while (left < right):


mid = left + (right - left) // 2


if (arr[mid] < arr[(mid + 1)]):

left = mid + 1

else:


right = mid

return left


def BS(X, left, right, arr):

while (left <= right):

mid = left + (right - left) // 2

if (arr[mid] == X):
return mid

elif (X > arr[mid]):

left = mid + 1

else:

right = mid - 1

return -1

def reverseBS(X, left, right, arr):

while (left <= right):

mid = left + (right - left)

if (arr[mid] == X):
return mid

elif (X > arr[mid]):

right = mid - 1

else:

left = mid + 1

return -1

def findInMA(X, mountainArr):

peakIndex = findPeak(mountainArr)

res = -1


if (X >= mountainArr[0] and
X <= mountainArr[peakIndex]):

res = BS(X, 0, peakIndex, mountainArr)


if (res == -1):

res = reverseBS(X, peakIndex + 1,
mountainArr.size() - 1,
mountainArr)

print(res)

if __name__ == "__main__":


X = 3

arr = [ 1, 2, 3, 4, 5, 3, 1 ]

findInMA(X, arr)