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
When spraying pesticide wear and protective eyeglass​
exis [7]
Ummmmmmmmmmmmmmmmmmmmm ok
6 0
3 years ago
Read 2 more answers
Not all products are well suited for sale on the internet. T or F
lianna [129]
This is true.  There are somethings that you can't sell online 
4 0
3 years ago
Read 2 more answers
Long Answer Questions: a. Briefly explain the types of Control Structures in QBASIC.​
Alexus [3.1K]

Answer:

The three basic types of control structures are sequential, selection and iteration. They can be combined in any way to solve a specified problem. Sequential is the default control structure, statements are executed line by line in the order in which they appear.

7 0
3 years ago
Motivational appeals refer to
Lesechka [4]

Answer: D) All of the given options

Explanation:

Motivational appeals refers to the an emotional nature developed to increase individual values. It is a tool of emotions which target the psychological needs. It is a visualization of someone's desire and values and method of satisfying the emotions. Motivational appeals are divided into many categories like motivation and emotions.

3 0
3 years ago
You are a network consultant who has been asked to attend an initial meeting with the executive management team of ElectroMyCycl
Snezhnost [94]

Answer:

ElectroMyCycle, LLC.

The research I will do before my initial meeting with the executive management team will be to find out the following:

4. Client’s suppliers and customers

5. Products and services offered

Explanation:

My research will concentrate on the types of products and services  that ElectroMyCycle is offering its customers and raw materials that it purchases from the suppliers, because the network assets are being installed to enhance business transactions between ElectroMyCycle and its customers and suppliers.  And as the company is expanding with more suppliers and customers, more products and services, the network configuration will be built around these stakeholders.

4 0
4 years ago
Other questions:
  • Cell A1 contains the formula "=10+D2". If you copied A1 to B3, what would be the formula in B3?
    10·2 answers
  • A _____________ is a method of controlled entry into a facility and provides access to secure areas such as a research lab or da
    6·1 answer
  • What are some reasons a person might choose to remain anonymous on the Internet?
    12·2 answers
  • In terms of Technology software is defined as?
    11·2 answers
  • Complete this code in main to perform the requested operations for a date entered by the user in this format: month dd, yyyy
    10·1 answer
  • Ellen has eggs she wishes to trade for grain. However, she cannot find anyone with grain that needs eggs. What is missing for th
    14·1 answer
  • To specify AutoCorrect rules and exceptions to the rules, click File to open the Backstage view, click ____ , and then click Pro
    6·1 answer
  • What software could i use to create music like Aphex Twin?
    6·1 answer
  • Cách soạn thảo văn bản trên word
    7·1 answer
  • OSI layer for HDLC??​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!