Loading...
Submission
# When Author Problem Language CPU Memory
20811 2024-06-03 01:35:33 AHAMMED_99 The Picky Penguins' Party C 9 ms 1500 kb Accepted
Test Cases
# CPU Memory Points
1 0 ms 1384 kb 1 Accepted
2 3 ms 1500 kb 1 Accepted
3 1 ms 1468 kb 1 Accepted
4 0 ms 1424 kb 1 Accepted
5 9 ms 1452 kb 1 Accepted
Source Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Function to check if array can be partitioned into two subsets of equal sum
  5. int canPartition(int* nums, int numsSize) {
  6. long long sum = 0;
  7. for (int i = 0; i < numsSize; i++) {
  8. sum += nums[i];
  9. }
  10.  
  11. // If sum is odd, it's not possible to partition into two equal subsets
  12. if (sum % 2 != 0) {
  13. return 0;
  14. }
  15.  
  16. int target = sum / 2;
  17. char* dp = (char*)calloc(target + 1, sizeof(char));
  18. dp[0] = 1;
  19.  
  20. for (int i = 0; i < numsSize; i++) {
  21. for (int j = target; j >= nums[i]; j--) {
  22. dp[j] = dp[j] || dp[j - nums[i]];
  23. }
  24. }
  25.  
  26. int result = dp[target];
  27. free(dp);
  28. return result;
  29. }
  30.  
  31. int main() {
  32. int T;
  33. scanf("%d", &T);
  34.  
  35. while (T--) {
  36. int N;
  37. scanf("%d", &N);
  38.  
  39. int* A = (int*)malloc(N * sizeof(int));
  40. for (int i = 0; i < N; i++) {
  41. scanf("%d", &A[i]);
  42. }
  43.  
  44. if (canPartition(A, N)) {
  45. printf("Party Time!\n");
  46. } else {
  47. printf("Party's Cancelled\n");
  48. }
  49.  
  50. free(A);
  51. }
  52.  
  53. return 0;
  54. }
  55.