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
Shubh2003
wants to merge
4
commits into
OpenGenus:master
Choose a base branch
from
Shubh2003:peak_element_in_a_mountain_array
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e1b518b
added code for peak element in a mountain array in c++
Shubh2003 0a1d89d
added code for peak element in a mountain array in c and c++
Shubh2003 3c3aadd
added code for peak element in a mountain aaray in C , C++ and Pyhton
Shubh2003 b6e0071
removed unecessary files
Shubh2003 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+32.9 KB
code/algorithm_applications/src/binary_search/peak_element_in_a_mountain_array/peak
Binary file not shown.
32 changes: 32 additions & 0 deletions
32
code/algorithm_applications/src/binary_search/peak_element_in_a_mountain_array/peak.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
31 changes: 31 additions & 0 deletions
31
code/algorithm_applications/src/binary_search/peak_element_in_a_mountain_array/peak.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
20 changes: 20 additions & 0 deletions
20
...ications/src/binary_search/peak_element_in_a_mountain_array/peak.dSYM/Contents/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 added
BIN
+9.59 KB
...rc/binary_search/peak_element_in_a_mountain_array/peak.dSYM/Contents/Resources/DWARF/peak
Binary file not shown.
9 changes: 9 additions & 0 deletions
9
...peak_element_in_a_mountain_array/peak.dSYM/Contents/Resources/Relocations/x86_64/peak.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
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 } | ||
... |
99 changes: 99 additions & 0 deletions
99
code/algorithm_applications/src/binary_search/peak_element_in_a_mountain_array/peak.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @AdiChat