Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Help: RULES: You may not import any extra functionality besides the default. For example, System and Math are imported by default and thus may

JAVA Help:

RULES:

  1. You may not import any extra functionality besides the default. For example, System and Math are imported by default and thus may be used, whereas something like ArrayList must be explicitly imported so it is disallowed.
  2. You may not use any built-in functionality for copying an array, like System.arrayCopy(), Arrays.copyOf(), Object.clone(), Arrays.stream, etc. If you want to copy an array, you should do it manually (i.e. create a new array and transfer the data).

1st Problem-

Create the following method that, given an array of integers and a cardinal number, k, finds the kth smallest number in the array. You are not allowed to call any sorting functionality. Remember, a cardinal number starts with one; for the smallest value, k=1. Assume, there are enough items in the array to find the kth value.

public static int smallest(int[] arr, int k)

Examples:

smallest({1,1,1,3,3,4,5,5,5}, 5) --> 3

2nd Problem-

Create the following method that takes an array of integers and returns true or false depending on whether the elements of the array can form a relaxed sequence of consecutive numbers. By relaxed sequence, we mean that any two consecutive integers are allowed to have a distance larger than 1 as long as it doesn't exceed gap. Duplicate numbers, though, are not forming a sequence. Assume that gap>=1 (no need for validation). You may not modify arr and you may not create new arrays; you'll get zero points if you do any of these.

public static boolean isConsecutive(int[] arr, int gap)

Examples:

isConsecutive({7, 2, 4, 6, 3}, 2) ---> true isConsecutive({1, 4, 2, 1, 4, 1, 3}, 1) ---> false isConsecutive({17, 14, 12, 10, 24, 21, 11}, 3) ---> false

Please help with these 2 problems. Comments in code will be very appreciated. Thank You! :)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Which is the most abundant gas presented in the atmosphere?

Answered: 1 week ago