My issue
in the last test case 3 values they say give candies but the output shows 1
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int count=0;
int arr[n];
for(int i=1; i<=n; i++){
cin>>arr[i];
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if((j>i) && (arr[j]<arr[i])){
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}
Problem Link: Christmas Candy Practice Coding Problem - CodeChef
first of all i guess this code will give tle if it passes test cases also as it have nested for loop so time complexity will be O(n^2) and your logic is also wrong you are checking one i=0 with every j element that is not the question like we take test case 4 3 2 1 11 10 9 8 7 6 5 in this first we take one max element that is 4 and if element is smaller than this then we done count++ in each iteration we check if there is new max element that we will found at index 4 that is 11 so we update max value as 11 and again check if element is smaller than this and update value because question is saying person with big number than other number can give candy to other person so 4 can give candy to 3 2 1 and similarly 11 can give candy to 10 9 so on till 5 but 4 can’t give candy to them
1 Like