Contest 2 - Hints to Problems [OFFICIAL]

In the problem - CHEF AND THE STREET FOOD, p/s people will buy food from chef only if p is not divisible by s
But in the example case 1, s = 2 and p= 6 then how chef gets profit of 12 ?

Because you are taking input of s, v, p you should take input of s, p, v. Hope this helps.

2 Likes

Whats wrong in this ???
Sum - matched Brackets

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define vsort sort(v.begin(),v.end())
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

int main()
{
fastio

#ifndef ONLINE_JUDGE
     freopen("input.txt","r",stdin);
     freopen("output.txt","w",stdout);
#endif

int n;
cin >> n;

std::vector<int> v;
v.pb(0);

for (int i = 0; i < n; ++i)
{
	int num;
	cin >> num;
	v.pb(num);
}

stack<int> s;
ll depth = 0, max_depth = INT_MIN, final_index1 = 0;

for(int i=1; i<=n; i++)
{
	if(v[i] == 1)
	{
		s.push(v[i]);
		depth++;
	}
	else
	{
		s.pop();

		if(max_depth < depth)
		{
		    max_depth = depth;
			final_index1 = i-1;
	    }

	    depth = 0;	
    }
}

// ((()))

ll len = 0, max_len = INT_MIN, index2 = 0, final_index2 = 0, flag = 0;

for(int i=1; i<=n; i++)
{
	if(v[i] == 1)
	{
		s.push(v[i]);
		len++;
		if(flag == 0)
		{
			index2 = i;
			flag = 1;
		}
	}
	else
	{
		s.pop();
		len++;
		if(s.empty())
		{
			if(max_len < len)
			{
				max_len = len;
				final_index2 = index2;
			}
			len = 0;
			flag = 0;
		}
	}
}

cout << max_depth << " " << final_index1 << " " << max_len << " " << final_index2 << endl;

return 0;

}

Can anyone please help in finding error within my code. it is giving wa on final submission. The question is of compilers and parsers.

I have tried several times but not able to figure out my mistake. thank you for your time.

problem statement: CodeChef: Practical coding for everyone
my solution: CodeChef: Practical coding for everyone

Can anyone tell me what is the reason for wrong answer in my code for matched brackets

#include
#include<bits/stdc++.h>
#include
#include<string.h>
#include
#include

using namespace std;

int main()
{
long long n,j,i,d1=0,d2=0,maximum=0,index;
cin>>n;
long long a[n];
for(i=0;i<n;i++){cin>>a[i];}

i=0;

while(i<n)
{
if(a[i]==1)
{
while(a[i]==1){d1++;i++;}
while(a[i]==2){d2++;i++;}
if(min(d1,d2)>maximum){maximum=min(d1,d2);
index=i-min(d1,d2);
}
d1=0,d2=0;
continue;
}
i++;
}

long long i1,d3=0,d4=0,maxi=0,index1;
for(i1=0;i1<n;i1++)
{
if(a[i1]==1){d3++;}
else if(a[i1]==2){d4++;}
if(d3==d4){
if(d3>maxi){maxi=d3;index1=i1+1;}
d3=0,d4=0;
}
}

cout<<maximum<<" “<<index<<” "<<maxi2<<" "<<index1-2maxi+1;

}

https://www.codechef.com/viewsolution/38815259
can someone help me what’s wrong with my code of infix to postfix.

Hey in Stupid Machine I used map to store the values and everytime I iterate through the map and take the smaller values .I got 50% correct .So may be I am missing some important corner case for this problem .Any one give me any idea for that.
Here is my code-

Spoiler
 cin>>n;
	M mp;
	V v(n);
	lp(i,n)cin>>v[i],mp.insert({v[i],i});
	mi=mp.begin()->first;
	p=mp.begin()->second;
	sum=mi*n;
	mp.erase(mi);
	for(auto i:mp)
	{
		if(i.second<p)
		{
			sum+=(i.first-mi)*(i.second+1);
			p=i.second;
			mi=i.first;
		}
	}
	cout<<sum<<endl;

hey @yawner, thanks man, sorry, logged in here after a long time, anyway…thanks for correcting me. cheers !

Hey guys, I might be really late here but, could anybody please help me as why I am getting WA for this solution to the question CHFQUEUE. Thanks in advance

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

int main() {
int chefs, desigNo;
cin>>chefs>>desigNo;
int chefQ[chefs];
for (int i=0;i<chefs;i++)
cin>>chefQ[i];
stack <pair<int, int>> x;
vector pos;
pos.push_back(-1);
x.push(make_pair(chefQ[chefs-1], chefs));
for(int i=chefs-2;i>=0;i–) {
while(!x.empty() && chefQ[i] <= x.top().first)
x.pop();
if(x.empty())
pos.push_back(-1);
else
pos.push_back(x.top().second);
x.push(make_pair(chefQ[i], i+1));
}
reverse(pos.begin(), pos.end());
// for(int i=0;i<pos.size();i++)
// cout<<pos[i]<<" “;
// cout<<”\n";
int ffness = 1;
for (int i=0;i<pos.size();i++) {
if (pos[i] != -1)
ffness *= (pos[i] - (i+1) + 1);
}
cout<<ffness%1000000007<<"\n";
return 0;
}

Please help i cant understand what is wrong with my logic.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;

for(int i=0;i<t;i++)
{
	string str;
	cin >> str;
	
	cin.ignore();
	
	long long int n = str.length(), length = 0, max = 0;
	
	stack <char> brackets;
	if(str[0] != '>'){
	for(long long int i=0;i<n;i++)
	{
		if(str[i] == '<')
		{
			brackets.push(str[i]);
			length += 1;
		}
		else if(str[i] == '>' && !brackets.empty())	{
		brackets.pop();
		length += 1;
		if(brackets.empty())
		{
			if(max < length)
			{
				max = length;
				length = 0;
			}
		}
		}
		else continue;
	}
	cout << max << "\n";	
	}
	else cout << "0" << "\n";
}
return 0;

}

I can’t figure out where i am going wrong in compiler and parsers problem. I tried a lot of cases all are working fine but it still is giving wrong answer on submission. Can anyone help me out?
https://www.codechef.com/LRNDSA02/submit/COMPILER

Please someone tell whats wrong in my code… WORMHOLES
Below code is giving incorrect answer…

#include<iostream>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    using namespace std;

int main() {
	int c,v,w;
	cin>>c>>v>>w;
	int d=c;
	vector<pair<int,int>> vec;
	while(c--){
	    int x,y;
	    cin>>x>>y;
	    vec.push_back({x,y});
	}
	vector<int> vArr;
	int j=0;
	while(j<v){
	    int i;
	    cin>>i;
	    vArr.push_back(i);
	    j++;
	}
	vector<int> wArr;
	j=0;
	while(j<w){
	    int i;
	    cin>>i;
	    wArr.push_back(i);
	    j++;
	}
    sort(vArr.begin(),vArr.end());
    sort(wArr.begin(),wArr.end());
	long long ans=INT_MAX;
	for(int i=0;i<d;i++){
	    long long start,endd;
	    long long it=lower_bound(vArr.begin(),vArr.end(),vec[i].first)-vArr.begin();
	    long long it1=lower_bound(wArr.begin(),wArr.end(),vec[i].second)-wArr.begin();
	    start=vArr[it];
	    endd=wArr[it1];
	    if(start<=vec[i].first && endd>=vec[i].second)
	    ans=min(ans,endd-start+1);
	}
	cout<<ans;
	return 0;
}

On replacing with the below lines of code…it is working perfectly fine…

for(int i=0;i<d;i++){
	    long long start,endd;
	    long long it=upper_bound(vArr.begin(),vArr.end(),vec[i].first)-vArr.begin();
	    long long it1=lower_bound(wArr.begin(),wArr.end(),vec[i].second)-wArr.begin();
	    start=vArr[it-1];
	    endd=wArr[it1];
	    if(start<=vec[i].first && endd>=vec[i].second)
	    ans=min(ans,endd-start+1);
	}

if…the elements in the array are distinct …isn’t the lower_bound index of a element=upper_bound index of the element -1???
Please resolve my query!

Hi, Can anyone point out what’s wrong with my code (#Stacks)
import bisect
t = int(input())
for __ in range(t):
n = int(input())
arr = [int(x) for x in input().split()]
st = []
max1 = 0

for i in range(n):
    if i==0:
        st.append(arr[i])
        continue
    if arr[i]>=st[len(st)-1]:
        st.append(arr[i])
    else:
        x = bisect.bisect_left(st,arr[i],0,len(st))
        if st[x]==arr[i] and st[x]!= st[len(st)-1]:
            st[x+1]=arr[i]
            continue
        
        st[x]= arr[i]
    #print(st)
    
print(len(st),*st)

problem solved.

import bisect
t = int(input())
for __ in range(t):
n = int(input())
arr = [int(x) for x in input().split()]
st = [arr[0]]
for i in range(1,n):

    if arr[i]>=st[-1]:
        st.append(arr[i])
        #print(2)
    else:
        x = bisect.bisect_right(st,arr[i])
        #print(1)
        if st[x]==arr[i]:
            st[x+1]=arr[i]
            #print(st)
            continue
        st[x]= arr[i]

    #print(st)
    
print(len(st),*st)

+1

Please check what is the mistake in infix to postfix problem …i will appreciate your efforts.thank yo

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

int main() {
// your code goes here
int t;
std::cin >> t;
while(t–)
{
int n;
cin>> n;
string s,ss;
std::stack a ;
cin>> s;
for(int i=0;i<n;++i)
{
if(s[i]>=‘A’ && s[i]<=‘Z’)
{
ss.push_back(s[i]);
}
else if(s[i]==’(’)
{
a.push(s[i]);
}
else if(s[i]==’)’)
{
while(a.top()!=’(’)
{
char ch=a.top();
a.pop();
ss.push_back(ch);
}
a.pop();
}
else if(s[i]==’^’)
{
while(a.empty()!=true && (a.top()==’^’))
{
char ch=a.top();
a.pop();
ss.push_back(ch);
}
a.push(s[i]);
}

        else if(s[i]=='*' || s[i]=='/')
        {
            
            a.push(s[i]);
        }
       
        else if(s[i]=='+' || s[i]=='-')
        {
            while(a.empty()!=true && (a.top()=='^' || a.top()=='/' || a.top()=='*' || a.top()=='-' || a.top()=='+'))
            {
                char ch=a.top();
                a.pop();
                ss.push_back(ch);
            }
            a.push(s[i]);
        }
        
    }
    while(a.empty()!=true)
    {
        char ch=a.top();
        a.pop();
        ss.push_back(ch);
    }
    std::cout << ss << std::endl;
    
}
return 0;

}

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

please help in chefs in queue problem i am getting tle for some datasets

Can anyone tell me on which test case it is giving wrong answer.
Even though I am getting correct answer on sample testcases.

void solve()
{
    string s;
    cin>>s;

    int open=0,close=0,count=0,ans=0;

    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='<')
        {
            open++;
            count++;
        }
        if(s[i]=='>')
        {
            open--;
            count++;
        }
        if(open<0){
            count=0;
            open=0;
            ans=max(ans,0);

        }
        if(open==0){
            ans=max(count,ans);
        }
    }
    cout<<ans<<"\n";
}

Can someone help me, why My code fails on infix to postfix.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while(t-->0){
		    int expLen = in.nextInt();
		    String expStr = in.next();
		    expStr = expStr;
		    char[] exp = expStr.toCharArray();
		    String postFix = "";
		    Stack<Character> operator = new Stack<>();
		    for(char ch:exp){
		        if(isOperand(ch)) postFix += ch;
		        else if(isOperator(ch)){
		            while(!operator.isEmpty() && precedence(ch) < precedence(operator.peek())){
		                postFix += operator.pop();
		            }
		                operator.push(ch);
		            
		        }
		        else if(ch == '(') operator.push(ch);
		        else if(ch == ')'){
		            while(operator.peek()!= '('){
		                postFix +=operator.pop();
		            }
		            operator.pop();
		        }
		    }
		    while(!operator.isEmpty()){
		        postFix +=operator.pop();
		    }
		    System.out.println(postFix);
		}
	}
	private static boolean isOperand(Character ch){
	    return (ch >= 'A') && (ch <= 'Z');
	}
	
	private static boolean isOperator(Character ch){
	    return ch == '+' || ch == '-' || ch=='*' || ch== '/' || ch== '^';
	}
	
	private static int precedence(Character ch){
	    switch(ch){
	        case '-' :
	        case '+' : return 1;
	        case '/' : 
	        case '*' : return 2;
	        case '^' : return 3;
	        default : return 0;
	        
	    }
	}
	
}

Your code fails for this test-case

1
><<>>
Expected o/p : 
0
Your o/p : 
4