Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1a85d2

Browse files
Merge pull request #80 from harshchan/master
Added 2 Array concepts
2 parents 00ddfaa + f39d0d1 commit a1a85d2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

‎Algorithms/Array/moore.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<iostream>
2+
#include<conio.h>
3+
#include<stdlib.h>
4+
#include<bits/stdc++.h>
5+
6+
using namespace std;
7+
8+
9+
10+
main()
11+
{
12+
int n,i=0;
13+
cin>>n;
14+
int arr[100];
15+
while(i<n)
16+
{
17+
cin>>arr[i];
18+
i++;
19+
}
20+
int ele=arr[0],count=0,flag=0;
21+
for(i=0;i<n;i++)
22+
{
23+
if(ele!=arr[i])
24+
count--;
25+
if(ele==arr[i])
26+
count++;
27+
if(count==0)
28+
{
29+
ele=arr[i];
30+
}
31+
}
32+
for(i=0;i<n;i++)
33+
{
34+
if(arr[i]==ele)flag++;
35+
}
36+
if(flag>n/2)cout<<"majority element is "<<ele;
37+
else
38+
cout<<"No majority element";
39+
40+
return 0;
41+
}
42+
43+

‎Algorithms/Array/rotated_search.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Problem link
3+
https://leetcode.com/problems/search-in-rotated-sorted-array/
4+
*/
5+
6+
7+
#include<iostream>
8+
#include<conio.h>
9+
#include<stdlib.h>
10+
#include<bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
int hello()
15+
{
16+
vector<int>arr(100);
17+
int n;
18+
cin>>n;
19+
for(int i=0;i<n;i++)
20+
{
21+
cin>>arr[i];
22+
}
23+
int target;
24+
cin>>target;
25+
int low=0,high=n-1;
26+
while(low<=high)
27+
{
28+
int mid=low+(high-low)/2;
29+
if(arr[mid]==target)return mid;
30+
if(arr[mid]>=arr[low])
31+
{
32+
if(target>=arr[low]&&target<arr[mid])
33+
high=mid-1;
34+
else
35+
low=mid+1;
36+
}
37+
else
38+
{
39+
if(target>arr[mid]&&target<=arr[high])
40+
low=mid+1;
41+
else
42+
high=mid-1;
43+
}
44+
}
45+
46+
}
47+
48+
main()
49+
{
50+
cout<<hello();
51+
52+
53+
return 0;
54+
}
55+
56+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /