Question

In: Computer Science

Please use Java language to write two efficient functions: Write an efficient function that compute the...

Please use Java language to write two efficient functions:

  1. Write an efficient function that compute the intersections of two sorted arrays of integers

  2. Write an efficient function that compute the intersection of two arrays of integers (not

    necessary sorted).

Solutions

Expert Solution

CODE

import java.util.HashSet;

public class Main
{
   public static void findIntersectionForSortedArrays(int arr1[], int arr2[])
   {
       int m = arr1.length;
       int n = arr2.length;
       int i = 0, j = 0;
       while (i < m && j < n)
       {
           if (arr1[i] < arr2[j])
               i++;
           else if (arr2[j] < arr1[i])
               j++;
           else
           {
               System.out.print(arr2[j++]+" ");
               i++;
           }
       }
   }
  
   public static void findIntersectionForUnsortedArrays(int arr1[], int arr2[])
    {
        HashSet<Integer> hs = new HashSet<>();
        
        for (int i = 0; i < arr1.length; i++)
            hs.add(arr1[i]);
        
        for (int i = 0; i < arr2.length; i++)
            if (hs.contains(arr2[i]))
               System.out.print(arr2[i] + " ");
    }

   public static void main(String args[])
   {
       int arr1[] = {1, 3, 4, 6, 8, 10, 11};
       int arr2[] = {3, 4, 7, 8};
       findIntersectionForSortedArrays(arr1, arr2);
      
       System.out.println();
       int arr3[] = {1, 9, 5, 4, 8, 6};
        int arr4[] = {7, 8, 9, 1, 17, 20, 4};
        findIntersectionForUnsortedArrays(arr3, arr4);
   }
}


Related Solutions

Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
java. please don't use complicated language I can follow up. Write a for loop that prints...
java. please don't use complicated language I can follow up. Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis. 1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...] Write this code once with for loop, once with while loop,...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If...
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT