BALLOON - Editorial

In test cases such as 1,1,2,3,4,5,6,7,8 your count will be equal to 7 when your i reaches to 6 so he didn’t solve question with difficulty 7 and still your code printed the output.
The bottom line is this code won’t work if there is any duplicate.

1 Like

@entschluselt Same doubt!
Any help would really be appreciated!
Thank you.

In test cases such as 1,1,2,3,4,5,6,7,8 your count will be equal to 7 when your i reaches to 6 so he didn’t solve question with difficulty 7 and still your code printed the output.
The bottom line is this code won’t work if there is any duplicate.

That code won’t work if there is any duplicates in the input

@nishant_sagar Thank you for taking time to help.
May I know why is this code failing even though I considered the duplicate test cases and re-wrote the code once again.

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t,n,i;
cin>>t;
while(t–)
{
cin>>n;
int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,f=0;
int a[n+1];
for(i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==1 && a1==0)
{
a1++;
f++;
}
else if(a[i]==2 && a2==0)
{
a2++;
f++;
}
else if(a[i]==3 && a3==0)
{
a3++;
f++;
}
else if(a[i]==4 && a4==0)
{
a4++;
f++;
}
else if(a[i]==5 && a5==0)
{

            a5++;
            f++;
        }
        else if(a[i]==6 && a6==0)
        {
            a6++;
            f++;
        }
        else if(a[i]==7 && a7==0)
        {
            a7++;
            f++;
        }
        if(a1==1 && a2==1 && a3==1 && a4==1 && a5==1 && a6==1 && a7==1 && f==7)
        {
            cout<<i<<"\n";
            break;
        }
    }
}
return 0;

}

@ekatva your solution would have worked fine if you would have first taken all the array elements, and then would have written the rest of the logic in the next for loop. for eg., if you consider the test case :
2
9
7 4 3 5 6 1 8 2 9
7
1 2 3 4 5 6 7
then for the first test case, your code only read input till the eighth element only in the first case and then would have moved to the next test case where it would have taken 9 as the value of n and then would produced a runtime exteption

2 Likes

@hi_anusha consider the test case
2
9
7 4 3 5 6 1 8 2 9
7
1 2 3 4 5 6 7
just seperate the code for reading input of test case and the rest of the logic. you will get an AC then, and ya you don’t need to keep check for duplicates as the question clearly states that the array elements are pairwise distinct

@deonarayan_08 Thanks a lot !!!

The entries are pairwise-distinct. They cannot be identical. The only error is that she takes the input and processes it in the same loop.

2 Likes

Hi all. I couldn’t solve the question initially. This is my solution. pls have a look. I don’t know why it wouln’t work. i applied a different logic and got the code working but still if you can help me then thanks.

    #include <iostream>

using namespace std;

int main() {

int N,T;

cin>>T;


while(T!=0)
{
    int countSeven = 0;
    int count = 0;
    int minCount = 0;
    cin>>N;
    int arr[N];
    for(int i = 0; i < N;i++)
    {
        cin>>arr[i];
        
        if(arr[i] <= 7)
        {
            countSeven++;
        }
        else
        {
            count++;
        }
        
        if(countSeven == 7)
        {
            break;
        }
    }

    minCount = count + countSeven;
    cout<<minCount<<endl;
    T--;
}


return 0;

}

This solution should work right? because the elements are pairwise distinct. Honestly speaking, my definition of pairwise distinct is that there would be no repeating digits in the whole array. correct me if I am wrong. Thanks!

Ok, I understand. Thanks for the help!

2 Likes

There is a slack in the test cases because my solution which technically shouldn’t pass gave AC
https://www.codechef.com/viewsolution/47977518
This solution doesn’t work for the test case 1 2 3 4 5 6 7 1 2 3 4 5 6 7
@admin look at it thanks!

Yes,Your logic is correct I think you are going wrong because you are not taking the entire elements in the array.You are trying to break the solution as soon as you find seven elements but you must run the entire loop try to modify the loop accordingly.

1 Like

Quick Editorial:
Prequisites: Basic Hashing

My approach towards the problem was kind of mixture of editorials method 1 and method 3. I solved the problem using simple hashing.At First,I took all the elements in the array and thereafter incremented the problem id by 1.At every point I increment I made sure that I used the check function to check whether all the problems 1 to 7 were done as soon as the problems are done I marked min_idx = i + 1(since i used 0 based indexing) in case of 1 based indexing you can do the necessary changes.

Code:
https://www.codechef.com/viewsolution/47972437

Time Complexity:O(7*N) per test case
If I am wrong somewhere feel free to correct me.:slight_smile:

a simple logic

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{ ll n,x;
cin>>n;
vector v(7) ;
ll a[n];
for (int i = 1; i <=n; i++) cin>>a[i];

for (int i = 1; i <=n; i++)
{ if( a[i]==1 || a[i]==2 || a[i]==3 || a[i]==4 || a[i]==5 || a[i]==6 ||a[i]==7 )
v.push_back(i);
}
x= *max_element(v.begin(), v.end());

cout<<x<<"\n";

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin>>t;
while(t–)
{ solve();
}
return 0;
}

Also you need to decrement T otherwise while loop will never terminate.
Better use while(T–)

@hi_anusha

your code for WAVE2 matches exactly with leaked solutions .

I decremented T. last statement in while loop is T–.

Can there be more than one occurences of the same number in the entire array? like for example: 1 2 3 4 5 19 2 45 99 7 6? what will be the output of this test case? Also, can we get a list of test cases for this challenge! :slight_smile:

how to solve the following problem in c lanaguage