Open In App

Square Difference of Two large Consecutive Numbers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given two positive consecutive numbers M and N, the task is to find the square difference of the two numbers without computing the square of those numbers.

Examples:  

Input: N = 4, M = 5 
Output:
Explanation: 
52 – 42 = 25 – 16 = 9.
Input: N = 999999999, M = 1000000000 
Output: 1999999999 

Approach: The idea is to use the algebraic expression to solve this problem. It is given in the question that M = N + 1. Therefore:  

  • The value to be computed is M2 – N2.
  • On replacing M with N + 1, the above equation becomes: 
(N + 1)2 - N2
  • (N + 1)2 can be expanded to: 
N2 + 12 + 2 * N - N2
  • On simplifying, the above equation becomes:
1 + 2 * N
1 + N + N
(1 + N) + N
M + N
  • Therefore, we simply need to compute and return the value of M + N.

Below is the implementation of the above approach: 
 

CPP




// C++ program to find the square
// difference of two large
// consecutive numbers
 
#include <bits/stdc++.h>
using namespace std;
typedef long long l;
 
// Function to find the square
// difference of two large
// consecutive numbers
l difference(l M, l N)
{
    return M + N;
}
 
// Driver code
int main()
{
    l M = 999999999;
    l N = 1000000000;
 
    cout << difference(M, N) << endl;
}


Java




// Java program to find the square
// difference of two large
// consecutive numbers
 
class GFG {
 
    // Function to find the square
    // difference of two large
    // consecutive numbers
    static long difference(long M, long N) { return M + N; }
 
    // Driver code
    public static void main(String[] args)
    {
        long M = 999999999;
        long N = 1000000000;
 
        System.out.print(difference(M, N) + "\n");
    }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program to find the square
# difference of two large
# consecutive numbers
 
# Function to find the square
# difference of two large
# consecutive numbers
def difference(M, N):
    return M + N
 
# Driver code
if __name__ == '__main__':
    M = 999999999
    N = 1000000000
 
    print(difference(M, N))
 
# This code is contributed by mohit kumar 29   


C#




// C# program to find the square
// difference of two large
// consecutive numbers
using System;
 
public class GFG{
   
// Function to find the square
// difference of two large
// consecutive numbers
static long difference(long M, long N)
{
    return M + N;
}
   
// Driver code
public static void Main(String[] args)
{
    long M = 999999999;
    long N = 1000000000;
   
    Console.Write(difference(M, N) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// JavaScript program to find the square
// difference of two large
// consecutive numbers
 
// Function to find the square
// difference of two large
// consecutive numbers
function difference(M, N)
{
    return M + N;
}
 
// Driver code
let M = 999999999;
let N = 1000000000;
 
document.write(difference(M, N));
 
</script>


Output

1999999999

Time Complexity: O(1)

Auxiliary Space: O(1)

Approach: Consecutive Number Square Difference Calculation using Simple Arithmetic

  • Take two input parameters, M and N, representing two consecutive numbers where M = N + 1.
  • Calculate the value of (M + N) * (M – N).
  • Return the result as the output of the function.
  • Call the function with the test case inputs and print the output.

C++




#include <iostream>
 
using namespace std;
 
int square_difference(int M, int N) {
    return (M + N) * (M - N);
}
 
int main() {
    int N = 4;
    int M = 5;
 
    cout << "Input: N = " << N << ", M = " << M << endl;
    cout << "Output: " << square_difference(M, N) << endl;
 
    N = 999999999;
    M = 1000000000;
 
    cout << "Input: N = " << N << ", M = " << M << endl;
    cout << "Output: " << square_difference(M, N) << endl;
 
    return 0;
}


Java




public class Main {
    public static int squareDifference(int M, int N) {
        return (M + N) * (M - N);
    }
 
    public static void main(String[] args) {
        int N = 4;
        int M = 5;
 
        System.out.println("Input: N = " + N + ", M = " + M);
        System.out.println("Output: " + squareDifference(M, N));
 
        N = 999999999;
        M = 1000000000;
 
        System.out.println("Input: N = " + N + ", M = " + M);
        System.out.println("Output: " + squareDifference(M, N));
    }
}


Python3




def square_difference(M, N):
    return (M + N) * (M - N)
 
# Driver code
if __name__ == '__main__':
    N = 4
    M = 5
 
    print("Input: N =", N, ", M =", M)
    print("Output:", square_difference(M, N))
     
    N = 999999999
    M = 1000000000
 
    print("Input: N =", N, ", M =", M)
    print("Output:", square_difference(M, N))


C#




using System;
 
namespace CSharpConversion {
class Program {
    // A function that calculates the square of the
    // difference between M and N
    static int SquareDifference(int M, int N)
    {
        return (M + N) * (M - N);
    }
    static void Main(string[] args)
    {
        int N = 4;
        int M = 5;
 
        // Print the input values
        Console.WriteLine("Input: N = " + N + ", M = " + M);
 
        // Call the SquareDifference function and print the
        // output
        Console.WriteLine("Output: "
                          + SquareDifference(M, N));
 
        N = 999999999;
        M = 1000000000;
 
        // Print the input values
        Console.WriteLine("Input: N = " + N + ", M = " + M);
 
        // Call the SquareDifference function and print the
        // output
        Console.WriteLine("Output: "
                          + SquareDifference(M, N));
 
        Console.ReadLine();
    }
}
}
// This code is contributed by sarojmcy2e


Javascript




function square_difference(M, N)
{
 
    // Calculate and return the result of the expression (M + N) * (M - N)
    return (M + N) * (M - N);
}
 
// Example usage
let N = 4;
let M = 5;
 
console.log("Input: N =", N, ", M =", M);
console.log("Output:", square_difference(M, N));
 
N = 999999999;
M = 1000000000;
 
console.log("Input: N =", N, ", M =", M);
console.log("Output:", square_difference(M, N));


Output

Input: N = 4 , M = 5
Output: 9
Input: N = 999999999 , M = 1000000000
Output: 1999999999

Time complexity: O(1)
Auxiliary space: O(1)

Approach: Mathematical Calculation

In this approach, we will use mathematical calculation to find the square difference of two large consecutive numbers. We know that the square of a number is equal to the sum of the first n odd numbers, where n is the number itself. Using this, we can calculate the squares of the two consecutive numbers and subtract them to find the square difference.

Steps:

  1. Calculate the square of the larger number.
  2. Calculate the square of the smaller number.
  3. Subtract the smaller square from the larger square to get the square difference.

C++




#include <bits/stdc++.h>
using namespace std;
 
int square_difference(int n, int m) {
    int larger = max(n, m);
    int smaller = min(n, m);
    int larger_square = larger * larger;
    int smaller_square = smaller * smaller;
    return larger_square - smaller_square;
}
 
int main() {
    cout << square_difference(4, 5) << endl;
    cout << square_difference(999999999, 1000000000) << endl;
    return 0;
}


Java




import java.util.*;
 
public class Main {
 
    public static int square_difference(int n, int m) {
        int larger = Math.max(n, m);
        int smaller = Math.min(n, m);
        int larger_square = larger * larger;
        int smaller_square = smaller * smaller;
        return larger_square - smaller_square;
    }
 
    public static void main(String[] args) {
        System.out.println(square_difference(4, 5));
        System.out.println(square_difference(999999999, 1000000000));
    }
}


Python3




def square_difference(n, m):
    larger = max(n, m)
    smaller = min(n, m)
    larger_square = larger ** 2
    smaller_square = smaller ** 2
    return larger_square - smaller_square
print(square_difference(4, 5))
print(square_difference(999999999, 1000000000))


C#




using System;
 
public class Program
{
    public static int SquareDifference(int n, int m)
    {
        int larger = Math.Max(n, m);
        int smaller = Math.Min(n, m);
        int larger_square = larger * larger;
        int smaller_square = smaller * smaller;
        return larger_square - smaller_square;
    }
 
    public static void Main()
    {
        Console.WriteLine(SquareDifference(4, 5));
        Console.WriteLine(SquareDifference(999999999, 1000000000));
    }
}


Javascript




function square_difference(n, m) {
    // Find the larger and smaller numbers
    let larger = Math.max(n, m);
    let smaller = Math.min(n, m);
 
    // Calculate the squares of the numbers
    let larger_square = larger * larger;
    let smaller_square = smaller * smaller;
 
    // Calculate the difference between the squares
    return larger_square - smaller_square;
}
 
console.log(square_difference(4, 5));
console.log(square_difference(999999999, 1000000000));


Output

9
1999999999

Time Complexity: O(1)

Auxiliary Space: O(1)



Last Updated : 07 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads