problem link:
Discuss:
In this problem you have to first find the difference of successive element.and check is there all digit between 1 to n-1. to do so you can take a bool type array. find difference and check weather the number less then n and bool is true.if bool is true then make it false.if not then it's not jolly.
try yourself before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,ara[3001],diff,a,b;
bool bl[3001];
while(cin >>n)
{
memset(bl,true,sizeof(bl));
bool jolly=true;
cin >>b;
for(i=1;i<n;i++)
{
a=b;
cin >>b;
diff=abs(a-b);
if(diff<n&&bl[diff])
bl[diff]=false;
else
jolly=false;
}
if(jolly)
cout <<"Jolly"<<endl;
else
cout <<"Not jolly"<<endl;
}
return 0;
}
Discuss:
In this problem you have to first find the difference of successive element.and check is there all digit between 1 to n-1. to do so you can take a bool type array. find difference and check weather the number less then n and bool is true.if bool is true then make it false.if not then it's not jolly.
try yourself before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,ara[3001],diff,a,b;
bool bl[3001];
while(cin >>n)
{
memset(bl,true,sizeof(bl));
bool jolly=true;
cin >>b;
for(i=1;i<n;i++)
{
a=b;
cin >>b;
diff=abs(a-b);
if(diff<n&&bl[diff])
bl[diff]=false;
else
jolly=false;
}
if(jolly)
cout <<"Jolly"<<endl;
else
cout <<"Not jolly"<<endl;
}
return 0;
}
No comments:
Post a Comment