Given an integer C, the task is to find all possible pairs (A, B) in range [1, C) such that:
- A2 + B2 = C2
- A < B
Examples:
Input: C = 5
Output:(3, 4)
Explanation:
(3)2 + (4)2 = 9 + 16 = 25 = 52
Input: C = 25
Output:(15, 20), (7, 24)
Explanation: Both the pairs satisfy the necessary conditions.
Approach:
- Check all possible values of A and B in the range [1, C).
- Store all pair that satisfies the given conditions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , int > > Pairs( int C)
{
vector<pair< int , int > > ans;
for ( int i = 1; i < C; i++) {
for ( int j = i + 1; j < C;
j++) {
if ((i * i) + (j * j) == (C * C)) {
ans.push_back(make_pair(i, j));
}
}
}
return ans;
}
int main()
{
int C = 13;
vector<pair< int , int > > ans
= Pairs(C);
if (ans.size() == 0) {
cout << "No valid pair exist"
<< endl;
return 0;
}
for ( auto i = ans.begin();
i != ans.end(); i++) {
cout << "(" << i->first << ", "
<< i->second << ")" << endl;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static Vector<pair> Pairs( int C)
{
Vector<pair> ans = new Vector<pair>();
for ( int i = 1 ; i < C; i++)
{
for ( int j = i + 1 ; j < C; j++)
{
if ((i * i) + (j * j) == (C * C))
{
ans.add( new pair(i, j));
}
}
}
return ans;
}
public static void main(String[] args)
{
int C = 13 ;
Vector<pair> ans = Pairs(C);
if (ans.size() == 0 )
{
System.out.print( "No valid pair " +
"exist" + "\n" );
return ;
}
for (pair i:ans)
{
System.out.print( "(" + i.first +
", " + i.second +
")" + "\n" );
}
}
}
|
Python3
def Pairs(C):
ans = []
for i in range (C):
for j in range (i + 1 , C):
if ((i * i) + (j * j) = = (C * C)):
ans.append([i, j])
return ans;
if __name__ = = "__main__" :
C = 13 ;
ans = Pairs(C);
if ( len (ans) = = 0 ):
print ( "No valid pair exist" )
exit()
for i in range ( len (ans)):
print ( "(" + str (ans[i][ 0 ]) +
", " + str (ans[i][ 1 ]) + ")" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static List<pair> Pairs( int C)
{
List<pair> ans = new List<pair>();
for ( int i = 1; i < C; i++)
{
for ( int j = i + 1; j < C; j++)
{
if ((i * i) + (j * j) == (C * C))
{
ans.Add( new pair(i, j));
}
}
}
return ans;
}
public static void Main(String[] args)
{
int C = 13;
List<pair> ans = Pairs(C);
if (ans.Count == 0)
{
Console.Write( "No valid pair " +
"exist" + "\n" );
return ;
}
foreach (pair i in ans)
{
Console.Write( "(" + i.first +
", " + i.second +
")" + "\n" );
}
}
}
|
Javascript
<script>
function Pairs(C)
{
var ans = [];
for ( var i = 1; i < C; i++) {
for ( var j = i + 1; j < C;
j++) {
if ((i * i) + (j * j) == (C * C)) {
ans.push([i, j]);
}
}
}
return ans;
}
var C = 13;
var ans
= Pairs(C);
if (ans.length == 0) {
document.write( "No valid pair exist<br>" );
}
ans.forEach(x => {
document.write( "(" + x[0] + ", "
+ x[1] + ")" + "<br>" );
});
</script>
|
Time Complexity: O(C2)