Count of squads of positive integers such that (A * B) + (C * D) = N
Given a positive integer N, the task is to find the count of squads of positive integers (A, B, C, D) such that (A * B) + (C * D) = N.
Note: (A, B, C, D) is different from (A, D, B, C).
Example:
Input: 4
Output: 8
Explanation:
(A, B, C, D)=(1, 1, 1, 3)
(A, B, C, D)=(1, 1, 3, 1)
(A, B, C, D)=(1, 2, 1, 2)
(A, B, C, D)=(1, 2, 2, 1)
(A, B, C, D)=(1, 3, 1, 1)
(A, B, C, D)=(2, 1, 1, 2)
(A, B, C, D)=(2, 1, 2, 1)
(A, B, C, D)=(3, 1, 1, 1)
Naive Approach: The basic way to solve the problem is as follows:
In this implementation, we use four nested loops to iterate over all possible values of A, B, C, and D. We check if (AB) + (CD) equals N and that A is less than or equal to B, and C is less than or equal to D to avoid counting the same squad multiple times.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countSquads( int N)
{
int count = 0;
int i, j, k, l;
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
for (k = 1; k <= N; k++) {
for (l = 1; l <= N; l++) {
if ((i * j) + (k * l) == N) {
count++;
}
}
}
}
}
return count;
}
int main()
{
int N = 4;
int ans = countSquads(N);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class Main {
static int countSquads( int N)
{
int count = 0 ;
int i, j, k, l;
for (i = 1 ; i <= N; i++) {
for (j = 1 ; j <= N; j++) {
for (k = 1 ; k <= N; k++) {
for (l = 1 ; l <= N; l++) {
if ((i * j) + (k * l) == N) {
count++;
}
}
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 4 ;
int ans = countSquads(N);
System.out.println(ans);
}
}
|
Python3
def countSquads(N):
count = 0
for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
for k in range ( 1 , N + 1 ):
for l in range ( 1 , N + 1 ):
if (i * j) + (k * l) = = N:
count + = 1
return count
N = 4
ans = countSquads(N)
print (ans)
|
C#
using System;
public class GFG {
static int countSquads( int N)
{
int count = 0;
int i, j, k, l;
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
for (k = 1; k <= N; k++) {
for (l = 1; l <= N; l++) {
if ((i * j) + (k * l) == N) {
count++;
}
}
}
}
}
return count;
}
static public void Main()
{
int N = 4;
int ans = countSquads(N);
Console.WriteLine(ans);
}
}
|
Javascript
function countSquads(N) {
let count = 0;
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
for (let k = 1; k <= N; k++) {
for (let l = 1; l <= N; l++) {
if ((i * j) + (k * l) === N) {
count++;
}
}
}
}
}
return count;
}
const N = 4;
const ans = countSquads(N);
console.log(ans);
|
Time Complexity: O(N4)
Auxilairy Space: O(1)
Efficient Approach: To solve the problem follow the below idea:
We can write the given number in the form X + Y and we have to find how many ways we express X and Y in the form of U×V. so we can find the total number of (X Y) combination by traversing from 1 to the number and we can find I and i-n. and in each operation, we have to find (U, V) for X and Y .for that we can find all divisors of the number. And it can be proved that number of divisors is equal to the number of (|U, v) combinations.So, we can find the C1 combination for X and C2 combination for Y . And in total we can get C1 * C2 combination in each iteration.
Steps involved in the implementation of code:
- Iterate a loop from 1 to the number N.
- Find all the combinations (A, B) such that A + B = N.
- Find two numbers A and B where A = i and B = N-i.
- By the countDivisor() function we are counting all the divisors of A and B.
- And by multiplying these we can get all possible combinations.
- And adding all the combinations to the answer we can get the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisors( int n)
{
int cnt = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2;
}
}
return cnt;
}
int main()
{
int n = 4;
int count = 0;
for ( int i = 1; i < n; i++) {
int X = i;
int Y = n - i;
int a1 = countDivisors(Y);
int a2 = countDivisors(X);
count += (a1 * a2);
}
cout << count << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static int countDivisors( int n) {
int cnt = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 ) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2 ;
}
}
return cnt;
}
public static void main(String[] args) {
int n = 4 ;
int count = 0 ;
for ( int i = 1 ; i < n; i++) {
int X = i;
int Y = n - i;
int a1 = countDivisors(Y);
int a2 = countDivisors(X);
count += (a1 * a2);
}
System.out.println(count);
}
}
|
Python3
import math
def countDivisors(n):
cnt = 0
for i in range ( 1 , int (math.sqrt(n)) + 1 ):
if n % i = = 0 :
if n / / i = = i:
cnt + = 1
else :
cnt + = 2
return cnt
def main():
n = 4
count = 0
for i in range ( 1 , n):
X = i
Y = n - i
a1 = countDivisors(Y)
a2 = countDivisors(X)
count + = (a1 * a2)
print (count)
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static int CountDivisors( int n)
{
int cnt = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
cnt++;
else
cnt += 2;
}
}
return cnt;
}
static void Main()
{
int n = 4;
int count = 0;
for ( int i = 1; i < n; i++)
{
int X = i;
int Y = n - i;
int a1 = CountDivisors(Y);
int a2 = CountDivisors(X);
count += (a1 * a2);
}
Console.WriteLine(count);
}
}
|
Javascript
function countDivisors(n) {
let cnt = 0;
for (let i = 1; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
if (n / i === i) {
cnt++;
} else {
cnt += 2;
}
}
}
return cnt;
}
function main() {
let n = 4;
let count = 0;
for (let i = 1; i < n; i++) {
let X = i;
let Y = n - i;
let a1 = countDivisors(Y);
let a2 = countDivisors(X);
count += a1 * a2;
}
console.log(count);
}
main();
|
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Last Updated :
01 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...