/* Groot has N trees lined up in front of him where the height of the i'th tree is denoted by H[i]. He wants to select some trees to replace his broken branches.
But he wants uniformity in his selection of trees. So he picks only those trees whose heights have frequency B. He then sums up the heights that occur B times. 
(He adds the height only once to the sum and not B times).But the sum he ended up getting was huge so he prints it modulo 10^9+7.
    In case no such cluster exists, Groot becomes sad and prints -1.

Constraints:
   1<=N<=100000
   1<=B<=N
   0<=H[i]<=10^9                */


import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    
    public static void getsum(int N , int B , int C[]){
        int totalsum = 0; 
        boolean clus_e = false ;   //Cluster exists varaible 

        //Step 1 : Iterate through height 
        for(int i=0 ; i < N ; i++){
            int currht = C[i];  // Current height
            int htcnt = 0 ;   // Height count 

            //Count occurrene of current height 
            for(int j=0 ; j< N ; j++){
                if(C[j] == currht){
                    htcnt++;
                }
            }

            //Step 2 : Check for B occurance and calc sum 

            if( htcnt == B){
                boolean isDistinct = true;

                //Check if height is DIstinct
                for(int k=0 ; k<i; k++){
                    if(C[k] == currht){
                        isDistinct = false;
                        break;
                    }
                }
                if(isDistinct){
                    totalsum = (totalsum + currht) % (1000000007);
                    clus_e = true ;
                }                  
            }
        }    

        //Step 3 : Print the Result
        if(clus_e ){
            System.out.println(totalsum);
        }
        else{
            System.out.println(-1);
        }
    }
    
    public static void main(String[] args) {
        int N = 5;
        int B = 2 ; 
        int C[] = {1,2,2,3,3};
        getsum(N,B,C);
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: