Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.88 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Set;
  8.  
  9. class Main {
  10. public static void main(String[] args) {
  11.  
  12. int A[] = {1,4,45,6,10,8,7,9};
  13. int n = 16;
  14. int arr_size = A.length;
  15. problem1(A, arr_size, n);
  16. linebreak();
  17. int a2[] = {1,3,3,1,2,1,2,2,2,1,2,2,2};
  18. problem2(a2, a2.length);
  19. linebreak();
  20. int a3[] = {2,3,4,5,2,4,3,5,2,4,2};
  21. problem3(a3);
  22. linebreak();
  23. int a4[] = {-2, -3, 4, -1, -2, 1, 5, -3};
  24. System.out.println("Maximum contiguous sum is: " + problem4(a4));
  25. linebreak();
  26. int a5[] = {1, 2, 4, 5, 6};
  27. System.out.println("The missing number is: " + problem5(a5));
  28. linebreak();
  29. int a6[] = {1,2,3,4,5,6,7};
  30. problem6(a6);
  31. linebreak();
  32. int a7[] = {1,2,3,4,5,6,7};
  33. problem7(a7, 3);
  34. linebreak();
  35. int a8[] = {3,2,5,10,7};
  36. problem8(a8);
  37. linebreak();
  38. problem9();
  39. linebreak();
  40. int a10[] = {18, 17 ,4, 5, 6};
  41. problem10(a10);
  42. linebreak();
  43. int a11[] = {2,4,1,3,5};
  44. problem11(a11);
  45. linebreak();
  46. int a12[] = {12,13,1,10,34,1,2};
  47. problem12(a12);
  48. linebreak();
  49. int a16[] = {1,2,3,4,4,4,4};
  50. problem16(a16,4);
  51. linebreak();
  52. int a17[] = {1000,456,348, 2, 8000, 698};
  53. problem17(a17);
  54. linebreak();
  55. int a18[] = {0,1,1,0,1,0,1,1,1,0,0,1,0,1,0};
  56. problem18(a18);
  57. linebreak();
  58. int a19[] = {1,23,12,9,30,2,50};
  59. problem19(a19,2);
  60. linebreak();
  61. int a20[] = {80,2,6,3,100};
  62. problem20(a20);
  63. linebreak();
  64. int a21[] = {2,3,6,80,104};
  65. problem21(a21,7);
  66. linebreak();
  67. int a22[] = {12,34,45,8,30,9,3};
  68. problem22(a22);
  69. linebreak();
  70. int a23[] = {1,2,3,7,3,2,9,8};
  71. problem23(a23);
  72. linebreak();
  73. int a24[] = {4,3,2,7,9,2,3,5,8,8,7};
  74. problem24(a24);
  75. linebreak();
  76. String s1[] = {"asd","fgh","jkl"};
  77. System.out.println(Arrays.asList(s1).contains("asb"));
  78. linebreak();
  79. System.out.println(IsPerfectSquare(121));
  80. linebreak();
  81. getSumofDigits("aa23sdsaadsfsd200sdfsf");
  82. linebreak();
  83. int a25[] = {0,1,1,0,1,2,1,2,0,0,0,0,1};
  84. problem25(a25);
  85. linebreak();
  86. int a26[] = {1,2,3,1,3,6,6};
  87. problem26(a26);
  88. linebreak();
  89. int a27[] = {0,1,2,3,4,6,7};
  90. problem27(a27);
  91. linebreak();
  92. int a28[] = {1,2,2,3,3,3,3};
  93. problem28(a28,3);
  94. linebreak();
  95. int a29[] = {1,2,3,4,6,1};
  96. problem29(a29);
  97. linebreak();
  98. int a30[] = {4,5,6,7,8,4,4};
  99. problem30(a30,3);
  100. linebreak();
  101. }
  102.  
  103. private static void linebreak() {
  104. System.out.println("--------------------------------------------------------");
  105. }
  106.  
  107. // Given an Array A[] and a number x, check for a pair in A[] with sum 'x'
  108. private static void problem1(int arr[], int arr_size, int sum) {
  109. int i, temp;
  110. boolean[] binMap = new boolean[100000];
  111.  
  112. Arrays.fill(binMap, false);
  113.  
  114. for(i = 0; i < arr_size ; i++) {
  115. temp = sum - arr[i];
  116. if(temp >= 0 && binMap[temp] == true) {
  117. System.out.println("Pair with given sum " + sum + " is: " + arr[i] + " and " + temp);
  118. }
  119. binMap[arr[i]] = true;
  120. }
  121. }
  122.  
  123. // Given an Array of size 'n', find an element that appears more than n/2 times
  124. private static void problem2(int[] a2, int length ) {
  125.  
  126. int candidate = findCandidate(a2, length);
  127. if(isMajority(a2, length, candidate)) System.out.println("The majority candidate is: " + candidate);
  128. else System.out.println("There's no majority element.");
  129. }
  130.  
  131. private static boolean isMajority(int[] a, int size, int cand) {
  132. int i, count = 0;
  133. for (i = 0; i < size; i++) {
  134. if(a[i] == cand) count++;
  135. }
  136. if (count > size/2) return true;
  137. else return false;
  138. }
  139.  
  140. private static int findCandidate(int a[], int size) {
  141. int maj_index = 0, count = 1;
  142. int i;
  143. for(i=1;i < size; i++){
  144. if(a[maj_index] == a[i]) count++;
  145. else count--;
  146. if(count == 0) {
  147. maj_index = i;
  148. count = 1;
  149. }
  150. }
  151. return a[maj_index];
  152. }
  153.  
  154. // Find the number occuring odd number of times
  155. private static void problem3(int[] ar) {
  156. int size = ar.length;
  157. int i, res = 0;
  158. for (i = 0; i< size; i++) {
  159. res = res ^ ar[i];
  160. }
  161. if (res != 0) System.out.println("The number occuring odd number of times is: " + res);
  162. else System.out.println("There's no such number. ");
  163. }
  164.  
  165. // Largest sum contiguous sub-array
  166. private static int problem4(int[] a) {
  167. int size = a.length;
  168. int max_so_far = a[0], i;
  169. int curr_max = a[0];
  170.  
  171. for(i = 1; i < size; i++) {
  172. curr_max = max(a[i], curr_max+a[i]);
  173. max_so_far = max(max_so_far, curr_max);
  174. }
  175. return max_so_far;
  176. }
  177. private static int max(int x, int y) {
  178. return (y > x)? y : x;
  179. }
  180.  
  181. // Find the missing number
  182. private static int problem5(int[] a) {
  183. int n = a.length;
  184. int i, total;
  185. total = (n+1)*(n+2)/2;
  186. for (i = 0; i < n; i++) total -= a[i];
  187.  
  188. return total;
  189. }
  190.  
  191. // Reverse an array
  192. private static void problem6(int[] ar) {
  193. System.out.print("Array before reversal: ");
  194. printArray(ar);
  195.  
  196. int n = ar.length;
  197. int i = 0;
  198. int j = n-1;
  199.  
  200. for(i = 0; i < n/2; i++,j--) {
  201. int temp = ar[i];
  202. ar[i] = ar[j];
  203. ar[j] = temp;
  204. }
  205. System.out.print("Array after reversal: ");
  206. printArray(ar);
  207. }
  208.  
  209. private static void printArray(int[] a) {
  210. for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
  211. System.out.println("");
  212. }
  213.  
  214. // Rotate an array by given places
  215. private static void problem7(int[] a, int n) {
  216. System.out.print("Original Array: ");
  217. printArray(a);
  218.  
  219. int newarr[] = new int[a.length];
  220. int k = 0;
  221.  
  222. for(int i = 0; i < n; i++) newarr[i] = a[i];
  223. for(int j = n; j < a.length; j++,k++) a[k] = a[j];
  224. for(int i = n+1, ii = 0; i < a.length; i++, ii++) a[i] = newarr[ii];
  225.  
  226. System.out.print("Array after rotation: ");
  227. printArray(a);
  228. }
  229.  
  230. // Function to return max sum such that no two elements are adjacent
  231. private static void problem8(int[] a) {
  232. int n = a.length;
  233. int incl = a[0];
  234. int excl = 0;
  235. int excl_new, i;
  236.  
  237. for(i = 1; i < n ; i++) {
  238. excl_new = (incl > excl) ? incl : excl;
  239.  
  240. incl = excl + a[i];
  241. excl = excl_new;
  242. }
  243. int result = (incl > excl) ? incl : excl;
  244. System.out.println("The max sum is: " + result);
  245. }
  246.  
  247. // Find union and intersection of 2 arrays
  248. private static void problem9() {
  249. int ar1[] = {1,2,3,4,5};
  250. int ar2[] = {2,4,6};
  251.  
  252. HashMap<Integer, Integer> union = new HashMap<Integer, Integer>();
  253. HashMap<Integer, Integer> intersection = new HashMap<Integer, Integer>();
  254.  
  255. for(int i = 0; i < ar1.length; i++) {
  256. if (!union.containsKey(ar1[i])) union.put(ar1[i], 1);
  257. }
  258.  
  259. for(int i = 0; i < ar2.length; i++) {
  260. if (!union.containsKey(ar2[i])) union.put(ar1[i], 1);
  261. else intersection.put(ar2[i],1);
  262. }
  263.  
  264. System.out.print("The union of 2 arrays is: ");
  265. for(Integer a: union.keySet()) System.out.print(a.toString() + " ");
  266. System.out.println("");
  267. System.out.print("The intersection of 2 arrays is: ");
  268. for(Integer a: intersection.keySet()) System.out.print(a.toString() + " ");
  269. System.out.println("");
  270. }
  271.  
  272. // Print leaders in an array
  273. private static void problem10(int[] a) {
  274. System.out.print("Leaders in the array: ");
  275. int max_from_right = a[a.length - 1], i;
  276.  
  277. System.out.print(max_from_right + " ");
  278. for(i = a.length - 2; i >= 0; i--) {
  279. if (max_from_right < a[i]) {
  280. System.out.print(a[i] + " ");
  281. max_from_right = a[i];
  282. }
  283. }
  284. System.out.println("");
  285. }
  286.  
  287. // Count inversions in an array
  288. private static void problem11(int[] a) {
  289.  
  290. int n = a.length;
  291. int inv = 0;
  292. for(int i = 0; i < n-1 ; i++) {
  293. for(int j = i+1; j < n ; j++) {
  294. if(a[i] > a[j]) {
  295. inv++;
  296. System.out.println("Inversion# " + inv + " is " + a[i] + " , " + a[j] + ".");
  297. }
  298. }
  299. }
  300. }
  301.  
  302. // Find the smallest and second smallest element in the array
  303. private static void problem12(int[] a) {
  304. int n = a.length - 1;
  305. int one = a[0];
  306. int two = a[1];
  307.  
  308. for(int i = 2; i<=n; i++) {
  309. if(a[i] < one) {
  310. two = one;
  311. one = a[i];
  312. }
  313. else if(a[i] < two && a[i] != one) two = a[i];
  314. }
  315. System.out.println("The two smallest numbers are: " + one + " and " + two);
  316. }
  317.  
  318. // Check for majority element in an array
  319. private static void problem16(int[] a, int num) {
  320. int n = a.length - 1, i = 0, count = 0;
  321. while(a[i] != num) i++;
  322. for(int j = i; j <=n; j++) {
  323. if (a[j] == num) count++;
  324. }
  325. if (count > n/2) System.out.println("The majority element is: " + num);
  326. else System.out.println("There is no majority element.");
  327. }
  328.  
  329. // Find max and min of an array using minimum comparisons
  330. private static void problem17(int[] a) {
  331. int n = a.length;
  332. int min = 0, max = 0;
  333. if (n == 1) {
  334. System.out.println("The min and max elements are: " + a[0] + " and " + a[0]);
  335. return;
  336. }
  337. if (a[0] > a[1]) {
  338. max = a[0];
  339. min = a[1];
  340. }
  341. else {
  342. max = a[1];
  343. min = a[0];
  344. }
  345.  
  346. for(int i = 2; i<n ; i++) {
  347. if (a[i] > max) max = a[i];
  348. else if (a[i] < min) min = a[i];
  349. }
  350. System.out.println("The min and max elements are: " + min + " and " + max);
  351. return;
  352. }
  353.  
  354. // Seggregate 0s and 1s in an array
  355. private static void problem18(int[] a) {
  356. printArray(a);
  357. int n = a.length;
  358. int count0 = 0, count1 = 0;
  359. for(int i = 0; i < n; i++) {
  360. if(a[i] == 0) count0++;
  361. else count1++;
  362. }
  363. System.out.println("The counts of 0 and 1 are: " + count0 + " and " + count1);
  364. int index;
  365. for(index = 0; index <= count0; index++) a[index] = 0;
  366. for(index = count0; index < n; index++) a[index] = 1;
  367. printArray(a);
  368. }
  369.  
  370. // k smallest and largest elements in an array
  371. private static void problem19(int[] a, int k) {
  372. printArray(a);
  373. mergeSort(a, 0, a.length-1);
  374. printArray(a);
  375.  
  376. System.out.print("The " + k + " smallest elements are: ");
  377. for(int i = 0;i < k; i++) System.out.print(a[i] + " ");
  378. System.out.println("");
  379. System.out.print("The " + k + " largest elements are: ");
  380. for(int i = a.length - k;i < a.length; i++) System.out.print(a[i] + " ");
  381.  
  382. System.out.println("");
  383. }
  384.  
  385. private static void mergeSort(int[]a, int l, int r) {
  386. if (l < r) {
  387. int m = (l + r)/2;
  388. mergeSort(a,l,m);
  389. mergeSort(a,m+1,r);
  390. merge(a,l,m,r);
  391. }
  392. }
  393.  
  394. private static void merge(int[]a, int l, int m, int r) {
  395. int i,j,k;
  396. int n1 = m-l+1;
  397. int n2 = r-m;
  398.  
  399. // create temp arrays
  400. int L[] = new int[n1];
  401. int R[] = new int[n2];
  402.  
  403. // copy data to temp arrays.
  404. for(i=0;i<n1;i++) L[i] = a[l+i];
  405. for(j=0;j<n2;j++) R[j] = a[m+1+j];
  406.  
  407. // merge temp arrays back to a.
  408. i = 0;j = 0;k = l;
  409.  
  410. while(i < n1 && j < n2) {
  411. if(L[i] <= R[j]) {
  412. a[k] = L[i];
  413. i++;
  414. }
  415. else {
  416. a[k] = R[j];
  417. j++;
  418. }
  419. k++;
  420. }
  421.  
  422. // copy remaining elements of L[]
  423. while(i < n1) {
  424. a[k] = L[i];
  425. i++;
  426. k++;
  427. }
  428. // copy remaining elements of R[]
  429. while(j < n2) {
  430. a[k] = R[j];
  431. j++;
  432. k++;
  433. }
  434. }
  435.  
  436. // Max difference between two elements such that larger appears after smaller
  437. private static void problem20(int[] a) {
  438. int n = a.length;
  439. int diff = a[1] - a[0];
  440. int curr_sum = diff;
  441. int max_sum = curr_sum;
  442.  
  443. for(int i = 1;i < n-1; i++) {
  444. diff = a[i+1] - a[i];
  445. if(curr_sum > 0) curr_sum += diff;
  446. else curr_sum = diff;
  447.  
  448. if(curr_sum > max_sum) max_sum = curr_sum;
  449. }
  450. System.out.println("The maximum difference is: " + max_sum);
  451. }
  452.  
  453. // Floor and Ceiling in a sorted array for the given number
  454. private static void problem21(int[] arr, int num) {
  455. if(arr[0] > num) {
  456. System.out.println("Floor is missing. The Ceiling is: " + arr[0]);
  457. return;
  458. }
  459. if(arr[arr.length - 1] < num) {
  460. System.out.println("Ceiling is missing. The Floor is: " + arr[arr.length - 1]);
  461. return;
  462. }
  463. int index = 0;
  464. while(arr[index] < num) index++;
  465. System.out.println("The number " + num + " has a floor of " + arr[index-1] + " and a ceiling of " + arr[index]);
  466. }
  467.  
  468. // Seggregate odd and even numbers
  469. private static void problem22(int[] a) {
  470. printArray(a);
  471. HashMap<Integer, Integer> odds = new HashMap<Integer, Integer>();
  472. HashMap<Integer, Integer> evens = new HashMap<Integer, Integer>();
  473.  
  474. for(int i = 0; i <= a.length-1; i++)
  475. if(a[i]%2 == 0) evens.put(a[i],1);
  476. else odds.put(a[i],1);
  477.  
  478. int index = 0;
  479. for(Integer aa: odds.keySet()) a[index++] = aa;
  480. for(Integer aa: evens.keySet()) a[index++] = aa;
  481. printArray(a);
  482. }
  483.  
  484. // Find out two repeating elements in a given array
  485. private static void problem23(int[] a) {
  486. int size = a.length;
  487. HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
  488.  
  489. for(int i = 0; i < a.length; i++) {
  490. if(!counts.containsKey(a[i])) counts.put(a[i],1);
  491. else counts.put(a[i], counts.get(a[i]) + 1);
  492. }
  493. System.out.print("The repeating elements are: ");
  494. for(Integer aa: counts.keySet()) {
  495. if (counts.get(aa) == 2) {
  496. System.out.print(aa + " ");
  497. }
  498. }
  499. System.out.println("");
  500. }
  501.  
  502. // Find missing numbers in an array
  503. private static void problem24(int[] nums) {
  504. printArray(nums);
  505. ArrayList<Integer> ret = new ArrayList<Integer>();
  506. for(int i = 0; i < nums.length; i++) {
  507. int val = Math.abs(nums[i]) - 1;
  508. if(nums[val] > 0) {
  509. nums[val] = -nums[val];
  510. }
  511. }
  512. printArray(nums);
  513. for(int i = 0; i < nums.length; i++) {
  514. if(nums[i] > 0) {
  515. ret.add(i+1);
  516. }
  517. }
  518. System.out.println("The missing numbers are: " + ret.toString());
  519. }
  520. // Determine if a number is a perfect square.
  521. private static boolean IsPerfectSquare(int n) {
  522. for(int i=1; i*i<=n; i++)
  523. {
  524. if(i*i == n) return true;
  525. }
  526. return false;
  527. }
  528.  
  529. // Find Sum of Number in the String
  530. private static void getSumofDigits(String ss){
  531.  
  532. int i = 0;
  533. int j = 0 ;
  534. int len = ss.length() ;
  535. int currentNum = 0;
  536. int sum = 0 ;
  537.  
  538. while(i<len){
  539. if(Character.isDigit(ss.charAt(i))) {
  540. j=i;
  541. currentNum = 0;
  542. while(j < len && Character.isDigit(ss.charAt(j))){
  543. currentNum = currentNum *10 + ss.charAt(j) - 48;
  544. j++;
  545. i++;
  546. }
  547. sum = sum + currentNum ;
  548. }
  549. i++;
  550. }
  551. System.out.println(sum) ;
  552. }
  553.  
  554. // Separate 0s, 1s and 2s
  555. private static void problem25(int[] arr) {
  556. printArray(arr);
  557. int n = arr.length;
  558. int count0 = 0, count1 = 0;
  559. for(int i = 0; i < n; i++) {
  560. if(arr[i] == 0) count0++;
  561. else if(arr[i] == 1) count1++;
  562. }
  563. for(int i = 0;i < count0;i++) arr[i] = 0;
  564. for(int j = count0;j < count0+count1;j++) arr[j] = 1;
  565. for(int k = count0+count1; k < n; k++) arr[k] = 2;
  566. printArray(arr);
  567. }
  568.  
  569. // Find duplicates in O(n) time.
  570. private static void problem26(int[] arr) {
  571. printArray(arr);
  572. HashMap<Integer,Integer> visited = new HashMap<Integer,Integer>();
  573.  
  574. for(int i=0;i<arr.length;i++) {
  575. if(!visited.containsKey(arr[i])) visited.put(arr[i], 1);
  576. else {
  577. visited.put(arr[i], visited.get(arr[i] + 1));
  578. System.out.print(arr[i] + " ");
  579. }
  580. }
  581. System.out.println("");
  582. }
  583.  
  584. // Find smallest missing element in a sorted array
  585. private static void problem27(int[] arr) {
  586. printArray(arr);
  587. if(arr[0] != 0) {
  588. System.out.println("The missing number is 0.");
  589. return;
  590. }
  591. for(int i=1;i<arr.length;i++) {
  592. if(arr[i] != i) {
  593. System.out.println("The missing number is: " + i);
  594. return;
  595. }
  596. else continue;
  597. }
  598. }
  599.  
  600. // Get first and last occurance of a number
  601. private static int first(int[] arr, int low, int high, int x, int n) {
  602. if(high >= low) {
  603. int mid = (high+low)/2;
  604. if(mid==0 || x>arr[mid-1] && arr[mid] == x) return mid;
  605. else if(x> arr[mid]) return first(arr, mid+1, high, x,n);
  606. else return first(arr, low, mid-1,x,n);
  607. }
  608. return -1;
  609. }
  610.  
  611. private static int last(int[] arr, int low, int high, int x, int n) {
  612. if(high>=low) {
  613. int mid = (high+low)/2;
  614. if(mid==n-1 || x<arr[mid+1] && arr[mid] == x) return mid;
  615. else if(x<arr[mid]) return last(arr, low, mid-1,x,n);
  616. else return last(arr, mid+1, high, x,n);
  617. }
  618. return -1;
  619. }
  620.  
  621. private static void problem28(int[] arr, int num) {
  622. int i, j;
  623. int n = arr.length;
  624. i = first(arr,0, n-1,num, n);
  625.  
  626. if(i== -1) {
  627. System.out.println("The number " + num + "does not exist in the array.");
  628. return;
  629. }
  630. j = last(arr,i, n-1,num,n);
  631. System.out.println(num + " exists " + (j-i+1) + " times.");
  632. }
  633.  
  634. // Given an array find max j-i such that arr[j] > arr[i]
  635. private static void problem29(int[] arr) {
  636. printArray(arr);
  637. int max = -1;
  638. int n = arr.length;
  639. for(int i = 0;i<n;i++) {
  640. for(int j=i+1;j<n;j++) {
  641. if(arr[j] > arr[i]) {
  642. if(arr[j] - arr[i] > max) max = arr[j] - arr[i];
  643. }
  644. }
  645. }
  646. System.out.println("The max j-i difference is: " + max);
  647. }
  648.  
  649. // Given an array of size n and a number k, find all elements that appear more than n/k times
  650. private static void problem30(int[] arr, int k) {
  651. int n = arr.length;
  652. HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
  653. for(int i = 0;i<n;i++) {
  654. if(freq.containsKey(arr[i])) freq.put(arr[i], freq.get(arr[i])+1);
  655. else freq.put(arr[i],1);
  656.  
  657. Set set = freq.entrySet();
  658. Iterator it = set.iterator();
  659. while(it.hasNext()) {
  660. Map.Entry me = (Map.Entry) it.next();
  661. if((Integer) me.getValue() > (n/k)) {
  662. System.out.println(me.getKey() + " has a count of " + me.getValue());
  663. }
  664. }
  665. }
  666. }
  667.  
  668. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement