Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 3, Problem 17C
Explanation of Solution
The algorithm for determine the integer which is repeated in array “A” is given below:
Algorithm:
Input: Array “A” with size of “n > 1” from “1” to “n-1” with exactly one repeated integer.
Output: Determine the integer which is repeated in array “A”.
findRepeat():
//Sort the array using any sorting techniques
Sort the input array "A"...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
An array A contains n - 1
unique integers in the range [0, n - 11;
that is, there is one number from this
range that is not in A. Design an O(n)-
time algorithm (write a pseudo-code,
Python function or Java method) for
finding that number. You can use only
0(1) additional space beside array A.
Let A be an array of size n >= 6 containing integers from 1 to n - 5, inclusive, with excactly five repeated. Describe a good algorithm for finding the five integers in A that are repeated.
Let B be an array that is of size n >= 6 containing integers from 1 to n-5, inclusive, with exactly five repeated. Describe an O(n) algorithm for finding the five integers in B that are repeated.
Chapter 3 Solutions
Data Structures and Algorithms in Java
Ch. 3 - Prob. 1RCh. 3 - Write a Java method that repeatedly selects and...Ch. 3 - Prob. 3RCh. 3 - The TicTacToe class of Code Fragments 3.9 and 3.10...Ch. 3 - Prob. 5RCh. 3 - Prob. 6RCh. 3 - Prob. 7RCh. 3 - Prob. 8RCh. 3 - Prob. 9RCh. 3 - Prob. 10R
Ch. 3 - Prob. 11RCh. 3 - Prob. 12RCh. 3 - Prob. 13RCh. 3 - Prob. 14RCh. 3 - Prob. 15RCh. 3 - Prob. 16RCh. 3 - Prob. 17CCh. 3 - Prob. 18CCh. 3 - Prob. 19CCh. 3 - Give examples of values for a and b in the...Ch. 3 - Suppose you are given an array, A, containing 100...Ch. 3 - Write a method, shuffle(A), that rearranges the...Ch. 3 - Suppose you are designing a multiplayer game that...Ch. 3 - Write a Java method that takes two...Ch. 3 - Prob. 25CCh. 3 - Prob. 26CCh. 3 - Prob. 27CCh. 3 - Prob. 28CCh. 3 - Prob. 29CCh. 3 - Prob. 30CCh. 3 - Prob. 31CCh. 3 - Prob. 32CCh. 3 - Prob. 33CCh. 3 - Prob. 34CCh. 3 - Prob. 35CCh. 3 - Write a Java program for a matrix class that can...Ch. 3 - Write a class that maintains the top ten scores...Ch. 3 - Prob. 38PCh. 3 - Write a program that can perform the Caesar cipher...Ch. 3 - Prob. 40PCh. 3 - Prob. 41PCh. 3 - Prob. 42PCh. 3 - Prob. 43P
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- A majority element is an element that makes up more than half of the items inan array. Given a positive integers array, find the majority element. If there is no majority element,return -1. Do this in O(N) time and 0(1) space.Input: 1 2 5 9 5 9 5 5 5Output: 5arrow_forwardGiven an unsorted array, A, of integers and an integer k, write a recursivejava code for rearranging the elements in A so that all elements less than or equal to k come before any elements larger than k. What is the running time of your algorithm on an array of n values.arrow_forwardLet A and B be two arrays of length n, each containing a random permutation of the numbers from 1 to n. An inversion between the two permutations A and B is a pair of values (x, y) where the index of x is less than the index of y in array A, but the index of x is more than the index of y in array B. Design an algorithm which counts the total number of inversions between A and B that runs in O(n log n) time.arrow_forward
- Write a program that randomly fills in 0s and 1s into a 6 * 6 matrix, prints the matrix, and finds the first row and first column with the least 1s.arrow_forwardGiven an array arr[] of N non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season. Example 1: Input: N = 6 arr[] = {3,0,0,2,0,4} Output: 10.arrow_forward16. Given an n-element array with n > 100, is it possible to find an algorithm that can find an element that is neither the maximum nor the minimum with fewer than 10 comparisons? (a) yes (b) noarrow_forward
- Let A be an array of size n ≥ 2 containing integers from 1 to n-1 inclusive, one of which is repeated. Describe an algorithm for finding the integer in A that is repeated and create a C++ method for the algorithm.arrow_forwardIN JAVA, USING RECURSION PLEASE Create a method int[][] generateMatrix(int row, int col, int boundary1, int boundary2, int iteration) that generates a random matrix with random numbers between [min(boundary1, boundary2), max(boundary1, boundary2)). The sum of the diagonal and the sub-diagonal should be the same. If not, regenerate it again, until a matrix that satisfies the condition is generated (return that matrix). If you try iteration times and none of the matrixes satisfy the condition, return null.arrow_forwardComputer Science Let A be an array of n numbers. Design an O(n)-time algorithm to rearrange elements of A in a way that all negative numbers precede all positive numbers. Explain your algorithm in detail and show why it runs in O(n).arrow_forward
- Implement a Binary Search algorithm, and using the results from Exercise 5, show how longthe Binary Search takes (on average) for arrays of size 10, 100, and 1000. (You do not haveto print out the values in the array).arrow_forwardEuclid’s algorithm (or the Euclidean algorithm) is an algorithm that computes thegreatest common divisor, denoted by gcd, of two integers. Below are the original versions ofEuclid’s algorithm that uses repeated subtraction and another one that uses the remainder.int gcd_sub(int a, int b){ if (!a) return b; while (b) if (a > b) a = a – b; else b = b – a; return a;}int gcd_rem(int a, int b){ int t; while (b) { t = b; b = a % b; a = t; } return a;}1. Trace each of the above algorithm using specific values for a and b.2. Compare both algorithms.arrow_forwardDesign an algorithm that takes an array containing n distinct natural numbers. A number k ≤ n and calculates the sum of the k largest numbers in the array. For example, if the array is {3, 7, 5, 12, 6} and k = 3, then the algorithm should return 25 (12+7+6). You may freely use standard data structure/s and algorithms from the course in your solutionarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage