I am solving a problem called Sticks in C. Here is the link https://www.codechef.com/problems/STICKS
. I am unable to determine what went wrong. I even included the SQUARE TEST CASE. Here is my code, Any help would be appreciated:
#include<stdio.h>
#include<math.h>
// using quicksort to sort the elements
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high -1; j++)
{
if(arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}
void quicksort(int arr[], int low, int high)
{
if(low < high)
{
int pi = partition(arr, low, high);
quicksort(arr, low, pi -1);
quicksort(arr, pi + 1, high);
}
}
int main()
{
int test_case, j;
scanf("%d", &test_case);
for(j = 1; j<=test_case; j++)
{
int arr[1000], duparr[1000], n=0, i=0, max1 = 0, max2 = 0, count = 0; // duparr is duplicate array used for storing duplicate values
scanf("%d", &n);
for(i = 0; i < 1000; i++)
{ arr[i] = 0; duparr[i] = 0; }
for(i = 0; i < n; i++)
{ scanf(" %d", &arr[i]); }
quicksort(arr, 0, n-1);
// to check how many duplicate values are
there
for(i=n-1;i>=0;i--)
{
if(i == 0)
{ ; }
else if(arr[i]==arr[i-1])
{ count++; duparr[i] = arr[i]; }
}
// for storing the two max values
for(i=n-1;i>=0;i--)
{
if(i == 0)
{ ; }
else if(duparr[i]!=duparr[i-1] && duparr[i] > max1 && (count >= 2))
{
max1 = duparr[i];
}
else if(duparr[i]!=duparr[i-1] && duparr[i] < max1 && duparr[i] > max2 && (count >= 2))
{
max2 = arr[i];
}
}
int product = 0, product2 = 0, final_product = 0;
product = max1*max2;
product2 = sqrt(product); // to check for a square
final_product = (product2 % 2);
if(count < 2 || product2 == 0)
printf("-1\n");
else if(count >= 2)
printf("%d\n", (product));
}
return 0;
}