I'm trying to return the index of the element to be searched. All the elements are being detected except the first element. What's wrong? Why is the function not returning index 0?
using namespace std;
int binsearch(int a[],int x,int y)
{
int low=a[0];
int high=a[x-1];
while(low<=high)
{
int mid=(low+high)/2;
if(y==a[mid])
{
return mid;
}
else if(y>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
return -1;
}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int x=sizeof(a)/sizeof(a[0]);
int y;
cin>>y;
cout<<binsearch(a,x,y);
}
int low=a[0];
int high=a[x-1];
should be
int low=0;
int high=x-1;
Dynamically Initialization of MinStack is not able to do. Why?
#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class Stack
{
int top;
int length;
int *arr;
public:
//Maximum size of Stack
Stack(int len)
{
top = -1;
length=len;
arr=new int[length];
} //constructor
void push(int data);
int pop();
bool isEmpty();
bool isStackFull();
int Size();
void Display();
int getPeek();
};
void Stack::push(int data)
{
if (isStackFull())
{
cout << "Stack Overflow"<<endl;
}
else
{
arr[++top] = data;
}
}
int Stack::pop()
{
if (isEmpty())
{
cout << "Stack Underflow"<<endl;
return 0;
}
else
{
int data = arr[top--];
return data;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
bool Stack::isStackFull()
{
return (top == length-1);
}
int Stack::Size()
{
return (top +1);
}
int Stack::getPeek()
{
return arr[top];
}
void Stack::Display()
{
for(int i=top;i!=-1;i--)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
class SpecialStack: public Stack
{
public:
Stack min1;
SpecialStack(int len):Stack(len)
{
min1= new Stack(len);
}
int pop();
void push(int x);
int getMin();
};
void SpecialStack::push(int x)
{
if(isEmpty()==true)
{
Stack::push(x);
min1.push(x);
}
else
{
Stack::push(x);
int y = min1.pop();
min1.push(y);
if( x < y )
min1.push(x);
else
min1.push(y);
}
}
/* SpecialStack's member method to remove an element from it. This method
removes top element from min stack also. */
int SpecialStack::pop()
{
int x = Stack::pop();
min1.pop();
return x;
}
int SpecialStack::getMin()
{
int x = min1.pop();
min1.push(x);
return x;
}
int main()
{
SpecialStack s(3); //size of stack should be 3
s.push(10);
s.push(20);
s.push(30);
cout<<s.getMin()<<endl;
s.push(5);
cout<<s.getMin();
return 0;
}
Dynamically Initialization of MinStack is not able to do. Why, How to rectify this problem.Please explain.
In the SpecialStack constructor you initialize the base Stack properly, but not the min1 member. They should be handled the same way:
SpecialStack(int len):Stack(len),min1(len)
{ }
When you don't have min1 in the constructors initializer list, the compiler tries do call its default constructor, but the Stack class doesn't have one.
I am new to graphs and just started practicing bfs.
https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/
when i compile my problem it runs without a problem but when i submit it, it throws error.
My code:
#include <iostream>
#include <list>
using namespace std;
class Graph{
int V;
list<int>*adj;
public:
Graph(int v);
void addedge(int u,int v);
void bfs(int s);
};
Graph::Graph(int v)
{
this->V=v;
adj=new list<int>[v];
};
void Graph:: addedge(int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void Graph:: bfs(int s)
{
bool *visited=new bool[V];
for(int i=0;i<V;i++)
visited[i]=false;
list<int>queue;
visited[1]=true;
int temp;
queue.push_back(1);
int curr_level=1;
list<int>::iterator i;
int counter=1;
//int flag=0;
int result=0;
int count=0;
while(!queue.empty())
{
temp=queue.front();
queue.pop_front();
counter--;
for(i=adj[temp].begin();i!=adj[temp].end();++i)
{
if(!visited[*i])
{
visited[*i]=true;
queue.push_back(*i);
}
count++;
}
if(counter>=0)
{
result+=count;
count=0;
}
if(counter==0)
{
curr_level++;
counter=result;
}
if(curr_level==s)
{
break;
}
}
cout<<result<<endl;
}
int main()
{
int n;
cin>>n;
Graph g(n);
int x,y;
n--;
while(n--)
{
cin>>x>>y;
g.addedge(x,y);
}
int p;
cin>>p;
if(p==1)
return 1;
else
g.bfs(p);
}
This is rmq using segment tree.
But i am not getting correct output can anybody tell me where i am wrong.
`
#include<iostream>
#include<cmath>
#define inf 1e9
using namespace std;
int get_mid(int a,int b){
return a+(b-a)/2;
}
int min(int a,int b){
int temp=(a<b)?a:b;
return temp;
}
int get_min_util(int str[],int beg,int end,int l,int r,int i){
if(l<=beg && r>=end)
return str[i];
if(l>end || r<beg){
return 0; ////////c1
}
int mid=get_mid(beg,end);
return (get_min_util(str,beg,mid,l,r,2*i+1),get_min_util(str,mid+1,end,l,r,2*i+2));
}
int get_min(int str[],int len,int l,int r){
if(l<0 || r>len-1 || l>r){
return 0;
}
else
return get_min_util(str,0,len-1,l,r,0);
}
int build_rmq_arr_util(int str[],int beg,int end,int big_arr[],int i){
if(beg==end){
big_arr[i]=str[beg];
return big_arr[i];
}
int mid=get_mid(beg,end);
big_arr[i]=min(build_rmq_arr_util(str,beg,mid,big_arr,2*i+1),build_rmq_arr_util(str,mid+1,end,big_arr,2*i+2));
return big_arr[i];
}
int*bulid_rmq_arr(int str[],int len){
int temp=ceil(log2(len));
temp=2*pow(2,temp)-1;
int *big_arr=new int[temp];
build_rmq_arr_util(str,0,len-1,big_arr,0);
return big_arr;
}
void pop(int arr[],int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
int str[]={1,2,3,4,5,6};
int len=sizeof(str)/sizeof(int);
int *rmq_arr;
rmq_arr=bulid_rmq_arr(str,len);
//pop(rmq_arr,3*len);
cout<<get_min(rmq_arr,len,1,2);
}
`
output of this code:
0
expected output in range 1-2 is 2
function get_min_util return 0 from if condition where comment ////////c1 is written
problem is in get_min_util().It should return infinity when not in range.Also you have to return minimum of left and right child.
int get_min_util(int str[],int beg,int end,int l,int r,int i){
if(l<=beg && r>=end)
return str[i];
if(l>end || r<beg){
return inf; ////////c1
}
int mid=get_mid(beg,end);
return min(get_min_util(str,beg,mid,l,r,2*i+1),get_min_util(str,mid+1,end,l,r,2*i+2));
}
This is the skyscraper floors problem from spoj. I m getting SIGABRT when I run the code.Link for the problem is http://www.spoj.com/problems/SCRAPER/. When I run this code on my PC, it runs without any error but spoj's online judge shows SIGABRT error.
#include <iostream>
#include <vector>
using namespace std;
struct floor
{
unsigned int floornum;
bool calldone;
vector <floor*> connection;
floor()
{
floornum=0;
calldone=0;
}
};
struct elevator
{
unsigned int x,y;
elevator()
{
x=y=0;
}
};
struct skyscrapper
{
unsigned int f,e,a,b;
vector <elevator> ele;
vector <floor*> flooraddress; //this is a spoj problem
void read()
{
cin>>f>>e>>a>>b;
elevator *temp;
for(unsigned int i=0;i<e;i++)
{
temp=new elevator;
cin>>(*temp).x>>(*temp).y;
ele.push_back(*temp);
}
for (unsigned int i=0;i<f;i++)
{
floor* tempp=new floor;
(*tempp).floornum=i;
flooraddress.push_back(tempp);
}
}
void allotaddress()
{
unsigned int j,k;
for(unsigned int i=0;i<ele.size();i++)
{
j=ele[i].y;
k=ele[i].x;
while(j<f)
{
if(j!=ele[i].y)
{
(*(flooraddress[j])).connection.push_back(flooraddress[j-k]);
(*(flooraddress[j-k])).connection.push_back(flooraddress[j]);
}
j=j+k;
}
}
}
};
bool flag;
bool traverse(floor* m,int destination)
{
if((*m).calldone==1)
{
return 0;
}
(*m).calldone=1;
if((*m).floornum==destination)
return 1;
if((*m).connection.empty())
return 0;
for(int i=0;i<(*m).connection.size();i++)
{
flag=traverse(((*m).connection[i]),destination);
if(flag==1)
return flag;
}
return 0;
}
int main()
{
int n;
cin>>n;
skyscrapper iit[n];
bool ans[n];
for(int i=0;i<n;i++)
{
iit[i].read();
iit[i].allotaddress();
ans[i]=traverse(iit[i].flooraddress[iit[i].a],iit[i].b);
}
for(int i=0;i<n;i++)
{
if(ans[i]==1)
cout<<"It is possible to move the furniture."<<endl;
else
cout<<"The furniture cannot be moved."<<endl;
}
return 0;
}