Charsi Game

QUESTION

Its been a few days since Charsi is acting weird. And finally you(his best friend) came to know that its because his proposal has been rejected.\n\nHe is trying hard to solve this problem but because of the rejection thing he can’t really focus. Can you help him? The question is: Given a number n , find if n can be represented as the sum of 2 desperate numbers (not necessarily different) , where desperate numbers are those which can be written in the form of (a*(a+1))/2 where a > 0 .\n\nInput :\n\nThe first input line contains an integer n (1<=n<=10^9).\n\nOutput :\n\nPrint \”YES\” (without the quotes), if n can be represented as a sum of two desperate numbers, otherwise print \”NO\” (without the quotes).

“TESTCASE_1”: “256\n###—###SEPERATOR—###—\nYES”, “TESTCASE_2”: “512\n###—###SEPERATOR—###—\nNO”, “TESTCASE_3”: “828\n###—###SEPERATOR—###—\nYES”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
int main()
{
    int n,i,j,flag=0;
    scanf("%d",&n);
    long int a[100005];
    a[0]=0;
    for(i=1;i<100005;i++)
     a[i]=a[i-1]+i;
  //  for(i=0;i<25;i++)
    // printf("%ld ",a[i]);
     //printf("%ld ",a[100000]);
    for(i=1,j=100000;i<j;)
    {
        if(a[i]+a[j]>n)
         j--;
        else if(a[i]+a[j]<n)
         i++;
        else
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
     printf("YES");
    else
     printf("NO");
  return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro