answer.
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]
2 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]2 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
The Internet is BEST described as a vast _______ connection of computer networks that also link to smaller networks
storchak [24]
Whats ur choices if there web or world try them

4 0
2 years ago
How do you download a video from sendvid
Leona [35]
You copy the url and paste it in the box on the website

5 0
2 years ago
Read 2 more answers
Dr. Robbins wants to know if there are different opinions regarding the value of public school education between Native American
zmey [24]

Answer:

The independent variable is Native American participants

Explanation:

Why Native Americans is the independent variable is bacause the survey population is Native Americans and the result of the survey won't be affected by the gender and age of the native american participants. So it the independent variable.

5 0
2 years ago
Exercise 9.1.6: Checkerboard, v1 Spoint
AysviL [449]

Answer:

In order to get following pattern, we have to use numpy package.

Following code with work perfectly fine and will print the pattern.

Python code starts as below

*********************************************

<em># Python program to print 8 X 8 alternative 1 and 0's. 3rd and 4th row with all 0's </em>

<em># checkerboard pattern using numpy </em>

<em># We need Following pattern </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 0 0 0 0 0 0 0 0 </em>

<em># 0 0 0 0 0 0 0 0 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em> </em>

<em>import numpy as np </em>

<em> </em>

<em># function to print Checkerboard pattern </em>

<em>def printcheckboard(n): </em>

<em> </em>

<em>        print(" Customized Checkerboard pattern:") </em>

<em>        # create a n * n matrix   </em>

<em>        x = np.zeros((n, n), dtype = int) </em>

<em>        y = np.zeros((n, n), dtype = int) </em>

<em>        # fill with 1 the alternate rows and columns </em>

<em>        x[1::2, ::2] = 1 </em>

<em>        x[::2, 1::2] = 1 </em>

<em>       # fill with 0 the alternate rows and columns </em>

<em>        y[1::2, ::2] = 0 </em>

<em>        y[::2, 1::2] = 0 </em>

<em> </em>

<em>        # print the pattern  for first 3 rows</em>

<em>        for i in range(0,3): </em>

<em>                for j in range(n): </em>

<em>                        print(x[i][j], end =" ") </em>

<em>                print() </em>

<em>        # print the pattern   for next two rows with all 0's</em>

<em>        for k in range(3,5): </em>

<em>                for l in range(n): </em>

<em>                        print(y[k][l], end =" ") </em>

<em>                print() </em>

<em>         # print the pattern  for last 3 rows with alternative 1 and 0.        </em>

<em>        for i in range(5,8): </em>

<em>                for j in range(n): </em>

<em>                        print(x[i][j], end =" ") </em>

<em>                print() </em>

<em> </em>

<em># Calling the function code </em>

<em>n = 8 </em>

<em>printcheckboard(n)</em>

**************************************

End of the Python Code.

Explanation:

In this you have to use Python3.7 and numpy should be installed as well in order to execute the code successfully.

2 D matrix will be created using Python numpy library and checkboard pattern is generated using array slicing.

Here n=8 and it will generate the checkerboard pattern of alternative 0 and 1. However, we need row 4th and 5th as all 0. So we have initialized two arrays matrix as x and y.

Comments in the code is self explanatory.

PS: Please make sure code is edited in IDE so that tabs or space issues can be taken care.

8 0
2 years ago
For drivers under 21, the penalties for driving with an illegal BAL include _____.
JulijaS [17]
Up to one year In jail
5 0
2 years ago
Read 2 more answers
Other questions:
  • Suppose the algorithms used to implement the operations at layer k is changed. how does this impact services at layers k-1 and k
    10·1 answer
  • A two-dimensional array can have elements of ________ data type(s).
    7·1 answer
  • The tuna marketers' task in the "tunathewonderfish.com" website and related campaign was to ________.
    15·1 answer
  • A workgroup database is a(n) _____ database
    9·1 answer
  • A mistake programmers often make with loops is that they ____.
    15·1 answer
  • Suppose Dave drops a watermelon off a high bridge and lets it fall until it hits the water. If we neglect air resistance, then t
    12·1 answer
  • Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). O
    5·1 answer
  • Alyosha was explaining to a friend the importance of protecting a cryptographic key from cryptoanalysis. He said that the key sh
    10·1 answer
  • Sean is forecasting the time and cost of developing a customized software program by looking at the number of inputs, outputs, i
    8·1 answer
  • When should a developer begin thinking about scalability? 1 during the design phase 2during testing 3when traffic increases by 2
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!