Open In App

Pair Cube Count

Last Updated : 19 Mar, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given n, count all 'a' and 'b' that satisfy the condition a^3 + b^3 = n. Where (a, b) and (b, a) are considered two different pairs

Examples: 

Input: n = 9
Output: 2
Explanation: 1^3 + 2^3 = 9 and 2^3 + 1^3 = 9

Input: n = 28
Output: 2
Explanation: 1^3 + 3^3 = 28 and 3^3 + 1^3 = 28

[Naive Approach] Using Nested Loops - O(n^2) time and O(1) space

The approach uses a nested loop to check all pairs (a, b) where a3+b3=n. If the condition holds, the count is incremented.

C++
#include <iostream>
using namespace std;

int countPairs(int n) {
    int count = 0;
    for (int a = 1; a <= n; a++) {
        for (int b = 0; b <= n; b++) {
            if (a * a * a + b * b * b == n) {
                count++;
            }
        }
    }
    return count;
}

int main() {
    int n = 9;
    cout << countPairs(n) << endl;
    return 0;
}
C
#include <stdio.h>

int countPairs(int n) {
    int count = 0;
    for (int a = 1; a <= n; a++) {
        for (int b = 0; b <= n; b++) {
            if (a * a * a + b * b * b == n) {
                count++;
            }
        }
    }
    return count;
}

int main() {
    int n = 9;
    printf("%d\n", countPairs(n));
    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

public class GfG {
    static int countPairs(int n) {
        int count = 0;
        for (int a = 1; a <= n; a++) {
            for (int b = 0; b <= n; b++) {
                if (a * a * a + b * b * b == n) {
                    count++;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int n = 9;
        System.out.println(countPairs(n));
    }
}
Python
def count_pairs(n):
    count = 0
    for a in range(1, n + 1):
        for b in range(n + 1):
            if a**3 + b**3 == n:
                count += 1
    return count

n = 9
print(count_pairs(n))
C#
using System;

class GfG {
    static int CountPairs(int n) {
        int count = 0;
        for (int a = 1; a <= n; a++) {
            for (int b = 0; b <= n; b++) {
                if (a * a * a + b * b * b == n) {
                    count++;
                }
            }
        }
        return count;
    }

    static void Main() {
        int n = 9;
        Console.WriteLine(CountPairs(n));
    }
}
JavaScript
function countPairs(n) {
    let count = 0;
    for (let a = 1; a <= n; a++) {
        for (let b = 0; b <= n; b++) {
            if (a ** 3 + b ** 3 === n) {
                count++;
            }
        }
    }
    return count;
}

let n = 9;
console.log(countPairs(n));

Output
2

[Expected Approach] Finding different pairs - O(n1/3) time and O(1) space

While traversing numbers from 1 to the cube root of n, Subtract the cube of the current number from n and check if their difference is a perfect cube.

C++
// C++ program to count pairs whose sum cubes is n
#include<bits/stdc++.h>
using namespace std;

int countPairs(int n) {
    int count = 0;

    // Check for each number 1 to cbrt(n)
    for (int i = 1; i <= cbrt(n); i++)  {
        // Store cube of a number
        int cb = i*i*i;

        // Subtract the cube from given N
        int diff = n - cb;

        // Check if the difference is also
        // a perfect cube
        int cbrtDiff = cbrt(diff);

        // If yes, then increment count
        if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
            count++;
    }

    return count;
}

int main() {
 		int n = 9;
        cout << countPairs(n) <<"\n";

    return 0;
}
C
#include <stdio.h>
#include <math.h>

int countPairs(int n) {
    int count = 0;

    // Check for each number 1 to cbrt(n)
    for (int i = 1; i <= cbrt(n); i++) {
        // Store cube of a number
        int cb = i * i * i;

        // Subtract the cube from given N
        int diff = n - cb;

        // Check if the difference is also a perfect cube
        int cbrtDiff = cbrt(diff);

        // If yes, then increment count
        if (cbrtDiff * cbrtDiff * cbrtDiff == diff)
            count++;
    }

    return count;
}

int main() {
    int n = 9;
    printf("%d\n", countPairs(n));

    return 0;
}
Java
// Java program to count pairs whose sum cubes is n

class GfG {
    // method to count the pairs satisfying
    // a ^ 3 + b ^ 3 = N
    static int countPairs(int n) {
        int count = 0;
     
        // Check for each number 1 to cbrt(n)
        for (int i = 1; i <= Math.cbrt(n); i++) {
            // Store cube of a number
            int cb = i*i*i;
     
            // Subtract the cube from given n
            int diff = n - cb;
     
            // Check if the difference is also
            // a perfect cube
            int cbrtDiff = (int) Math.cbrt(diff);
     
            // If yes, then increment count
            if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
                count++;
        }

        return count;
    }
    
    public static void main(String args[])  {
  
 			int n = 10;
            System.out.println(countPairs(n));
    }
}
Python 3
# Python 3 program to count pairs whose sum cubes is n
import math 

def countPairs(n):
    count = 0
    
    # Check for each number 1 to cbrt(n)
    for i in range(1, int(math.pow(n, 1/3)) + 1):
        # Store cube of a number
        cb = i * i * i
        
        # Subtract the cube from given n
        diff = n - cb
        
        # Check if the difference is also a perfect cube
        cbrt_diff = round(diff ** (1/3))
        
        # If yes, then increment count
        if cbrt_diff * cbrt_diff * cbrt_diff == diff:
            count += 1

    return count

if __name__ == "__main__":
  n = 2
  print(countPairs(n))
C#
// C# program to count pairs whose sum cubes is n
 
using System;
class GfG {
    // method to count the pairs satisfying
    // a ^ 3 + b ^ 3 = N
    static int countPairs(int n) {
        int count = 0;
      
        // Check for each number 1 to cbrt(N)
        for (int i = 1; i <= Math.Pow(n,(1.0/3.0)); i++) {
            // Store cube of a number
            int cb = i*i*i;
      
            // Subtract the cube from given N
            int diff = n - cb;
      
            // Check if the difference is also
            // a perfect cube
            int cbrtDiff = (int) Math.Pow(diff,(1.0/3.0));
      
            // If yes, then increment count
            if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
                count++;
        }

        return count;
    }
     
    public static void Main()  {
        int n = 9;
         Console.Write(countPairs(n));
	}
}
JavaScript
    // Javascript program to count pairs whose sum cubes is n

    function countPairs(n) {
        let count = 0;
        
        // Check for each number 1 to cbrt(n)
        for (let i = 1; i <= parseInt(Math.pow(n,(1.0/3.0)), 10); i++)
        {
            // Store cube of a number
            let cb = i*i*i;
        
            // Subtract the cube from given n
            let diff = n - cb;
        
            // Check if the difference is also
            // a perfect cube
            let cbrtDiff = parseInt(Math.pow(diff,(1.0/3.0)), 10);
        
            // If yes, then increment count
            if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
                count++;
        }
        
        // Return count
        return count;
    }
    
	// driver code
   	  const n = 9;
      console.log(countPairs(n));

Output
2


 


Article Tags :

Explore