Skip to content

Commit 2225518

Browse files
authored
Merge pull request #505 from himanshu30-max/leetcode
leetcode problems
2 parents b21b021 + 4adc09a commit 2225518

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

LeetCode/Max_consequtive_one.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
int findMaxConsecutiveOnes(int nums[],int n) {
7+
// sort(nusm.begin(),nums.end());
8+
int count =0,maxi=0;
9+
for(int i=0;i<n;i++){
10+
if(nums[i]==1){
11+
count++;
12+
}
13+
else{
14+
count=0;
15+
}
16+
maxi=max(count,maxi);
17+
}
18+
return maxi;
19+
}
20+
21+
//driver code
22+
int main(){
23+
int n;
24+
cout<<"Enter the size of array";
25+
cin>>n;
26+
int arr[n];
27+
//taking input in the array
28+
for(int i=0;i<n;i++){
29+
cin>>arr[i];
30+
}
31+
int ans= findMaxConsecutiveOnes(arr,n);
32+
cout<<ans<<endl;
33+
34+
return 0;
35+
36+
}

LeetCode/Max_consequtive_one.exe

46.8 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//This program will return the peak element index in a mountain array
2+
#include <iostream>
3+
using namespace std;
4+
5+
// function code
6+
int peakIndexInMountainArray(int arr[],int size){
7+
int start=0;
8+
int end=size-1;
9+
int mid=start+(end-start)/2;
10+
while(start<end){
11+
if(arr[mid]<arr[mid+1])
12+
start=mid+1;
13+
else
14+
end=mid;
15+
mid=start+(end-start)/2;
16+
}
17+
return start;
18+
}
19+
20+
//driver code
21+
int main(){
22+
int n;
23+
cout<<"Enter the size of array";
24+
cin>>n;
25+
int arr[n];
26+
//taking input in the array
27+
for(int i=0;i<n;i++){
28+
cin>>arr[i];
29+
}
30+
int ans= peakIndexInMountainArray(arr,n);
31+
cout<<ans<<endl;
32+
33+
return 0;
34+
35+
}
44.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)