Skip to content

S1Q1 · Sum of Squares and Absolute Difference of Squares

⚡ Quick Reference

Function: sum_squares_abs_diff_squares(a: int, b: int) -> tuple

Core idea: compute and , return both their sum and their absolute difference.

def sum_squares_abs_diff_squares(a: int, b: int) -> tuple:
    return (a**2 + b**2, abs(a**2 - b**2))

Key rules: - First element: a² + b² - Second element: |a² - b²| - abs() handles the case where b² > a²


Problem Statement

Problem

Write a function sum_squares_abs_diff_squares(a, b) that returns a tuple of: 1. The sum of the squares: a² + b² 2. The absolute difference of the squares: |a² - b²|

Example:

Input
a=3, b=4
Output
(25, 7)

Verification: 3²=9, 4²=16, sum=9+16=25, abs diff=|9-16|=7


Solution approaches

def sum_squares_abs_diff_squares(a: int, b: int) -> tuple:
    return (a**2 + b**2, abs(a**2 - b**2))
def sum_squares_abs_diff_squares(a: int, b: int) -> tuple:
    sq_a = a ** 2
    sq_b = b ** 2
    sum_sq  = sq_a + sq_b
    diff_sq = abs(sq_a - sq_b)
    return (sum_sq, diff_sq)
def sum_squares_abs_diff_squares(a: int, b: int) -> tuple:
    sq_a, sq_b = pow(a, 2), pow(b, 2)
    return (sq_a + sq_b, abs(sq_a - sq_b))

pow(a, 2) is equivalent to a**2. Compute each square once and reuse.

sum_squares_abs_diff_squares = lambda a, b: (a**2 + b**2, abs(a**2 - b**2))

Key takeaways

01

abs() for absolute difference

abs(a² - b²) handles both cases: when a² > b² and when b² > a². Never write if a**2 > b**2: ... else: ... when abs() does it in one call.

02

Compute squares once, reuse

If using the explanatory approach, store sq_a and sq_b - avoids computing a**2 twice. For two values it's minor, but it's a good habit.

03

Return a tuple, not a list

The problem specifies a tuple return type. (x, y) creates a tuple; [x, y] creates a list. They're different types - check the expected return carefully.