Header Ads Widget

Chef and Replication | codechef solution

 Problem:-

Chef Avi likes the number . Given a list of  integers, help him find the sum of all distinct numbers repeating exactly  times. Each of those numbers should be considered only once in the sum, not  times.

###Input:

  • First line will contain , number of testcases. Then the testcases follow.
  • Each testcase contains two lines:
  • The first line contains two space-separated integers ,.
  • The second line contains  space-separated integers. ###Output: For each testcase, print the sum of all numbers occurring exactly  times. If there is no such number, print -1 instead.

###Constraints

  • 110
  • 110,000
  • 1
  • 0109

Sample 1:

Input
Output
3
4 1
2 3 4 4
5 3
9 9 1 1 9
2 1
2 2
5
9
-1

Explanation:

Test Case 1: The numbers 2 and 3 occur exactly once. So the answer is 2 + 3 = 5.

Test Case 2: The number 9 occurs exactly three times. So the answer is 9.

Test Case 3: There is no number which occurs exactly once. Hence the answer is -1.

Code(C++):-

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--){
     ll n,k;
     cin>>n>>k;
     ll arr[n];
     map<ll,ll>m;
     for(int i=0; i<n; i++){
     cin>>arr[i];
     m[arr[i]]++;
     }
     ll ans=0;
     int j=0;
     for(auto x:m){
     if(x.second==k){ans+=x.first;
     j++;
     }
     }
     if(j==0)cout<<-1<<endl;
     else cout<<ans<<endl;
    }
    return 0;
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-



Post a Comment

0 Comments