Question

Java Program Sequential Search You are given a sequence of n integers S and a sequence...

Java Program

Sequential Search

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output: Print C in a line.

Constraints

  • n ≤ 10000
  • q ≤ 500
  • 0 ≤ an element in S ≤ 109
  • 0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
3 1 2
1
5

Sample Output 2

0

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

Homework Answers

Answer #1

package Sort;

import java.util.Scanner;

// Defines class SequentialSearch for sequential match count

public class SequentialSearch

{

// main method definition

public static void main(String ss[])

{

// Displays the information

System.out.println("You are given a sequence of n integers S " +

"\n and a sequence of different q integers T. " +

"\n The number of integers in T which are also in the set S." +

"\n Input: In the first line n is given. " +

"\n In the second line, n integers are given. " +

"\n In the third line q is given. " +

"\n Then, in the fourth line, q integers are given." +

"\n Output: Print C in a line." +

"\n Constraints" +

"\n n <= 10000" +

"\n q <= 500" +

"\n 0 <= an element in S <= 109" +

"\n 0 <= an element in T <= 109");

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Accepts the number of elements for first array

System.out.print("Enter the value of N: ");

int n = sc.nextInt();

// Creates the first array

int s[] = new int[n];

// Accepts each element for first array

System.out.print("Enter " + n + " elements.");

for(int c = 0; c < n; c++)

s[c] = sc.nextInt();

// Accepts the number of elements for second array

System.out.print("Enter the value of Q: ");

int q = sc.nextInt();

// Creates the second array

int t[] = new int[q];

// Accepts each element for second array

System.out.print("Enter " + q + " elements.");

for(int c = 0; c < q; c++)

t[c] = sc.nextInt();

// Calls the method and stores the count of matching

int count = showCount(s, t);

// Displays the result

System.out.println("\n Number of matches: " + count);

}// End of main method

// Method to count the number of matching numbers in second array with first array

// Receives two arrays

// Returns number of matches

static int showCount(int s[], int t[])

{

// Counter to store matches

int count = 0;

// Loops till end of the second array

for(int c = 0; c < t.length; c++)

{

// Loops till end of the first array

for(int d = 0; d < s.length; d++)

{

// Checks if current element of first array is equals to

// c index position element of the second array

if(s[d] == t[c])

{

// Increase the counter by one

count++;

// Come out of the loop

break;

}// End of if condition

}// End of inner for loop

}// End of outer for loop

// Returns the count

return count;

}// End of method

}// End of class

Sample Output:

You are given a sequence of n integers S
and a sequence of different q integers T.
The number of integers in T which are also in the set S.
Input: In the first line n is given.
In the second line, n integers are given.
In the third line q is given.
Then, in the fourth line, q integers are given.
Output: Print C in a line.
Constraints
n <= 10000
q <= 500
0 <= an element in S <= 109
0 <= an element in T <= 109
Enter the value of N: 5
Enter 5 elements.1 1 2 2 3
Enter the value of Q: 2
Enter 2 elements.1 2

Number of matches: 2

Know the answer?
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
Similar Questions
This is an intro to Java Question. My current solution is giving me bad outputs. Please...
This is an intro to Java Question. My current solution is giving me bad outputs. Please show me your way of solving this. Problem 4: Min/Max Search by Value Develop a program that, given a sequence S of integers as input, produces as two output values, the first is the minimum value that appears in the sequence and the second is the maximum value that appears in the sequence. Facts ● Scanner has a method that returns a boolean indicating...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to restore the ecological balance in the universe, has reached planet earth. He considers a planet ecologically balanced if more than half of the people on the planet have the same Consumption Capacity There are N people on planet earth, each having Consumption Capacity C1, C2, ...CN and Strength S1, S2... Sn . Thanos will make earth ecological balanced by killing some people(Possibly None). To...
Using Tuples in python In this problem, you will be given an array A of integers...
Using Tuples in python In this problem, you will be given an array A of integers of fixed size N and an integer K and you have to find the number of tuples (i, j) such that the following properties are satisfied, ● A[i]*A[i+1]*A[i+2]...A[j-1]*A[j] < K ● -1 < i < N ● i < j + 1 Note that the array is 0-indexed. Input Format The first line will contain integers N and K separated by a single space....
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Write a program to do the following. JAVA CODE • Input an integer n. • Create...
Write a program to do the following. JAVA CODE • Input an integer n. • Create a BinarySearchTree S inserting the keys 1, 2, . . . , n in that order, which will result in a completely-skewed tree. • Measure the time to search for n + 1 in S. 4 • Display the time taken for search.
Using the facilities of numpy, write a python program that reads the input signal as a...
Using the facilities of numpy, write a python program that reads the input signal as a space separated sequence of numbers on one line, and the system impulse response as a space separated sequence of numbers on the next line, and outputs (on one line) the output of the DT LTI system. The input lines should be interpreted as x[0] x[1] x[2] ... x[N-1] h[0] h[1] h[2] ... h[M-1] and the output should be produced in the same order: y[0]...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT