competitivecoder
BAN USERThis will take time O(n) and space O(n)
map<int,bool>m;
bool TwoNumbersThatSumValue(int a[],int num)
{
for(int i=0;i<sizeof(a)/sizeof(a[0]),i++)
{
if(m[num-a[i]]==true)
{
return true;
}
}
return false;
}
int main()
{
int a[]={1,9,4,5}; //let us take this for example;
int sum=14; //Searching for sum==14
for(int i=0;i<sizeof(a)/sizeof(a[0]);i++)
m[a[i]]=true;
TwoNumbersThatSumValue(a,sum);
}
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
vector<int>v;
int proddig[10000];
int getdig(int x)
{
int prod=1,temp=x;
while(temp)
{
prod*=(temp%10);
temp/=10;
}
return proddig[x]=prod;
}
int main() {
memset(proddig,0,sizeof proddig);
long long int n;
cin>>n;
for(int i=1;i<=n/2;i++)
{
if(i*(proddig[i]?proddig[i]:getdig(i))==n)
v.pb(i);
}
if(v.size()==0)
cout<<"NO seeds exist!!!"<<endl;
else
{
for(int i=0;i<v.size();i++)
cout<<v[i]<<endl;
}
return 0;
}
Note- In my dfs function,see in the IF condition please remove the extra ; before 4 and 5.I don't know why but I am unable to remove that.Thank you :)
- competitivecoder June 06, 2015As above said,this can be done using dfs as well as bfs.Here's I implemented the same
#include <bits/stdc++.h>
using namespace std;
#define maxx 100
#define pb push_back
#define pii pair<int,int>
vector<pii>vpii[maxx];
#define inf 0x7fffffff
int X[]={+1,-1,0,0};//possible movements
int Y[]={0,0,+1,-1};
vector<int>v[maxx];
bool vis[maxx][maxx];
int a[4][5];
struct data
{
int x,y;
void set(int i,int j)
{
x=i;
y=j;
}
};
void dfs(data s)
{
vis[s.x][s.y]=true;
for(int i=0;i<4;i++)
{
data t;
int t1=s.x+X[i];
int t2=s.y+Y[i];
if(vis[t1][t2]==false && t1<4 && t1>=0 && t2>=0 && t2<5&& a[t1][t2]==0)
{
vis[t1][t2]=true;
t.set(t1,t2);
dfs(t);
}
}
}
int main() {
memset(vis,false,sizeof vis);
for(int i=0;i<4;++i)
for(int j=0;j<5;++j)
cin>>a[i][j];
data start,endd;
cin>>start.x>>start.y;
cin>>endd.x>>endd.y;
dfs(start);
if(vis[endd.x][endd.y])
cout<<"true";
else
cout<<"false";
return 0;
}
result=b*(a^0)+c*(a^1)
- competitivecoder June 05, 2015string s[]={"Good","Word","good","woRd"};
map<string,int>mp;
int l=((sizeof s)/(sizeof s[0]));
for(int i=0;i<l;i++){
string x=s[i];
transform(x.begin(),x.end(),x.begin(),::tolower);
mp[x]++;
}
string tofind="Good";
string keeporigional=tofind;
transform(tofind.begin(),tofind.end(),tofind.begin(),::tolower);
cout<<keeporigional<<"->"<<mp.find(tofind)->second;
Repdubinalinda4, Animator at Apache Design
Hello, I am a school librarian from Lewistown USA. I work in public or private schools at elementary, middle and ...
If you are using C++,we can use upper_bound and lower_bound which is actually binary search in some ways.
- competitivecoder June 13, 2015