Question

The input consists of n numbers a1, a2, . . . , an and a target...

The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t.

For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two ways: 2+5 and 1 + 6. Note that some numbers may occur several times in the sequence. For example, if the sequence is 2, 7, 2, 3, 1, 5, 6 and t = 7, we can get t in three ways 2(first occurrence) + 5, 2(second occurrence) + 5, and 1 + 6.

Describe an O(n log n) algorithm for this problem, and implement it in Java or C++.

You must explain clearly why your algorithm runs in O(n log n) time. Input specification: The input consists of two lines. The first line contains two integers n and t, separated by space. The second line contains integers a1, a2, . . . , an, separated by spaces. You may assume that the integers, as well as the sum of two such numbers, fit in int. You may also assume that n is positive and not larger than 1,000,000. Output specification: the output contains a single line with the number of pairs that sum to t.

sample input:

6 7
2 7 3 1 5 6

sample output:

4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size,t;
int arr[];
size=sc.nextInt();
t=sc.nextInt();
arr=new int[size];
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
  
int count=0;
  
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]+arr[j]==t)
count++;
}
}
System.out.println("\n"+count);
}
  
}
output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
The input consists of n numbers a1, a2, . . . , an and a target...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space....

    Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and adds all the integers between those two numbers (starting from the first number and ending at the second. Show your output in B1.txt. Sample Input: 1 6 18 74 29 38 Sample Output: 21 2622 335 (For further clarification, the first line of the sample output was done by 1+2+3+4+5+6 =...

  • Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a)...

    Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm? Algorithnm Input: a1, a2,...,an, a sequence of numbers...

  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

    Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample Input: 3 17 42 91 Sample Output: 6...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • 3. (6 pts) A sequence A = [a1, a2, . . . , anjis called a...

    3. (6 pts) A sequence A = [a1, a2, . . . , anjis called a valley sequence if there is an index i with 1 < t < n such that al > a2 > . . . > ai and ai < ai+1 < . . . < an. A valley sequence must contain at least three elements. (a) (2 pts) Given a valley sequence A of length n, describe an algorithm that finds the element a, as...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT