Skip to content

Solution for Problem of 29 Oct in C++ #532

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 2022-Oct/29 Oct 2022/Suraj_Kumar_Kushwaha.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
using namespace std;

int solve(int arr[], int n){
if(n==1 || arr[0]>arr[1])
return 1;
if(arr[n-1]>arr[n-2])
return n;

for(int i=1; i<n-1; i++)
if(arr[i]>arr[i-1] and arr[i]>arr[i+1])
return i+1;

return -1;
}

bool check(int index, int arr[], int n){
if(index==1 and n==1)
return true;

if(index==1 and arr[0]>arr[1])
return true;

if(index==n and arr[n-1]>arr[n-2])
return true;

if(arr[index-1]>arr[index-2] and arr[index-1]>arr[index])
return true;

return false;
}

int main() {
cout << "Enter the size of array" << endl;
int n; cin >> n;
int arr[n];
cout << "Enter elements of array" << endl;
for(int i=0; i<n; i++)
cin >> arr[i];

int index = solve(arr, n);
if(index!=-1)
cout << "Possible Index of Peak element is : " << index << endl;
else
cout << "No Peak element found " << endl;

cout << "Output: ";
if( index!=-1 and check(index, arr, n)) cout << 1 << endl;
else cout << 0 << endl;
}