Skip to content

Update Binary Search.cpp #205

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
67 changes: 32 additions & 35 deletions C++/Binary Search.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid
if (arr[m] == x)
return m;

// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half
int binarySearch(int arr[],int size,int element){
int low,mid,high;
low = 0;
high = size-1;

while(low<=high){
mid = (low + high)/2;
if (arr[mid] == element)
{
cout<<"OK\n";
cout<<"The element "<<element<<" was foung on index: "<<mid<<endl;
return mid;
}
if (arr[mid]<element){
low = mid+1;
}
else
r = m - 1;
}

// if we reach here, then element was
// not present
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}
{
high = mid - 1;
}
}
cout<<mid<<endl;
return -1;
}

int main(){
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int size = sizeof(arr)/sizeof(int);
int element = 9;
int searchIndex = binarySearch(arr,size,element);
// cout<<"The element "<<element<<" was foung on index: "<<mid<<endl;
return 0;
}
58 changes: 38 additions & 20 deletions HackerEarth/Roy_and_profile_picture.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
#include<iostream>
//The reason why you don't see drastic changes is the code is same but you could've used {}braces to format which is a good practice and increases readability
using namespace std;
int main()
{
int L;
cin>>L;
int N;
cin>>N;
while(N--)

int main(){
int i,j;
cout<<"i";
cin>>i;
cout<<"j";
cin>>j;
cout<<endl;
while (j--)
{
int W,H;
cin>>W>>H;
if(W<L||H<L)
cout<<"UPLOAD ANOTHER"<<endl;
if(W>L&&H>L||W>L&&H==L||W==L&&H>L)
{ if(W!=H)
cout<<"CROP IT"<<endl;
else
cout<<"ACCEPTED"<<endl;
}
if(W==L&&H==L)
cout<<"ACCEPTED"<<endl;
int a,b;
cout<<"a";
cin>>a;
cout<<"b";
cin>>b;
if (a<i||b<i)
{
cout<<"UPLOAD ANOTHER"<<endl;
}
if(a>i&&b>i||a>i&&b==i||a==i&&b>i){
if (a!=b)
{
cout<<"CROP IT"<<endl;
}else
{
cout<<"ACCEPTED"<<endl;
}


}
if (a==i&&b==i)
{
cout<<"ACCEPTED"<<endl;
}


}
return 0;

return 0;
}