Question: 1. Write a Java program (Ex1.java) to find the index i in an integer array A such that A[i] is closest to a given target

1. Write a Java program (Ex1.java) to find the index i in an integer array A such that A[i] is closest to a given target number. The array A sorted in ascending order. Print "-1" if there is no element in the array.

Example:

Given [1, 2, 3] and target = 2, print 1.

Given [1, 4, 6] and target = 3, print 1.

Given [1, 4, 6] and target = 5, print 1 or 2.

Given [1, 3, 3, 4] and target = 2, print 0 or 1 or 2.

2. Write a Java program (Ex2.java) to find the total number of occurrences of a target number in a sorted array. The array sorted in ascending order.

Example:

Given [1, 3, 3, 4, 5] and target = 3, print 2.

Given [2, 2, 3, 4, 6] and target = 4, print 1.

Given [1, 2, 3, 4, 5] and target = 6, print 0.

3. Write a Java program (Ex3.java) to compare two strings A and B, determine whether A contains all of the characters in B. The characters in string A and B are all Upper Case letters.

Example:

For A = "ABCD", B = "ACD", print "A contains B".

For A = "ABCD", B = "AABC", print "A doesn't contain B".

4. Write a Java program (Ex4.java) to find the contiguous subarray with smallest sum. Print the sum of the subarray.

Example:

For [1, -1, -2, 1], return -3.

5. Write a Java program (Ex5.java) to compute the number of trailing zeros in n factorial.

Example:

11! = 39916800, print 2

6. Write a program (Ex6.java) to play the game of "Guessing Number - 2 Players". Firstly, player one(P1) proposes a number N, the program generates a random number between 0 to N. Then, player two(P2) has 1 chance to guess the number. If P2 hits the number, P2 wins. Otherwise, P2 can either proposes a new number N' (N' <= N) and let the P1 guesses the new generated number, or doesn't propose any new number and let the P1 guesses the previous random number. Then, P1 has 1 chance to guess. If P1 hits the number, P1 wins. Otherwise, P1 can either proposes a new number N'' (N'' <= N (or N')) and let the P2 guesses the new generated number, or doesn't propose any new number and let the P2 guesses the previous random number. Keep repeating until one of players win the game.

Example:

(P1) - Welcome P1, please input a random seed:

(P1) - 5

(P2) - P2, please input your guess(0 - 5):

(P2) - 1

(P2) - Incorrect. Please select "ONE. Continue guessing; TWO. Generate a new number

(P2) - ONE

(P1) - P1, please input your guess(0 - 5):

(P1) - 2

(P1) - Incorrect. Please select "ONE. Continue guessing; TWO. Generate a new number

(P1) - TWO

(P1) - 4

(P2) - P2, please input your guess(0 - 4):

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!