easy
0 views

Function Subtraction of Multiplicative Product – CTS PATTERN

Given three integers, find the absolute difference between the second maximum value and the product of the maximum and minimum values

Understand the Problem

Problem Statement

You are given three integers a, b, and c. Your task is to:

  • Find the maximum, minimum, and second maximum (middle) values among the three numbers
  • Calculate the multiplicative product of the maximum and minimum values
  • Compute the absolute difference between the second maximum value and this product

The function should return this computed value.

Constraints

  • All input values are integers
  • The function must handle both positive and negative integers
  • The result should always be non-negative (absolute value)

Examples

Example 1
Input
a = 5, b = 2, c = 8
Output
2
Explanation

Maximum = 8, Minimum = 2, Middle = 5. Product = 8 × 2 = 16. Result = |5 - 16| = 11. Wait, let me recalculate: Middle = 5, Product = 16, |5 - 16| = 11. Actually, checking the formula again: result = |mid - (max × min)| = |5 - (8 × 2)| = |5 - 16| = 11.

Example 2
Input
a = 1, b = 1, c = 1
Output
0
Explanation

All values are equal: max = 1, min = 1, mid = 1. Product = 1 × 1 = 1. Result = |1 - 1| = 0.

Example 3
Input
a = -2, b = 3, c = 1
Output
7
Explanation

Maximum = 3, Minimum = -2, Middle = 1. Product = 3 × (-2) = -6. Result = |1 - (-6)| = |1 + 6| = 7.

Solution

#include <stdio.h>
#include <math.h>

int printValue(int a, int b, int c)
{
    int result, min, mid, max;
    // Find maximum value using conditional operators
    max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
    // Find minimum value using conditional operators
    min = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
    // Find middle value by subtracting max and min from total sum
    mid = (a + b + c) - (min + max);
    // Calculate absolute difference between middle and product of max and min
    result = abs(mid - (max * min));
    return result;
}

// Test function to verify the implementation
typedef struct {
    int a, b, c, expected;
} TestCase;

int main() {
    TestCase tests[] = {
        {5, 2, 8, 11},
        {1, 1, 1, 0},
        {-2, 3, 1, 7},
        {10, 5, 3, 25}
    };
    int num_tests = sizeof(tests) / sizeof(tests[0]);
    
    for (int i = 0; i < num_tests; i++) {
        int result = printValue(tests[i].a, tests[i].b, tests[i].c);
        printf("Test %d: a=%d, b=%d, c=%d -> result=%d (expected=%d) %s\n",
               i+1, tests[i].a, tests[i].b, tests[i].c, result, tests[i].expected,
               result == tests[i].expected ? "✓" : "✗");
    }
    return 0;
}
Time:O(1)
Space:O(1)
Approach:

The C solution uses conditional operators to efficiently find the maximum and minimum values without sorting. The middle value is calculated by subtracting the sum of max and min from the total sum of all three numbers.

Key points:

  • Max calculation: Uses nested ternary operators to compare all three values
  • Min calculation: Similar approach as max but with less-than operators
  • Middle value: Since max + mid + min = a + b + c, we get mid = (a + b + c) - (min + max)
  • Result: Uses abs() to ensure non-negative result

The solution is efficient with O(1) time complexity and O(1) space complexity.

Visual Explanation

Loading diagram...