FFL - Editorial

How is it O(n) when you have 2 for loops, one nested inside another? (Setter’s Solution)

1 Like

Thank U bro :smiley: :grinning:

1 Like

test case is passing but showing wrong answer after submission???
pls HELP…
#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
unsigned int n,s,i,k,z,q,r,mind,minf;
cin>>n;
cin>>s;
int fo[2][n];

    for(i=0;i<2;i++){
        for(k=0;k<n;k++){
            cin>>fo[i][k];}
                            }

    int fp=0,dp=0;
    for(z=0;z<n;z++){
    if(fo[1][z]==0){
        dp=dp+1;
        }
        if(fo[1][z]==1){
            fp=fp+1;  }
                          }

    int d[dp];
    int f[fp];
    q=0;r=0;

     for(z=0;z<n;z++){

            if(fo[1][z]==0){

                d[q]=fo[0][z];
                mind=d[0];
                if(mind>=d[q]){
                    mind=d[q];
                }
            q=q+1;
                           }

            if(fo[1][z]==1){
                      f[r]=fo[0][z];
                minf=f[0];
                if(minf>=f[r]){
                    minf=f[r];
                }
                   r=r+1;
            }
        }

    if(s+mind+minf<=100){
            cout<<"yes";
        }
        else
        cout<<"no";

        cout<<endl;
}
return 0;

}

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test;
cin >> test;
while (test–)
{
int n, S;
cin >> n >> S;
vector<pair<int, int>> a(n, make_pair(0, 0));
int min_def = 200, min_for = 200;
for (int i = 0; i < n; i++)
{
cin >> a[i].first;
}
for (int i = 0; i < n; i++)
{
cin >> a[i].second;
}
for (int i = 0; i < n; i++)
{
if (a[i].second == 0)
{
min_def = min(min_def, a[i].first);
}
else
{
min_for = min(min_for, a[i].first);
}
}
if (min_def + min_for + S <= 100)
{
cout << “yes” << endl;
}
else
{
cout << “no” << endl;
}
}
return 0;
}

indent preformatted text by 4 spaces

for _ in range(int(input())):
n,s = map(int,input().split())
A,B=[],[]
lis = list(map(int,input().split()))
lis1 = list(map(int,input().split()))
for i in range(n):
if(lis1[i]==0):
A.append(lis[i])
else:
B.append(lis[i])
A.sort();B.sort()
if(A[0]+B[0]<=(100-s)):
print(“yes”)
else:
print(“no”)

Why it is throwing NZEC even though it is working on sample test cases?

There maybe a case when N=1. In that case, either the player is 0 or 1. So, either the list A or B remains empty. Hence, when you are trying to access the first element of an empty list, NZEC error shows up.

https://www.codechef.com/viewsolution/32217243

Hope this helps.

1 Like

Thanks

[Solution] (CodeChef: Practical coding for everyone)
whats wrong ??

#include <bits/stdc++.h>
using namespace std;
int main() {

int t;
cin >> t;
while(t--) {
    int n, s;
    map<int, int> info;
    cin >> n >> s;
    int price[n], player[n];
    int zeroDone = 0, oneDone = 0, sum = 0, found = 0;
    for ( int i = 0; i < n; i++) {
        cin >> price[i];
        
    }
    for ( int i = 0; i < n; i++) {
        cin >> player[i];
    }

    for (int i = 0; i < n; i++){
        info[price[i]] = player[i];
    }

    for (auto i : info) {
        
        if(i.second == 0 && zeroDone < 1) {
            zeroDone = 1;
            sum = sum + i.first;
        } else if (i.second == 1 && oneDone < 1) {
            oneDone = 1;
            sum = sum + i.first;
        } 
        if(zeroDone == 1 && oneDone == 1) {
            found = 1;
            break;
        }

    }
    
    if(sum <= 100-s && found == 1) {
        cout << "yes" << '\n';
    } else {
        cout << "no" << '\n';
    }

}

}

Whats wrong ? PLZZ HELP

in the bootomost for loop it should if(min2>g[i]) instead of d[i] ,also review you input i think something is wrong with it

1 Like

This was my solution, it worked perfectly fine in my editor but was showing runtime error (SIGSEGV). I tried my best to find out what is wrong in my code but failed. Please if anyone can figure out what is the issue.

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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, s, input;
        cin >> n >> s;

        vector<int> p;
        vector<int> c;
        vector<int> d;
        vector<int> f;

        for (int i = 0; i < n; i++)
        {
            cin >> input;
            p.push_back(input);
        }
        for (int i = 0; i < n; i++)
        {
            cin >> input;
            c.push_back(input);
        }
        for (int i = 0; i < n; i++)
        {
            if (c[i] == 0)
                d.push_back(p[i]);
            else
                f.push_back(p[i]);
        }

        sort(d.begin(), d.end());
        sort(f.begin(), f.end());

        if ((d.front() + f.front() + s) > 100)
            cout << "no\n";
        else
            cout << "yes\n";
    }
}

i was replying to somebody else , in your case i think map stores sorted and unique key value pairs but in the problem there are same values (you can see in the test case only), so try using some other technique to find minimum cost of defender and forward ans then add them to check wether the resulting sum is less than 100-s or not

can anybody help me in finding my mistake,i unable to understand why it is not passing all the test case and giving wa.

#include <stdio.h>

typedef struct PLAYERS
{
int price;
int type;
}players;

int main(void) {
int T;
scanf("%d",&T);
while(T)
{
int N,S;
scanf("%d %d",&N,&S);
players P[N];
for(int i = 0;i < N;i++)
{
scanf("%d",&P[i].price);
printf(“j0”);
}
for(int j = 0;j < N;j++)
{
scanf("%d",&P[j].type);
printf(“j1”);
}

    int min0 = 101;
    int min1 = 101;
    
    for(int i = 0; i < N;i++)
    {
        if(P[i].type == 0)
        {
            if(P[i].price < min0)
            {
                min0 = P[i].price;
            }
        }
        else
        {
            if(P[i].price < min1)
            {
                min1 = P[i].price;
            }
        }
    }
    
    if(min0 + min1 + S <= 100)
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
    T--;
}
return 0;

}

#include
using namespace std;

int main() {
// your code goes here
int t=0;
cin>>t;
while(t–>0)
{
int size=0;
cin>>size;
int s=0;
cin>>s;
int req= 100-s;
int a1[size+1];
int a2[size+1];
for(int i=1;i<=size;i++)
{
cin>>a1[i];

	    }
	     for(int j=1;j<=size;j++)
	    {
	        cin>>a2[j];
	    }
	    int m1 = 2147483647;
	    int m2 = 2147483647;
	    
	    for(int jk=1;jk<=size;jk++)
	    {
	        if(m1>a1[jk] && a2[jk]==0)
	        {
	            m1=a1[jk];
	        }
	        if(m2>a1[jk] && a2[jk]==1)
	        {
	            m2=a1[jk];
	        }
	    }
	    if(m1+m2<req)
	    {
	        cout<<"yes"<<endl;
	    }
	    else
	    {
	        cout<<"no"<<endl;
	    }
	    
	}
return 0;

}

Can anyone check what is wrong here

bhai thik kaise hoga ye batao
min0 and min1 ki kya value lein?

#include
#include <bits/stdc++.h>
using namespace std;
int main() {
int tcas;
scanf("%d",&tcas);
while(tcas–)
{
int numbe,copri,i;
scanf("%d %d",&numbe,&copri);
int numbearr[numbe],copriarr[numbe],onearr[numbe],zeroarr[numbe];
for(i=0;i<numbe;i++)
scanf("%d",&numbearr[i]);
for(i=0;i<numbe;i++)
scanf("%d",&copriarr[i]);
for(i=0;i<numbe;i++)
{
onearr[i]=INT_MAX;
zeroarr[i]=INT_MAX;
}
for(i=0;i<numbe;i++)
{
if(copriarr[i]==1)
onearr[i]=numbearr[i];
else
zeroarr[i]=numbearr[i];
}
sort(onearr,onearr+numbe);
sort(zeroarr,zeroarr+numbe);
copri+=onearr[0]+zeroarr[0];
if(copri<=100)
printf(“no\n”);
else
printf(“yes\n”);
}
return 0;

Why is this a wrong answer?

It happened with me too. You didn’t think about the case where Defender is 0 or forward is 0…

if you check for d.size==0 or f.size==0 it may help

Still not working man … Please try to find … What’s error … I am blind now

#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll n,sum=101,a,b,s,k=101;

vectorcost(n);

vectorpos(n);

void go(){

  if(cost.size()==0 || pos.size()==0 ){

   cout<<"no"<<endl;

   exit(1);

  }

 for(ll i=0;i<n;i++){

      if(pos[i])

      k=min(k,cost[i]);

      else

      sum=min(sum,cost[i]);

  }

if(sum+k<=100-s)

  cout<<"yes"<<endl;

  else cout<<"no"<<endl ;    

}

int main(){

void go();

int tc=0;

cin>>tc;

while(tc–){

  cin>>n>>s;

  for(ll i=0;i<n;i++){

    cin>>a;

    cost.push_back(a);

  }

 for(int i=0;i<n;i++){

    cin>>b;

    pos.push_back(b);

  } 

  go();

       } 

return 0;

}
// what is the problem here why it’s giving wrong answer??..please assist for this code…

Thanks, It’s working now.