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;
}
Can someone point out the mistake in my code.
https://www.codechef.com/viewsolution/33338571
And why does codechef does not show test cases where the solution fails.
@rishup_nitdgp please help.
Please help me know what is wrong with my code?
It is working for most test cases but I’m getting wrong answer. I am new here so forgive me if I’m missing something.
import java.io.FileInputStream;
import java.util.*;
public class Main {
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
if(T<1 || T>100){
System.out.println(“NO”);
return;
}
int N;
Integer[] arrayList;
int A;
for(int i = 0; i < T; i++){
N = scanner.nextInt();
if(N < 1 || N > 103){
System.out.println(“NO”);
continue;
}
arrayList = new Integer[N];
boolean validIngredients = true;
for(int j = 0; j < N; j++){
A = scanner.nextInt();
if(A < 1 || A > 103){
validIngredients = false;
break;
}
arrayList[j] = A;
}
if(!validIngredients) {
System.out.println(“NO”);
continue;
}
HashMap<Integer, Integer> hashMap = getHashMapFromArray(arrayList);
Integer[] array = hashMap.values().toArray(new Integer[hashMap.values().size()]);
if(!isFrequencyOfAllIngredientsNonRepeating(array)) {
System.out.println("NO");
continue;
}
if(!areAllIngredientsNotRepeatingAfterTheirChainEnds(arrayList)) {
System.out.println("NO");
continue;
}
System.out.println("YES");
}
}
public static HashMap<Integer, Integer> getHashMapFromArray(Integer[] arrayList){
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
for(Integer value : arrayList){
if(!hashMap.containsKey(value)){
hashMap.put(value,1);
}else{
hashMap.put(value,hashMap.get(value) + 1);
}
}
return hashMap;
}
public static boolean isFrequencyOfAllIngredientsNonRepeating(Integer[] array) {
HashMap<Integer,Integer> hashMap = new HashMap<Integer,Integer>();
for(Integer x : array) {
if(!hashMap.containsKey(x)) {
hashMap.put(x, 1);
}else{
if(hashMap.get(x) >= 1) {
return false;
}
}
}
return true;
}
public static boolean areAllIngredientsNotRepeatingAfterTheirChainEnds(Integer[] arrayList) {
HashMap<Integer,Boolean> hashMap = new HashMap<Integer,Boolean>();
Integer value = arrayList[0];
for(Integer x : arrayList) {
if(!hashMap.containsKey(x)) {
hashMap.put(x, true);
}else {
if(value != x) {
return false;
}
}
value = x;
}
return true;
}
}
Please help me know why my solution is wrong. I’m new here.
import java.io.FileInputStream;
import java.util.*;
public class Main {
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
if(T<1 || T>100){
System.out.println(“NO”);
return;
}
int N;
Integer[] arrayList;
int A;
for(int i = 0; i < T; i++){
N = scanner.nextInt();
if(N < 1 || N > 103){
System.out.println(“NO”);
continue;
}
arrayList = new Integer[N];
boolean validIngredients = true;
for(int j = 0; j < N; j++){
A = scanner.nextInt();
if(A < 1 || A > 103){
validIngredients = false;
break;
}
arrayList[j] = A;
}
if(!validIngredients) {
System.out.println(“NO”);
continue;
}
HashMap<Integer, Integer> hashMap = getHashMapFromArray(arrayList);
Integer[] array = hashMap.values().toArray(new Integer[hashMap.values().size()]);
if(!isFrequencyOfAllIngredientsNonRepeating(array)) {
System.out.println("NO");
continue;
}
if(!areAllIngredientsNotRepeatingAfterTheirChainEnds(arrayList)) {
System.out.println("NO");
continue;
}
System.out.println("YES");
}
}
public static HashMap<Integer, Integer> getHashMapFromArray(Integer[] arrayList){
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
for(Integer value : arrayList){
if(!hashMap.containsKey(value)){
hashMap.put(value,1);
}else{
hashMap.put(value,hashMap.get(value) + 1);
}
}
return hashMap;
}
public static boolean isFrequencyOfAllIngredientsNonRepeating(Integer[] array) {
HashMap<Integer,Integer> hashMap = new HashMap<Integer,Integer>();
for(Integer x : array) {
if(!hashMap.containsKey(x)) {
hashMap.put(x, 1);
}else{
if(hashMap.get(x) >= 1) {
return false;
}
}
}
return true;
}
public static boolean areAllIngredientsNotRepeatingAfterTheirChainEnds(Integer[] arrayList) {
HashMap<Integer,Boolean> hashMap = new HashMap<Integer,Boolean>();
Integer value = arrayList[0];
for(Integer x : arrayList) {
if(!hashMap.containsKey(x)) {
hashMap.put(x, true);
}else {
if(value != x) {
return false;
}
}
value = x;
}
return true;
}
}