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]
1 year 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]1 year 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
Import simplegui
victus00 [196]

Answer:

ooh....

Explanation:

For example, to convert from bar to pounds per square inch you would multiply by 100000 then divide by 6894.757. Or, multiply by 100000/6894.757 = 14.503774. So, to convert directly from bar to pounds per square inch, you multiply by 14.503774.

3 0
2 years ago
. Write a function wordscramble that will receive a word in a string as an input argument. It will then randomly scramble the le
inysia [295]

Answer: You can use the function randperm() to generate a random permutation of the indexes. so then you would use the random permutation to reorder the string array

Explanation:

4 0
1 year ago
Assign decoded_tweet with user_tweet, replacing any occurrence of 'TTYL' with 'talk to you later'. Sample output with input: 'Go
nekit [7.7K]

Answer:

I am going to use the Python programming language to answer this. The source code is given below:

print("Enter your tweet here")

user_tweet = input()

decoded_tweet = user_tweet.replace('TTYL', 'talk to you later')

print("This is the decoded tweet: ")

print(decoded_tweet)

Explanation:

In the program the replace() module was used to replace 'TTYL' with 'talk to you later.'

Attached is the screenshot of the output.

8 0
2 years ago
Read 2 more answers
ANSWER AS SOON AS POSSIBLE: When I try to join roblox adopt me it says: "Roblox Datastore servers are currently down. Please rej
masha68 [24]
Bro This is homework no roblox
7 0
1 year ago
Read 2 more answers
Which decimal value (base 10) is equal to the binary number 1012?
GaryK [48]

Answer:

The decimal value of 101₂² base 2 = 25 base 10

Explanation:

The number 101₂² in decimal value is found as follows;

101₂ × 101₂ = 101₂ + 0₂ + 10100₂

We note that in 101 + 10100 the resultant 2 in the hundred position will have to be converted to a zero while carrying over 1 to the thousand position to give;

101₂ + 0₂ + 10100₂ = 11001₂

Therefore;

101₂² =  11001₂

We now convert the result of the square of the base 2 number, 101², which is 11001₂ to base 10 as follows;

Therefore converting 11001₂ to base 10 gives;

11001₂= 1 × 2⁴ + 1 × 2³ + 0 × 2² + 0 × 2 ¹ + 1 × 2⁰

Which gives;

16 + 8 + 0 + 0 + 1 = 25₁₀.

7 0
1 year ago
Other questions:
  • U.S. industries like steel, computers, and energy need to be protected from foreign competition to ensure which of the following
    6·2 answers
  • Computer design software requires __________________ to be used properly and successfully by architects.
    9·2 answers
  • JAVAThe method longestStreak is intended to determine the longest substring of consecutive identical characters in the parameter
    7·1 answer
  • Write the definition of a function named averager that receives a double parameter and return-- as a double -- the average value
    12·1 answer
  • A sine wave is offset 1/9 cycle with respect to time 0. What is its phase in degrees and radians? Which simple signal has a wide
    12·1 answer
  • Probability of theft in an area is 0.03 with expected loss of 20% or 30% of things with probabilities 0.55 and 0.45. Insurance p
    5·1 answer
  • You have an on-premises network that contains several servers. You plan to migrate all the servers to Azure. You need to recomme
    14·1 answer
  • Performing binary search on an unsorted list will always return the correct answer in O(n) time where n is the length of the lis
    9·1 answer
  • Through which of the devices listed are we able to connect to wireless networks? Check all that apply.
    14·1 answer
  • Assume you have a project with seven activities labeled A-G (following). Derive the earliest completion time (or early finish-EF
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!