My issue
1
5
5 4 3 2 1
this should give answer as 2 and not 4(as given by a correctly submitted answer)
for this test case the 1st and 2nd will give 4 and 2 candies to 4th and 5th respectively, so only 4th and 5th are ‘getting’ candies, so 2.
the problem says in order to give candy, j>i and this does not mean that they can give only to adjacent people, it means they can give to any one sitting on their right and the one on right should have lesser than former.
My code
#include <stdio.h>
void sortArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int N, count=0;
scanf("%d",&N);
int A[N], B[N];
for (int i=0; i<N; i++)
{
scanf("%d",&A[i]);
B[i]=A[i];
}
sortArray(A, N);
for (int i=0; i<N; i++)
{
if (B[i] < A[i])
count++;
}
printf("%d\n",count);
}
return 0;
}
Problem Link: Christmas Candy Practice Coding Problem - CodeChef
@thepritamshaw
plzz refer the following c code
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int t;
scanf("%lld",&t);
while(t--)
{
long long int n;
scanf("%lld",&n);
long long int arr[n];
long long int t[n];
for(int i=0;i<n;i++)
{
scanf("%lld",&arr[i]);
t[i]=0;
}
int max=arr[0];
int count=0;
for(int j=1;j<n;j++)
{
if(arr[j]<max)
{
count++;
}
else
max=arr[j];
}
// for(int i=0;i<n;i++)
// {
// if(t[i]!=0)
// count++;
// }
printf("%d\n",count);
}
}
@dpcoder_007
I want to point the logical fault in the question (or maybe I’m missing something logically),
Your code will be accepted, but for the test case
1
5
5 4 3 2 1
The answer according to u and codechef for this testcase is 4, but according to me it’s 2, as 1st guy will give 4 candies to 5th and 2nd will give 2 to 4th, so only 2 friends will eventually be recieving the candies, and in the question it’s not mentioned that they only can transfer to adjacent right friend, in the question a guy can transfer if j>i, i.e anyone on the right
you may have read question wrongly or missed logic somewhere. The question is there are some friends standing in a row, if their index number is less than other and their value is greater than his value then he will give a chocolate. we have to count number of people get atleast one chocolate
here in 5 4 3 2 1. 0th element is 5 and it is greater than all the other greater than it so he give one chocolate to 4 people. similarly 4(index=1) give chocolate to 3 people and so on. so except 5 every one have chocolate so output is 4
1 Like
@rohith_77
I revisited the qn, and it says a friend can give ‘one’ candy and not multiple
I missed the objective of the qn
Thankyou for the explanation.