Question Description: Sakthi has been acting strangely for a few days now. Finally, you (his best friend) found out that it was because his project proposal was turned down (rejected). He is working hard to solve the problem, but he is unable to concentrate due to the rejection. Are you able to assist him? Find if n can be expressed as the sum of two desperate numbers (not necessarily dissimilar) given a number n. where desperate numbers are those which can be written in the form of (a*(a+1))/2 where a > 0 . Constraints: (1 ≤ n ≤ 10^9) Input : The first input line contains an integer n
C Program

#include<stdio.h>

long long t[50000];
int sz=0;

// mandatory
int binarySearch(int low,int high,int key){
    while(low<=high){
        int mid=(low+high)/2;
        if(t[mid]==key) return 1;
        else if(t[mid]<key) low=mid+1;
        else high=mid-1;
    }
    return 0;
}

int main(){
    long long n;
    scanf("%lld",&n);

    // generate triangular numbers
    for(long long i=1;;i++){
        long long val=i*(i+1)/2;
        if(val>n) break;
        t[sz++]=val;
    }

    // check pairs
    for(int i=0;i<sz;i++){
        long long rem=n - t[i];
        if(rem<0) break;

        if(binarySearch(0,sz-1,rem)){
            printf("YES");
            return 0;
        }
    }

    printf("NO");
    return 0;
}