CHPINTU - Editorial

https://www.codechef.com/viewsolution/30385080
can anybody tell what is the error in it?

what’s wrong with my solution. I just calculate the total cost of baskets present for the fruit types and the print minimum of them…

:point_right: CodeChef: Practical coding for everyone

https://www.codechef.com/viewsolution/30531860
you are accessing an out of bound element here, causing undefined behaviour.

FOR(i,0,n){  
             tp +=  vec[i].second;
             
             if(vec[i].first != vec[i+1].first){ //this may be out of bounds
                 s[j++] = tp ;
                  tp = 0;
             }
             
        }  

simply pushing a nonsense value at the end, so that s always updates, got an AC.

2 Likes

Thanks :blush:

One more question . when i was checking some test cases on codechef IDE , Why there is no out of bound error

C++ assumes the coder is always correct. So it doesn’t bother checking. That’s why it’s a lot faster.if you want a runtime error, refer to :
It shows how to enable debug mode.

1 Like

Okay…

“Hidden Traps”

By count, I meant the no. of baskets of that particular fruit. There can be a test case where the no. of baskets for a particular fruit may be > 0 but the total price is 0.
1
6 4
1 2 3 4 1 2
0 2 3 3 0 1
your code here is giving 3 as an answer but the actual answer is 0. As there are 2 baskets with fruit 1 and cost 0.

2 Likes

Please tell me why this code is giving wrong answer?

#include
using namespace std;

int main() {
int t,n,m,i,j,min;
cin>>t;
while(t–)
{
cin>>n>>m;
int f[50],p[50],q[50]={0};
int ans=0;
for(i=0;i<n;i++)
{
cin>>f[i];
}
for(i=0;i<n;i++)
{
cin>>p[i];
}
for(j=1;j<=m;j++)
{
for(i=0;i<n;i++)
{
if(f[i]==j)
{
q[j]=q[j]+p[i];
}
}
}

   for(i=1;i<=m;i++)
   {
       if(q[i]!=0)
        {
            min=q[i];
            break;
        }
    }
    
    for(i=1;i<=m;i++)
    {
       if(q[i]<min && q[i]!=0)
        {
           min=q[i];
        }   
   }
   for(i=1;i<=m;i++)
   {
       if(q[i]==min)
       {
           ans=ans+min;
       }
   }
   cout<<ans<<endl;    
}
return 0;

}

Thanks bro…

#include
using namespace std;

int main()
{
int t;
cin>>t;
for(int i=0; i<t; i++)
{
int n,m;
int arr[n], rate[n], final[n];
int temp;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
for(int j=0; j<n;j++)
{
cin>>rate[j];
}

	for(int k=0; k<n; k++)
	{
		
		final[k]+=rate[k];
		for(int r=k+1; r<=n; r++)
		{
			if(arr[k]==arr[r])
			{
				final[k]+=rate[r];
				rate[r]=0;

			}
			
		}
		if(temp>final[k] && final[k]!=0)
	        {
			     temp=final[k];
		    }
		
		
	}
	cout<<temp<<endl;
	

	
}

}

can anyone pls tell me the error, i was getting sigmentation fault while sublitting
anyone please

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        int f[n], p[n];
        for(int i=0;i<n;i++)
            scanf("%d", &f[i]);
        for(int i=0;i<n;i++)
            scanf("%d", &p[i]);
    
    //to check the count
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(f[i]==f[j])
            {
                p[i]=p[i]+p[j];
                p[j]=-2;
            }
        }
    }
    
    int min=p[0];
    for(int i=1;i<n;i++)
    {
        if(min>p[i])
        {
            if(p[i]==0)
                continue;
            else
                min=p[i];
        }
    }
    printf("%d\n", min);
}
	return 0;
}

My code says WA. I am damn sure the problem is with nested ‘for’ loops. Please someone help me!
Keeping the code same and replacing the nested for loops and using same array to sum up elements the code worked perfect!
What may be the issue? I am stuck on this for 3 days. Please someone help.

def fruits(dict1):
arr=[]
for i in range(2):
arr.append(list(map(int,input().split())))
for j in range(n):
dict1[arr[0][j]]+=arr[1][j]
x=min(t for t in dict1.values() if t>0)
return(x)

t=int(input())
for _ in range(t):
n,m=map(int,input().split())
dict1={x+1:0 for x in range(m)}
print(fruits(dict1))

why its not working
indentations are taken

/please find the error in my code i have also taken care of the hidden trap/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–){
int n,m;
cin>>n>>m;
int f[n],p[n],h[51]={0},i,temp=INT_MAX;
for(i=0;i<n;i++)
cin>>f[i];

for(i=0;i<n;i++){
cin>>p[i];
h[f[i]]+=p[i];
}
for(i=0;i<n;i++)
if(temp>h[f[i]])
temp=h[f[i]];

cout<<temp<<endl;
}
return 0;
}

what is wrong in this------------

tc = int(input())
while tc:
n, m = list(map(int, input().split()))
fruits = list(map(int, input().split()))
prices = list(map(int, input().split()))

presence_array = [0]*m

for i in range(1, m+1):
    if i in fruits:
        presence_array[i-1] = True
    else:
        presence_array[i-1] = False

c = presence_array.count(True)
new_array = [0]*c

for i in range(len(fruits)):
    new_array[fruits[i]-1] += prices[i]

print(min(new_array))
tc -= 1

Can someone please help me with my code?
https://www.codechef.com/viewsolution/30325072

in your nested loop make the following changes to your inner loop:
for (int j=0;j<n;j++)
{
if(f[i]==f[j])
{
p[i]=p[i]+p[j];// i also recommend to use a new array instead of using p[i]
}
}

also when finding the minimum element in the price array don’t skip over zero as a type of fruit may also have zero price (see the constraints carefully)

Please, can you help me, I am getting zero- socre , but i am getting the correct output.
PLEASE HELP .

t=int(input())
n_barrrier,n_type=map(int,input().split())
lis1=list(map(int,input().split()))
lis2=list(map(int,input().split()))
lis3=[]
for i in range(t):
for i in range(1,n_type+1):

sum=0

    c=False
    for j in range(n_barrrier):
        if i==lis1[j]:
            sum+=lis2[j]
            lis1[j]=0
            c=True
    if c==True:
        lis3.append(sum)
print(min(lis3))

why do i get SIGSEV here in this code and how to deal with this I am new to this platform.
int main() {
int t;
cin>>t;

while(t!=0){
    int n,m;
    cin>>n>>m;
    int f[n],p[n];
    
    for(int i=0;i<n;i++)
        cin>>f[i];
    for(int i=0;i<n;i++)
        cin>>p[i];
    
    int freq[m+1];
    bool avail[m+1];
    memset(avail,false,sizeof(avail));
    memset(freq,-1,sizeof(freq));
    
    for(int i=0;i<n;i++){
        freq[f[i]]+=p[i];
        avail[f[i]]=true;
        
    }
    int min = INT_MAX;
    for(int i=0;i<=m;i++){
        if(freq[i]<min && avail[i]==true){
            min=freq[i];
        }
    }
    cout<<min<<"\n";
    t--;
}
return 0;

}