-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
169 lines (122 loc) · 3.81 KB
/
Main.java
File metadata and controls
169 lines (122 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg20180696;
import java.util.*;
/**
*
* @author hp
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void get_index(int arr[], int x) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
System.out.println("Exist in index : " + i);
}
}
}
public static int firstt(int arr[], int l, int h, int key, int size) {
while (h >= l) {
int mid = (l + h) / 2;
if ((mid == 0 || arr[mid - 1] < key) && arr[mid] == key) {
return mid;
} else if (key > arr[mid]) {
return firstt(arr, (mid + 1), h, key, size);
} else {
return firstt(arr, l, (mid - 1), key, size);
}
}
return -1;
}
public static int lastt(int arr[], int l, int h, int key, int size) {
while (h >= l) {
int mid = (l + h) / 2;
if ((mid == size - 1 || arr[mid + 1] > key) && arr[mid] == key) {
return mid;
} else if (key < arr[mid]) {
return lastt(arr, l, (mid - 1), key, size);
} else {
return lastt(arr, (mid + 1), h, key, size);
}
}
return -1;
}
public static void first_solution(int arr[], int arr2[]) {
Arrays.sort(arr);
int size = arr.length;
int first, last = 0;
int count = -1;
first = firstt(arr, 0, size - 1, arr[size / 2], size);
if (first != -1) {
last = lastt(arr, first, size - 1, arr[size / 2], size);
count = last - first + 1;
}
if (count > (size / 2) ) {
get_index(arr2, arr[size / 2]);
} else {
System.out.println("-1");
}
}
public static int find_Dominator(int arr[]) {
int size = arr.length;
int index = 0;
int counter = 0;
for (int i = 0; i < size; i++) {
if (arr[index] == arr[i]) {
counter++;
} else {
counter--;
}
if (counter == 0) {
index = i;
counter = 1;
}
}
return arr[index];
}
public static int check_frequency(int num, int arr[]) {
int count = 0;
int size = arr.length;
for (int i = 0; i < size; i++) {
if (arr[i] == num) {
count++;
}
}
if (count > (size / 2)) {
return 1;
} else {
return -1;
}
}
public static void Second_solution(int arr[]) {
int dominator = find_Dominator(arr);
int check = check_frequency(dominator, arr);
if (check != -1) {
get_index(arr, dominator);
} else {
System.out.println("-1");
}
}
public static void main(String[] args) {
int size;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array size :");
size = sc.nextInt();
int[] arr = new int[size];
int[] arr2 = new int[size];
for (int i = 0; i < size; i++) {
System.out.print("Enter element number "+(i+1)+":");
arr[i] = sc.nextInt();
arr2[i] = arr[i];
}
//Divide and Conquer Algorithm
first_solution(arr, arr2);
//Boyer Moore's Algoritm
//Second_solution(arr);
}
}