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
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
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
Get Answers For Free
Most questions answered within 1 hours.