Given an integer n representing the length of a stick, split it into four positive integer parts. Return the number of distinct ways to split the stick such that the four parts can form a rectangle but cannot form a square. Two splits are considered the same if they contain the same four side lengths, regardless of their order.
Examples:
Input: n = 10
Output: 2
Explanation: The valid splits are: {1, 1, 4, 4}, {2, 2, 3, 3}.Input: n = 20
Output: 4
Explanation: The valid splits are: {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7}, {4, 4, 6, 6}.The split {5, 5, 5, 5} forms a square, so it is not counted.
Table of Content
[Naive Approach] Iterating Over Possible Lengths - O(n) Time and O(1) Space
The idea is to observe that a rectangle must have side lengths of the form {x, x, y, y}. Hence, the total length of the stick satisfies 2x + 2y = n, or equivalently, x + y = n / 2.
If n is odd, no valid split is possible. Otherwise, iterate over all possible values of x and compute y = n / 2 - x. Count only the pairs where x < y, since x = y forms a square and x > y represents a duplicate of an already counted split.
#include <iostream>
using namespace std;
int countRectangles(int n) {
if (n % 2 != 0) return 0;
int half = n / 2;
int res = 0;
for (int x = 1; x < half - x; x++) {
// x and half - x form unequal sides.
res++;
}
return res;
}
int main() {
int n = 10;
cout << countRectangles(n) << endl;
n = 20;
cout << countRectangles(n) << endl;
}
class GFG {
static int countRectangles(int n) {
if (n % 2 != 0) return 0;
int half = n / 2;
int res = 0;
for (int x = 1; x < half - x; x++) {
// x and half - x form unequal sides.
res++;
}
return res;
}
public static void main(String[] args) {
int n = 10;
System.out.println(countRectangles(n));
n = 20;
System.out.println(countRectangles(n));
}
}
def countRectangles(n):
if n % 2 != 0:
return 0
half = n // 2
res = 0
for x in range(1, half):
# x and half - x form unequal sides.
if x < half - x:
res += 1
return res
if __name__ == "__main__":
n = 10
print(countRectangles(n))
n = 20
print(countRectangles(n))
using System;
class GFG {
static int countRectangles(int n) {
if (n % 2 != 0) return 0;
int half = n / 2;
int res = 0;
for (int x = 1; x < half - x; x++) {
// x and half - x form unequal sides.
res++;
}
return res;
}
static void Main() {
int n = 10;
Console.WriteLine(countRectangles(n));
n = 20;
Console.WriteLine(countRectangles(n));
}
}
function countRectangles(n) {
if (n % 2 !== 0) return 0;
const half = Math.floor(n / 2);
let res = 0;
for (let x = 1; x < half - x; x++) {
// x and half - x form unequal sides.
res++;
}
return res;
}
// Driver Code
let n = 10;
console.log(countRectangles(n));
n = 20;
console.log(countRectangles(n));
Output
2 4
[Expected Approach] Using Direct Formula - O(1) Time and O(1) Space
The idea is to use the fact that a rectangle must have side lengths of the form {x, x, y, y}. Thus, the total length satisfies 2x + 2y = n, or equivalently, x + y = n / 2.
If n is odd, no valid split is possible. Otherwise, the number of positive pairs (x, y) with x < y is n / 4. If n / 2 is even, one of these pairs is (n / 4, n / 4), which forms a square. Exclude this case by subtracting 1 from the count.
#include <iostream>
using namespace std;
int countRectangles(int n) {
// Rectangle cannot be formed if n is odd.
if (n % 2 != 0) {
return 0;
}
int res = n / 4;
// Exclude the square case.
if ((n / 2) % 2 == 0) {
res--;
}
return res;
}
int main() {
int n = 10;
cout << countRectangles(n) << endl;
n = 20;
cout << countRectangles(n) << endl;
}
class GFG {
static int countRectangles(int n) {
// Rectangle cannot be formed if n is odd.
if ((n & 1) == 1) {
return 0;
}
int res = n / 4;
// Exclude the square case.
if (((n / 2) & 1) == 0) {
res--;
}
return res;
}
public static void main(String[] args) {
int n = 10;
System.out.println(countRectangles(n));
n = 20;
System.out.println(countRectangles(n));
}
}
def countRectangles(n):
# Rectangle cannot be formed if n is odd.
if n % 2 != 0:
return 0
res = n // 4
# Exclude the square case.
if (n // 2) % 2 == 0:
res -= 1
return res
if __name__ == "__main__":
n = 10
print(countRectangles(n))
n = 20
print(countRectangles(n))
using System;
class GFG {
static int countRectangles(int n) {
// Rectangle cannot be formed if n is odd.
if ((n & 1) == 1) {
return 0;
}
int res = n / 4;
// Exclude the square case.
if (((n / 2) & 1) == 0) {
res--;
}
return res;
}
static void Main() {
int n = 10;
Console.WriteLine(countRectangles(n));
n = 20;
Console.WriteLine(countRectangles(n));
}
}
function countRectangles(n) {
// Rectangle cannot be formed if n is odd.
if ((n & 1) === 1) {
return 0;
}
let res = Math.floor(n / 4);
// Exclude the square case.
if (((Math.floor(n / 2)) & 1) === 0) {
res--;
}
return res;
}
// Driver Code
let n = 10;
console.log(countRectangles(n));
n = 20;
console.log(countRectangles(n));
Output
2 4