BALLOON - Editorial

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

No, as it was clearly mentioned in constraints that the numbers are pairwise distinct.

1 Like

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

int main() {
int t,n,i,flag=0;
cin>>t;
while(t–){
cin>>n;
long long int a[n];
for(i=0;i<n;i++){
cin>>a[i];
if(a[i] ==1 or a[i] ==2 or a[i] ==3 or a[i] ==4 or a[i] ==5 or a[i] ==6 or a[i] ==7 )
flag++;
if(flag == 7)
{ cout<<i+1<<endl;
break ;} } }

   return 0;}  [quote="mya83, post:2, topic:91349, full:true"]

A simpler approach

#include <bits/stdc++.h>
using namespace std;
#define test_case \
  int t;          \
  cin >> t;       \
  while (t--)
void initial() {  std::ios_base::sync_with_stdio(0);  cin.tie(0); cout.tie(0); }

int main(){
  initial();
  test_case
  {
    int n, x;
    cin >> n;
    bool found = false;
    int mask = 0, rmask = (1 << 7) - 1;
    for (int i = 0; i < n; i++)
    {
      cin >> x;
      if (found)  continue;
      mask |= (1 << (x - 1));
      if ((mask & rmask) == rmask) {
        cout << i + 1 << "\n";
        found = true;
      }
    }
  }
  return 0;
}

[/quote]

where i am getting wrong ?

@strange_boy_0 I added comments.
The solution is based on setting bits (Bit manipulation) for each problem from (1-7)

1 Like

Well! I got your logic thanks!!

1 Like

it clearly written in the problem statement we have solve all question which are in between before [1,7] suppose for this test case

8 7 6 5 4 3 2 1

8 is coming before any number so we have to solve it in order move forward int the array.
so the min ques what we have to solve in order to make VIBGYOR is 8.
and this case

7 4 3 5 6 1 8 2 9

and this case as you can see 7 is coming at first then 4,3,5,6,1,8 and then 2 .
so we have to solve min of 8 ques to form VIBGYOR.

bro its provided in the question that the input array is pairwise distinct. Does that mean that there will be no duplicate elements?..i tried to process the elements while taking them as input…so if the value of count==7, i break the loop and return k, which the counter for the outer loop…my solution is getting WA…ps help

Could you please explain why am I getting time limit exceeded. So my solution right or wrong?
https://www.codechef.com/viewsolution/48008801

it is given that all numbers are pair-wise distinct.

//Solution from a beginner’s POV

#include < iostream >
using namespace std;

int main() {
int T;
cin >> T;

for(int i=0; i<T; i++){
    int n,balloon=0,problems=0;
    cin >> n;
    
    int A[n];
    for(int j=0; j<n; j++)  cin >> A[j];
 
 
    for(int k=0; k<n; k++ ){
        if(A[k]<=7) 
            balloon++;
        else if(balloon==7)
            continue;
        else 
            problems++;
        
    }	 
    
    
 
    cout << problems+balloon <<endl;
    
    
}
return 0;

}

Hey, I am using the same approach and Got the correct answer.
Here’s my solution ,hope it helps: https://www.codechef.com/viewsolution/47983624

hey! please can anyone tell me why this code is giving WA

#include
using namespace std;
int main()
{
int t;
cin >> t;
while(t–)
{
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i–)
{
if(arr[i]<=7)
{
cout << i+1;
break;
}
}
}
return 0;
}

nothing much different .If you are going with the first approach,then what you have to do is that you have to include a header file of c++i.e #include

and in the next line you have to write using namespace std;
further your code will be same .Just you have to do the following replacement
scanf ->cin>>
printf-> cout<<

Other than this your whole code will be same

one advise:-you should learn c++ language ,as it is effiecient as compared to the c language
Thanks for asking

Yeah there will be no duplicate and input all the integer first and then apply your logic.
PS- Sorry for late reply!

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

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–){
int N;
cin>>N;
unordered_map<int,int>mp;
for(int i=1;i<=7;i++)
mp[i]++;
int A[N];
int cnt=0;
int qn=7;
for(int i=0 ;i<N;i++)
{ cin>>A[i];
if(mp.find(A[i])!=mp.end())
{
cnt++;
qn=i+1;
}
}
if(cnt==7){
cout<<qn<<endl;
}
}
}