Question

In: Computer Science

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer.

Input:

arr = [1,2,2,6,6,6,6,7,10]

Output:

Solutions

Expert Solution

Note: As the programming language in which the code required is not mentioned. So I answered it in java.

Code to copy:

   import java.util.*;

   class special_Integer {
       public int find_Special_Integer(int[] arr) {
           int len = arr.length/4;
           for (int i=0; i<arr.length;i++){
               if (arr[i] == arr[i+len])
                   return arr[i];
           }
           return -1;
       }
      
       public static void main(String[] args){
           Scanner sc = new Scanner(System.in);
           int num;
           System.out.println("Enter array size: ");
           num = sc.nextInt();
           int[] arr = new int[num];
           for(int i=0; i<num; i++){
               arr[i] = sc.nextInt();
           }
           special_Integer si = new special_Integer();
           System.out.println("Frequently repeated integer = "+si.find_Special_Integer(arr));
       }
   }

Code Screenshot:

Output Screenshot:


Related Solutions

python3: Given a list of numbers in order order, return a new list sorted in non-decreasing...
python3: Given a list of numbers in order order, return a new list sorted in non-decreasing order, and leave the original list unchanged. function only def mergeSort(inputArray): #code here a = [3,4,6,1,21,11,9,8] b = mergeSort(a) print('original array is: ', a) print('sorted array is: ', b)
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4. the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT