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
In a _____ network, each device on the network is attached to a central server. If the server fails, then the other devices will
Tresset [83]
Answer: star

Explanation: A star network topology is an implementation of a spoke–hub distribution paradigm in computer networks. In a star network, every host is connected to a central hub. In its simplest form, one central hub acts as a conduit to transmit messages. The star network is one of the most common computer network topologies.
5 0
3 years ago
What is an insertion point?
Tasya [4]
The answer to that would have to be c I think this is it so a feature bl bl bla
7 0
3 years ago
When planning the structure of a spreadsheet, columns are for _______ items and rows are for _______ items.
Alisiya [41]
I believe the correct answer from the choices listed above is option D. <span>When planning the structure of a spreadsheet, columns are for group items and rows are for single items. Hope this answers the question. Have a nice day.</span>
5 0
2 years ago
Read 2 more answers
To bundle links together in order to improve bandwidth availability and redundancy, what protocol is used
Daniel [21]

Answer:

Link Aggregation Control Protocol

Explanation:

Link Aggregation Control Protocol can be used to assist in the formation of port channel bundles of physical links.

Link Aggregation Control Protocol is an IEEE standard defined in IEEE 802.3ad. LACP lets devices send Link Aggregation Control Protocol Data Units (LACPDUs) to each other to establish a link aggregation connection.

8 0
3 years ago
Which of the following expressions is not equivalent to X ' ?
damaskus [11]

Answer:

the answer is going to be c

8 0
3 years ago
Other questions:
  • Write a function wordcount() that takes the name of a text file as input and prints the number of occurrences of every word in t
    12·1 answer
  • Why is it a mistake to put email addresses of people who don't know each other in the "To:" field?
    8·2 answers
  • Rapid development programming languages eliminate the possibility of having bugs in code. True or False
    8·1 answer
  • When typing the Works Cited in a report, set a .5" ______ indent. *
    12·1 answer
  • Which of the following statements is true?A)Implicit data type conversion is performed when you mix values of different data typ
    5·1 answer
  • Translation of a file into a coded Format that occupies less space than the original file is called
    6·1 answer
  • Question # 6
    11·2 answers
  • Plz answer this....quickkkkk​
    7·2 answers
  • The goal of this problem is to cover all roads with cameras. A camera placed at a station can cover all the roads connected to i
    9·1 answer
  • A man-in-the-middle attack or impersonation are likely to result in problems with
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!