Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Algorithms3 {
- public static void main (String[] args) {
- Scanner in = new Scanner (System.in);
- System.out.println("Enter lenght of the first array");
- int n = in.nextInt();
- System.out.println("Enter elements of the first array");
- int []a = new int [n];
- for (int i = 0; i < n; i++) {
- a[i] = in.nextInt(); // заполняем первый массив
- }
- System.out.println("Enter lenght of the second array");
- int k = in.nextInt();
- System.out.println("Enter elements of the second array");
- int []b = new int [k];
- for (int j = 0; j < k; j++) {
- b[j] = in.nextInt(); // заполняем второй массив
- }
- int i = 0;
- int j = 0;
- int counter = 0;
- while ((i < n) && (j < k)) {
- if (a[i] == b[j]) {
- counter++;
- i++;
- j++;
- } else if (a[i] > b[j]) {
- j++;
- } else {
- i++;
- }
- }
- System.out.println(counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement