1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
koban [17]
3 years ago
12

Given a sorted array of integer, A, with possible duplicate elements. Implement an efficient (sublinear running time complexity)

function in Java to find in A, the numbers of occurrences of the input value k. For example, in an array A = {-1, 2, 3, 5, 6, 6, 6, 9, 10} and k = 6, your program should return 3.
Computers and Technology
1 answer:
allsm [11]3 years ago
7 0

Answer:

The java program is as follows.

import java.util.*;

import java.lang.*;

public class SearchProgram

{

   // sorted array may or may not containing duplicate elements

   static int A[] = {-1, 2, 3, 5, 6, 6, 6, 9, 10};

   

   // variable to keep track of number of occurrences, declared and initialized to 0

   static int times=0;

   

// variable to hold value to be searched in the array

   static int key;

   static int search(int[] art, int n)

   {

       // enhanced for loop used to find how many times n occurs in array, arr

       for(int j:arr)

       {

           if(n == j)

               times = times+1;

       }

       

       return times;

   }

   

   public static void main(String[] args)

   {

       // scanner object to allow user input

       Scanner sc = new Scanner(System.in);

       

       // user prompted to enter key to be searched

       System.out.print("Enter the element to be searched: ");

       key = sc.nextInt();

       

       // message displayed to the user on how many times the element is present in the array

       System.out.println("The element " + key + " is present " + search(A, key) + " times in the array.");

   

   }

}

OUTPUT

Enter the element to be searched: 3

The element 3 is present 1 times in the array.

Explanation:

The steps of program execution are shown below.

1. An integer array, A, is declared and initialized.

2. An integer variable, times, is declared and initialized to 0.

3. A method, search(), is created which takes 2 parameters, array A and user entered value for the element to be searched in A.

4. The search() method has return type of integer and returns the value of variable, times.

5. Inside the search() method, an enhanced for loop is used to compute the value of the variable, times.

6. In the main() method, user is prompted to enter the value of key.

7. The search() is called and value of the variable, times, displayed to the user.

8. All the methods and variables are declared as static.

You might be interested in
If you paste a word document text into an excel spreadsheet, the paste optio allow you to keep source formatting or
castortr0y [4]
I think the answer is c
6 0
3 years ago
Choose all that apply.
kirill115 [55]

Answer:

Bullets can be turned off and on.

Lists don't have to use numbers or bullets.

Explanation:

because L just wanted the light

6 0
3 years ago
Read 2 more answers
To what are multiple servers arranged in racks related
qaws [65]

Various number of servers placed in racks are related to Rack servers in data center.

Explanation:

In data center commonly used server is Rack servers.  Servers are being arranged in the mounted racks are generally called as rack servers in the data center. Internal fans are being fitted inside the racks make the servers to make a good airflow and maintain the cooling. There are different types of racks available the user can choose based on their requirement.

5 0
3 years ago
Read 2 more answers
(2-2) In relatives.pl file, write and add the following rule. ancestor(X, Y) : X is an ancestor of Y Run ancestor(brown, X). Sub
alukav5142 [94]

Answer:

Check the explanation

Explanation:

relatives.pl

/* Facts */

male(ace).

male(john).

male(jack).

male(bill).

male(david).

male(brown).

male(daniel).

female(cecil).

female(aba).

female(cathy).

parent(john,ace)

parent(tom,john)

parent(jack,john)

parent(bill,aba)

parent(brown,aba)

parent(cecil,bill)

parent(david,cecil)

parent(cathy,brown)

parent(daniel,cathy)

parent(ellen,daniel)

/* parent(X,Y) -> Y is parent of X */

wife(ceceil,jack) /* wife(X,Y) -> Y is wife of X */

Answer of 2-2

Considering all facts and rules answer will be daniel,ellen.

Answer 2-3

Considering all facts and rules answer will be bill.

Answer 2-4

Considering all facts and rules X will be john and y will be cecil.

Answer 2-5

Considering all facts and rules X will be tom and y will be david.

4 0
3 years ago
Which device makes computers that are connected to separate segments appear and behave as if they're on the same segment? (Pleas
Mashcka [7]

Answer: Switch

Explanation:

 The switch is the device that is basically use for the connection for separate segment that basically appear in the similar segment in the computer network.

Switch is the ethernet that is based on the  LAN peruses approaching TCP/IP information packets containing goal data as they go into at least one info ports. The goal data in the bundles is utilized to figure out which yield ports that will be utilized to sending the information on to its proposed goal.

Therefore, Switch is the correct options.

 

4 0
4 years ago
Other questions:
  • You are a member of the Administrators group on all company computers. You are having difficulty changing permissions for a fold
    11·1 answer
  • You are working on a router that has established privilege levels that restrict access to certain functions. you discover that y
    5·1 answer
  • Contextual targeting method mean
    11·1 answer
  • Enables businesses and consumers to share data or use software applications directly from a remote server over the Internet or w
    6·1 answer
  • Why is outfitting a workspace with video games in technology development company considered a strategic use of money
    8·2 answers
  • Explain 5 service provided by Internet​
    6·1 answer
  • RAM that can be changed. True or False
    9·2 answers
  • I’ll mark who ever is first or last Doesn’t matter JUST PLEASE HELP
    8·1 answer
  • Which are the two alternatives for pasting copied data in a target cell or a group of cells?
    12·1 answer
  • g Write a function number_of_pennies() that returns the total number of pennies given a number of dollars and (optionally) a num
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!