0% found this document useful (0 votes)
61 views2 pages

Que 5

Define a square pair to be the tuple <x, y> where x and y are positive integers, x<y and x + y is a perfect square. A perfect square is an integer whose square root is also an integer. Write a function that takes an array and returns the number of square pairs that can be constructed from the elements. For example, the array {11, 5, 4, 20} would return 3 pairs: <5, 11>, <5, 20>, <4, 5>.

Uploaded by

Ramin Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Que 5

Define a square pair to be the tuple <x, y> where x and y are positive integers, x<y and x + y is a perfect square. A perfect square is an integer whose square root is also an integer. Write a function that takes an array and returns the number of square pairs that can be constructed from the elements. For example, the array {11, 5, 4, 20} would return 3 pairs: <5, 11>, <5, 20>, <4, 5>.

Uploaded by

Ramin Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Define a square pair to be the tuple <x, y> where x and y are positive, non-zero

integers, x<y and x + y is a perfect square. A perfect square is an integer whose


square root is also an integer, e.g. 4, 9, 16 are perfect squares but 3, 10 and 17
are not. Write a function named countSquarePairs that takes an array and returns
the number of square pairs that can be constructed from the elements in the array.
For example, if the array is {11, 5, 4, 20} the function would return 3 because the
only square pairs that can be constructed from those numbers are <5, 11>,
<5, 20> and <4, 5>. You may assume that there exists a function named
isPerfectSquare that returns 1 if its argument is a perfect square and 0 otherwise.
E.G., isPerfectSquare(4) returns 1 and isPerfectSquare(8) returns 0.
If you are programming in Java or C#, the function signature is
int countSquarePairs(int[ ] a)
If you are programming in C++ or C, the function signature is
int countSquarePairs(int a[ ], int len) where len is the number of elements in the
array.
You may assume that there are no duplicate values in the array, i.e, you don�t have
to deal with an array like {2, 7, 2, 2}.

Java

public class perfectSquarePair {


static int isPerfectSquare(int [] a)
{ int count=0, m,n,no;
for(int i=0;i<a.length-1;i++)
{ m=a[i]; n=a[i+1];
if(m<n && m>0 && n>0)
{ no=m+n;
for(int p=1;p<no;p++)
if(p*p==m)
{count++;break;}
}

}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a []={9, 0, 2, -5, 7};
System.out.println(isPerfectSquare(a));
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class perfectSquarePair {
static int isPerfectSquare(int [] a)
{ int count=0, m,n,no;
for(int i=0;i<a.length-1;i++)
{ m=a[i]; n=a[i+1];
if(m<n && m>0 && n>0)
{ no=m+n;
for(int p=1;p<no;p++)
if(p*p==m)
{count++;break;}
}

}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a []={9, 0, 2, -5, 7};
System.out.println(isPerfectSquare(a));
}

You might also like