In: Computer Science
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:
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: