Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the following program is printing all pairs whose sum is equal to a given no. If no such pairs exist then how do i display a message that it doesn't do so.If i put it in the else part then same message is printing arr.length times.Anyone can help me to display a message-"no such pairs exist" if the no. is greater or less than the sum of given array only once?Any help would be appreciated.

What I have tried:

Java
public class pairsum {
	
	public static void arri(int arr[],int n)
	{  int first=0,second=0;
		for(int a=0;a<arr.length;a++)
		{	 first=arr[a];
			for(int b=a+1;b<arr.length;b++)
			{	second=arr[b];
				if(first+second==n)
					System.out.printf("(%d, %d)%n",first,second);		
			}
		}	
	}
	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int arr[]=new int[5];
		System.out.println("enter integer array\n");
		for(int i=0;i<arr.length;i++)
			arr[i]=s.nextInt();
		System.out.println("enter the no. to find sum\n");
		int n=s.nextInt();
		arri(arr,n);
			
	}
}
Posted
Updated 25-Aug-18 13:59pm
v2

Although the algorithm written is very poor, but I will not be talking about it. :)

Just the thing that, you can have the function print that the values in the array do not sum to the value provided, inside the function itself, if the iterations have finished running. Like this,
Java
public static void arri(int arr[],int n)
{
    int first=0,second=0;
    for(int a=0;a<arr.length;a++) {
        first=arr[a];
        for(int b=a+1;b<arr.length;b++) {
            second=arr[b];
            if(first + second == n) {
                System.out.printf("(%d, %d)%n", first, second);
                return; // Return out of the function.
            }
        }
    }

    // Program reacher here, because the loop didn't find any combination. 
    System.out.println("no such pairs exist");
}

Tip: You can improve the function by using a different approach, for example, use one index and follow along the start of the array, and sum it with the last element in the array to see if you got it. This would work with sorted arrays. In the case of an unsorted one, you can try saving the numbers you have seen and finding it you have that number available in the previous list. This will improve the current algorithm from O(N2) to O(N).

In academics, data structures, algorithms and refinement matter a lot, please do not skip them, otherwise getting a job will be more difficult than this simple problem. :-) Just my 2 cents.
 
Share this answer
 
Quote:
How do I print if given no. Is greater than array then no such pair exists

The answer is hidden in the statement.
Quote:
the following program is printing all pairs whose sum is equal to a given no. If no such pairs exist then how do i display a message that it doesn't do so

When can you know that no pair was found ?
- After the check is finished.
How to know that there was some pairs or not ?
- Set a counter and count them as you find them.
Java
public static void arri(int arr[],int n)
{  int first=0,second=0;
    // set the pairs counter here
    for(int a=0;a<arr.length;a++)
    {    first=arr[a];
        for(int b=a+1;b<arr.length;b++)
        {   second=arr[b];
            if(first+second==n) // This is where you know that a pair is found
                System.out.printf("(%d, %d)%n",first,second);
        }
    }
    // end of pairs check is here
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900