Your code is fine but you are printing Yes instead of YES and same for NO.
Oh no…
Thanks…
Was getting WA , help me to find that wrong logic…
vthz.25
for _ in range(int(input())):
n = int(input())
arri = [int(e) for e in input().split()]
current = arri[0]
temp = []
flag = True
temp.append(current)
temp = []
for i in range(1, len(arri)):
if arri[i] != current:
current = arri[i]
if arri[i] in temp:
flag = False
break
temp.append(current)
nflag = False
if flag:
dic = {}
for z in arri:
if z not in dic:
dic[z] = 1
else:
dic[z] += 1
#nflag=len(dic)!=len(set(dic.values()))
#print(dic)
dic2={}
for x,value in dic.items():
#print(str(x)+" "+str(dic[x]))
if value not in dic2:
dic2[value]=1
else:
dic2[value]+=1
#print(dic2)
myset=set(arri)
if len(arri)==len(myset):
nflag=True
if len(dic)==len(dic2):
nflag=True
if flag and nflag:
print("YES")
else:
print("NO")
Can you share the link to your submission?
Can anyone please help me figure out, why this doesn’t work?
package Problems;
import java.util.HashSet;
import java.util.Scanner;
import java.util.HashMap;
public class ChefLand {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner obj = new Scanner(System.in);
int testcase = obj.nextInt();
for(int i = 0;i<testcase;i++) {
int number = obj.nextInt();
int[] array = new int[number];
boolean flag = false;
HashSet<Integer> hashSet = new HashSet<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int j = 0;j<number;j++) {
array[j] = obj.nextInt();
}
for(int k = 0;k<array.length;k++) {
if(!map.containsKey(array[k])) {
map.put(array[k], 1);
}else {
map.replace(array[k], map.get(array[k])+1);
}
}
for(int num :map.values()) {
if(hashSet.contains(num)) {
flag = true;
System.out.println("NO");
break;
}else {
hashSet.add(num);
}
}
if(flag==false) {
System.out.println("YES");
}
}
}
}
can you please help with this. I am facing a timeout error!
#include
#include
#include
#include
using namespace std;
bool check(vector v)
{
map<int, bool> m;
m[v[0]] = true;
int k = 0;
set s;
for (int i = 1; i < v.size(); ++i)
{
if (v[i] == v[i - 1])
continue;
if (m[v[i]] == true)
return false;
if (s.find(i - k) != s.end())
return false;
m[v[i]] = true;
s.insert(i - k);
k = i;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t–)
{
int n;
cin >> n;
vector v;
for (int i = 0; i < n; ++i)
{
int q;
cin >> q;
v.push_back(q);
}
if (check(v))
cout << “YES” << endl;
else
cout << “NO” << endl;
}
return 0;
}
@vthz and @sarthakbhatia both of your submissions are wrong for the below test case.
1
2
1 2
P.S. Try to look over line 10 @vthz .
The link to my code is given above. Please help. I’m unable to find the mistake. It shows WA.
@rishup_nitdgp
All right, I didn’t noticed that I have declared temp twice. After removing second declaration, it worked. Lost 64 for nothing…hahahaha. Thanks!!!
Hey, can you tell me whats wrong with my code…
Link: CodeChef: Practical coding for everyone
I am not sure for which test case it is failing…
Here a case for which your code is giving wrong output.
1
11
100 100 100 100 100 100 1000 1000 1000 1000 1000
thanks ! learnt a lot from this question
Can anybody tell what is wrong with this code
#include
#include
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int n,count{},flag{},c{},flag2{};
cin>>n;
int arr[n];
vector v;
vector x;
vector z;
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++){
if(arr[i]==arr[i-1]&&i!=0){
continue;
}else{
count=1;
for(int j=i+1;j<n;j++){
if(arr[i]==arr[j])
count++;
}
if(i>=2){
flag=0;
for(int k=i-2;k>=0;k–){
if(arr[i]==arr[k]){
flag=1;
break;
}
}
}
if(flag!=1){
v.push_back(count);
x.push_back(arr[i]);
}
}
}
for (size_t i = 0; i < v.size(); i++) {
for (size_t j = i+1; j < v.size(); j++){
if (v[i] == v[j]&&v[j]!=1){
flag2 = 1;
break;
}
}
}
for(size_t j=0;j<v.size();j++){
for(size_t i=0;i<v[j];i++){
z.push_back(x[j]);
}
}
for(int j=0;j<n;j++){
if(arr[j]!=z[j]){
c++;
}
}
if(c>=1||flag2==1){
cout<<“NO\n”;
}else{
cout<<“YES\n”;
}
}
return 0;
}