Question: An array is defined to be paired - N if it contains two distinct elements that sum to N for some specified value of N

An array is defined to be paired-N if it contains two distinct elements that sum to N for some specified value of N and the indexes of those elements also sum to N. Write a function named isPairedN that returns 1 if its array parameter is a paired-N array. The value of N is passed as the second parameter.
If you are writing in Java, the function signature is
int isPairedN(int[] a, int n)
There are two additional requirements.
Once you know the array is paired-N, you should return 1. No wasted loop iterations please.
Do not enter the loop unless you have to. You should test the length of the array and the value of n to determine whether the array could possibly be a paired-N array. If the tests indicate no, return 0 before entering the loop. N.B: youshould use java programming language
Examples
if a is and n is return reason
{1,4,1,4,5,6}51 because a[2]+ a[3]==5 and 2+3==5. In other words, the sum of the values is equal to the sum of the corresponding indexes and both are equal to n (5 in this case).
{1,4,1,4,5,6}61 because a[2]+ a[4]==6 and 2+4==6
{0,1,2,3,4,5,6,7,8}61 because a[1]+a[5]==6 and 1+5==6
{1,4,1}50 because although a[0]+ a[1]==5,0+1!=5; and although a[1]+a[2]==5,1+2!=5
{8,8,8,8,7,7,7}150 because there are several ways to get the values to sum to 15 but there is no way to get the corresponding indexes to sum to 15.
{8,-8,8,8,7,7,-7}-150 because although a[1]+a[6]==-15,1+6!=-15
{3}30 because the array has only one element
{}00 because the array has no elements

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!