1 / 3
May 2020

I have tried a number of test cases, i don’t know why i am getting wrong answer for my submission. Please help me out with this problem.
Here is my solution:
#include
#include

using namespace std;

int main(){
int t = 0, n = 0, c = 0;
unsigned int *a = new unsigned int[n];
cin >> t;
while(t–){

    cin >> n >> c;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    sort(a, a + n);
    
    unsigned int l = 0, h = a[n - 1], ans = 0;
    while(l <= h){
        unsigned int m = l + (h - l)/2;
        int d = 1;
        unsigned int pos = a[0];
        for(int i = 0; i < n - 1; i++){
            if(a[i + 1] - pos >= m){
                pos = a[i + 1];
                d++;
            }
            if(d == c){
                break;
            }
        }
        if(d < c)
            h = m - 1;
        else{
            ans = m;
            l = m + 1;
        }
    }
    cout << ans << "\n";
}

return 0;

}

  • created

    May '20
  • last reply

    May '20
  • 2

    replies

  • 557

    views

  • 2

    users

  • 1

    like

Something looks fishy here.

damn! i had done a blunder :man_facepalming: (i need to be careful) ! thankyou so much simes for helping me out :pray::+1: