Solving Chefina and Swap using priority queue.
Chefina and Swaps
#include
#include
using namespace std;
long func(int n);
int main() {
int T;
int n;
cin>>T;
while (T--)
{
cin>>n;
cout <<func(n);
cout<<"\n";
}
return 0;
}
long func(int n)
{
unsigned long long sum, xsum;
unsigned long a,b,c,y;
int x;
//get sum of 1....n
sum = ((n * (n+1))/2);
if (sum%2)
return 0;
// get pivot element 1,....x, x+1....n
x = (((sqrt(1.0+(4*sum))) - 1.0)/2.0);
//get sum of 1......x
xsum = ((x * (x+1))/2);
y = n-x;
if ((2*xsum) == sum)
{
a = ((x * (x-1))/2);
b = ((y * (y-1))/2);
return (a + b + y);
}
return y;
}
Can anyone tell what is the problem with this