File tree Expand file tree Collapse file tree 2 files changed +109
-0
lines changed
2018-feb/adhoc-thinking/src/com/alg/top20/adhoc Expand file tree Collapse file tree 2 files changed +109
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .alg .top20 .adhoc ;
2+ import java .util .Arrays ;
3+
4+ public class FindDuplicate {
5+
6+ // pair wise check
7+ public static int findDuplicate1 (int [] in ) {
8+ for (int i = 0 ; i < in .length - 1 ; ++i ) {
9+ for (int j = i + 1 ; j < in .length ; ++j ) {
10+ if (in [i ] == in [j ])
11+ return in [i ];
12+ }
13+ }
14+ return -1 ;
15+ }
16+
17+ // sort + scan
18+ public static int findDuplicate2 (int [] in ) {
19+ Arrays .sort (in );
20+ for (int i = 1 ; i < in .length ; ++i ) {
21+ if (in [i ] == in [i -1 ]) return in [i ];
22+ }
23+ return -1 ;
24+ }
25+
26+ // in-place check
27+ public static int findDuplicate4 (int [] in ) {
28+ int element = -1 ;
29+ for (int i = 0 ; i < in .length ; ++i ) {
30+ element = Math .abs (in [i ]);
31+ if (in [element ] < 0 ) break ;
32+ in [element ] *= -1 ;
33+ }
34+ //System.out.println(Arrays.toString(in));
35+ return element ;
36+ }
37+
38+ public static void main (String [] args ) {
39+ int n = Integer .parseInt (args [0 ]);
40+ int [] in = new int [n ];
41+ //testcase1
42+ int i ;
43+ for (i = 0 ; i < in .length -1 ; ++i )
44+ in [i ] = i +1 ;
45+ in [i ] = i ;
46+ //System.out.println(Arrays.toString(in));
47+ long start = System .currentTimeMillis ();
48+ System .out .println (findDuplicate4 (in ));
49+ long end = System .currentTimeMillis ();
50+ System .out .println ("Time:" + (end -start )/1000.0 +"seconds" );
51+ }
52+
53+ }
Original file line number Diff line number Diff line change 1+ package com .alg .top20 .adhoc ;
2+
3+ import java .util .Arrays ;
4+
5+ public class MatrixSearch {
6+
7+ // binary search on each row
8+ public static boolean matrixSearch1 (int [][] in , int target ) {
9+ int m = in .length ;
10+ for (int i = 0 ; i < m ; ++i ) {
11+ if (Arrays .binarySearch (in [i ], target ) >= 0 )
12+ return true ;
13+ }
14+ return false ;
15+ }
16+
17+ // find the required row by linear search
18+ // along last column
19+ // binary search on that row
20+ public static boolean matrixSearch2 (int [][] in , int target ) {
21+ int m = in .length ;
22+ int n = in [0 ].length ;
23+ int i ;
24+ for (i = 0 ; i < m ; ++i ) {
25+ if (target <= in [i ][n - 1 ])
26+ break ;
27+ }
28+ return Arrays .binarySearch (in [i ], target ) >= 0 ;
29+ }
30+
31+ public static boolean matrixSearch3 (int [][] in , int target ) {
32+ int m = in .length ;
33+ int n = in [0 ].length ;
34+
35+ int left = 0 , right = n * m - 1 ;
36+
37+ while (left <= right ) {
38+ int mid = (left + right ) / 2 ;
39+ int element = in [mid / n ][mid % n ];
40+ if (element == target )
41+ return true ;
42+ if (target < element )
43+ right = mid - 1 ;
44+ else
45+ left = mid + 1 ;
46+ }
47+ return false ;
48+ }
49+
50+ public static void main (String [] args ) {
51+
52+
53+
54+ }
55+
56+ }
You can’t perform that action at this time.
0 commit comments