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
Please use the tables in the flights database. Your deliverable should include a single SQL query that I can run against a "fres
Arlecino [84]

Answer:

The scripts are:

1. SELECT * FROM planes

2. UPDATE planes SET YEAR = 2013 WHERE year IS NULL

3. INSERT INTO planes (Tailnum, Year, type, Manufacturer, Model, Engines, Seats) VALUES ('N15501',2013,'Fixed wing single engine', 'BOEING', 'A222-101',3,100,NULL)

4. DELETE FROM planes WHERE Tailnum = 'N15501'

Explanation:

1. SELECT * FROM planes

To select all from a table, use <em>select * from [table-name]. </em>In this case, the table name is planes

2. UPDATE planes\ SET YEAR = 2013 WHERE year IS NULL

To do this, we use the update query which is as follows:

<em>UPDATE [table-name] SET [column-name]= [value] WHERE [column] IS NULL</em>

So: the above query will update all YEAR column whose value is NULL to 2014

3. INSERT INTO planes (Tailnum, Year, type, Manufacturer, Model, Engines, Seats) VALUES ('N15501',2013,'Fixed wing single engine', 'BOEING', 'A222-101',3,100,NULL)

To insert is very straight foward.

The syntax is:

INSERT INTO [table-name] (column names) VALUES (values)

4. DELETE FROM planes WHERE Tailnum = 'N15501'

To do this, we use the update query which is as follows:

<em>DELETE FROM [table-name] WHERE [column] = [value]</em>

So: The above query will delete the entry in (3) above

5 0
3 years ago
When you add text to a slide using presentation software, which of the following is important?
katrin [286]
I think the answer is B. The text should be brief. 
7 0
4 years ago
Read 2 more answers
What is ms- power point?​
WITCHER [35]

Answer:

Microsoft PowerPoint is a powerful slide show presentation program. It is a standard component of the company's Microsoft Office suite software, and is bundled together with Word, Excel, and other office productivity tools. The program uses slides to convey information rich in multimedia.

hope it work️

5 0
3 years ago
Read 2 more answers
Who is the CEO of Epic Games?​
slavikrds [6]

Answer:

it's tim sweeney....

Explanation:

hope it helps

4 0
3 years ago
Read 2 more answers
What is life all about?
zloy xaker [14]
Life can be about anything........
8 0
3 years ago
Read 2 more answers
Other questions:
  • Complete each statement by choosing the correct answer from the drop-down menu.
    10·1 answer
  • Pls help............
    9·2 answers
  • When would an absolute cell reference be most helpful?
    12·2 answers
  • Hurry asap it’s times
    8·1 answer
  • What is the most important trait of the first pilot project in the AI Transformation Playbook?
    10·1 answer
  • The __________ operator increases the value of the variable by 1 after the original value is used in the expression in which the
    7·1 answer
  • Please can someone help my assignment.​
    13·1 answer
  • What does input allow a computer to do
    14·1 answer
  • Guys is there a way to watch manga in colour for free
    13·1 answer
  • When you save a new file, you use the ______ dialog box to provide a filename and select a location of the file.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!