Trust Nobody - 870A Solution in cpp | Codeforces

 Question


There is a group of n people. Some of them might be liars, who always tell lies. Other people always tell the truth. The i-th person says "There are at least liliars amongst us". Determine if what people are saying is contradictory, or if it is possible. If it is possible, output the number of liars in the group. If there are multiple possible answers, output any one of them.


Example

input
Copy
7
2
1 2
2
2 2
2
0 0
1
1
1
0
5
5 5 3 3 5
6
5 3 6 6 3 5
output
Copy
1
-1
0
-1
0
3
4

Explanation

Take a test case t then take input n size of array "li" means li liars 

Then After that check if the arr[i] is less than n-1 or not if its is then count it in a sum other wise skip it

if there is same number too then also skip it. and keep count of number of times added too


Then to generate answer -1 you have to take if sum if zero and count is also zero then answer would be -1;

else if answer should be sum/count

Code

#include<bits/stdc++.h>

using namespace std;


#define test     long long T;cin>>T;while(T--)


void solve(){

    //870A div2

   int n; cin>>n; int arr[n];

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

      cin>>arr[i];

   }

   int sum=0; int flag=0;

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

      if(arr[i]<=n-1) {sum += arr[i]; flag++; }

   }

   if(flag==1) cout<<sum<<endl;

   else if(flag>1) cout<<sum/flag<<endl;

   else if(sum==0 and flag==0) cout<<-1<<endl;

}


signed main() {

    test

    //(if you want to take the more test cases you may uncomment it out)

    solve();

}



This is my logic what I have thought during contest. I cannot say that 100% it will work but by changing some logic or adding some edges case it will surely work. if you have any other solution you are welcome to post.
Previous Post Next Post