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++
// C++ code for the above approach #include <iostream> using namespace std; // Function to count number of squads int countSquads( int N) { int count = 0; int i, j, k, l; // Iterating 4 nested loop to find // four varible 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 (a*b) + (c*d) == N // then increase the // counter by 1. if ((i * j) + (k * l) == N) { count++; } } } } } // Return the total count return count; } // Driver code int main() { int N = 4; // Function call int ans = countSquads(N); cout << ans << endl; return 0; } |
Java
// Java code for the above approach import java.io.*; class Main { // Function to count number of squads static int countSquads( int N) { int count = 0 ; int i, j, k, l; // Iterating 4 nested loop to find // four variable 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 (a*b) + (c*d) == N // then increase the // counter by 1. if ((i * j) + (k * l) == N) { count++; } } } } } // Return the total count return count; } // Driver code public static void main(String[] args) { int N = 4 ; // Function call int ans = countSquads(N); System.out.println(ans); } } // This code is contributed by Prajwal Kandekar |
Python3
# Function to count number of squads def countSquads(N): count = 0 # Iterating 4 nested loop to find # four variable i j k l 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 (a*b) + (c*d) == N # then increase the # counter by 1. if (i * j) + (k * l) = = N: count + = 1 # Return the total count return count # Driver code N = 4 # Function call ans = countSquads(N) print (ans) |
C#
// C# code for the above approach using System; public class GFG { // Function to count number of squads static int countSquads( int N) { int count = 0; int i, j, k, l; // Iterating 4 nested loop to find four variable 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 (a*b) + (c*d) == N then // increase the counter by 1. if ((i * j) + (k * l) == N) { count++; } } } } } // Return the total count return count; } static public void Main() { // Code int N = 4; // Function call int ans = countSquads(N); Console.WriteLine(ans); } } // This code is contributed by karthik. |
Javascript
// Function to count number of squads function countSquads(N) { let count = 0; // Iterating 4 nested loops to find // four variables i, j, k, l 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 (a*b) + (c*d) == N // then increase the counter by 1. if ((i * j) + (k * l) === N) { count++; } } } } } // Return the total count return count; } // Driver code const N = 4; // Function call const ans = countSquads(N); console.log(ans); |
8
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++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function for counting all divisors 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 count return cnt; } // Driver code int main() { int n = 4; int count = 0; for ( int i = 1; i < n; i++) { int X = i; int Y = n - i; // Function call int a1 = countDivisors(Y); // a1 is permutaion of (u, v) for X; int a2 = countDivisors(X); // a2 is permutaion of (u, v) for Y; count += (a1 * a2); // Total permutation for (X, Y) } cout << count << endl; return 0; } |
Python3
import math # Function for counting all divisors 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 count return cnt # Driver code def main(): n = 4 count = 0 for i in range ( 1 , n): X = i Y = n - i # Function call a1 = countDivisors(Y) # a1 is permutation of (u, v) for X a2 = countDivisors(X) # a2 is permutation of (u, v) for Y count + = (a1 * a2) # Total permutation for (X, Y) print (count) if __name__ = = "__main__" : main() |
Javascript
// Function for counting all divisors 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 count return cnt; } // Driver code function main() { let n = 4; let count = 0; for (let i = 1; i < n; i++) { let X = i; let Y = n - i; // Function call let a1 = countDivisors(Y); // a1 is permutaion of (u, v) for X; let a2 = countDivisors(X); // a2 is permutaion of (u, v) for Y; count += a1 * a2; // Total permutation for (X, Y) } console.log(count); } main(); // akashish__ |
8
Time Complexity: O(N3/2)
Auxiliary Space: O(1)
Please Login to comment...