Question

In: Computer Science

Please use Java eclipse Find pair in an array with given sum Given an array of...

Please use Java eclipse

Find pair in an array with given sum

Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not.

Display 1 a pair is found in an array with matching sum S else 0.

Input
    6
    5
    1 -2 3 8 7

    Where,

  • First line represents integer S.
  • Second line represents the size of an array.
  • Third line represents array elements separated by single space.

Output
    1

For the given array, A[1] + A[3] = -2 + 8 = 6 which is equal to the given number S=6

Solutions

Expert Solution

Java Program:  Change the class name as per your convenient

import java.util.Scanner;

public class Main
{
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
                System.out.print("Enter Sum S : ");
                int S = sc.nextInt();
                System.out.print("Enter Size of Array : ");
                int N = sc.nextInt();
                System.out.print("Enter Elements of Array : ");
                int A[] = new int[N];
                for (int i=0; i<N; i++)
                {
                    A[i] = sc.nextInt();
                }
                
                int flag = 0;
                
                for (int i=0; i<N; i++)
                {
                    for (int j=i+1; j<N; j++)
                    {
                        if((A[i] + A[j]) == S)
                        {
                            flag = 1;
                        }
                    }
                }
                
                if (flag == 1)
                {
                    System.out.println("Your Output : 1");
                }
                else{
                    System.out.println("Your Output : 0");
                }
        }
}

Output:

Thumbs Up Please !!!


Related Solutions

In java run through eclipse /**    *    * Given a square array, determines if...
In java run through eclipse /**    *    * Given a square array, determines if it is diagonal    * That is, returns true if all values in the array are 0    * except the main diagonal. The main diagonal is entries of the form    * data[i][j] where i == j. Elements on the main    * diagonal can be 0 or any other number.    *    * Examples:    * {{1,0,0},    * {0,2,0}   ...
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
Consider the problem of finding if an array contains a pair of integers with a sum...
Consider the problem of finding if an array contains a pair of integers with a sum of 100. The array contains n integers. a. Define the signature (header) of a C++ function that solves this problem (hint: inputs/outputs of function) b. Write the pseudocode or C++ body of the function (extra credit: write the most efficient algorithm) c. What is the asymptotic complexity of your algorithm? d. What would be the complexity of the best algorithm for this problem if...
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Find the probability that the sum is as stated when a pair of dice is rolled....
Find the probability that the sum is as stated when a pair of dice is rolled. (Enter your answers as fractions.) (a) odd and less than 5 (b) odd or less than 5
A pair of dice are rolled find the following? the odds in favor of a sum...
A pair of dice are rolled find the following? the odds in favor of a sum of 4 -the odds in favor a sum of 7 or 11 - the probability of a product that is even and greater than 5
Find the probability that the sum is as stated when a pair of dice is rolled....
Find the probability that the sum is as stated when a pair of dice is rolled. (Enter your answers as fractions.) (a) odd and doubles (b) odd or doubles
for the experiment of rolling an ordinary pair of dice, find the probability that the sum...
for the experiment of rolling an ordinary pair of dice, find the probability that the sum will be even or a multiple of 6. ( you may want to use a table showing the sum for each of the 36 equally likely outcomes.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT