Menu Close

Find if n can be expressed as the sum of two desperate numbers

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
Output:
Print “YES” , if n can be represented as a sum of two desperate numbers, otherwise print “NO”.

#include <stdio.h>
int desperate(int a)
{
  return (a*(a+1))/2;
}
int main()
{
  int n, i, j, flag = 0;
  printf("Enter the number N: ");
  scanf("%d", &n);
  for(i=1;i<n;i++)
  {
    int temp=desperate(i);
    for(j=1;j<n;j++)
    {
      if(temp + desperate(j) == n)
      {
        flag = 1;
        break;
      }
    }
    if(flag==1)
    {
      printf("YES \n");
      break;
    }
  }
  if(flag==0)
      printf("NO \n");
  return 0;
}

Output_1:
Enter the number N: 100
YES

Output_2:
Enter the number N: 500
NO

Output_3:
Enter the number N: 600
YES

Output_4:
Enter the number N: 1500
YES

Output_5:
Enter the number N: 1600
YES

Output_6:
Enter the number N: 2500
YES

Output_7:
Enter the number N: 6350
NO


scrshot


Explore

1. Quantum Mechanics relation with quantum computing

2. Unzipping Files using Python

3. An Algorithm which accepts n integer values and calculates the average and prints it.

4. How can Quantum Computing merge with Virtual Reality ? !

5. Blockchain and Cryptocurrency. And their relations with Virtual Reality !!