├── BinarySearch.java ├── BubbleSort.java ├── BuyAndSellStock.class ├── FastExponentiation.class ├── LinkedList.java ├── Question1.java ├── README.md └── SpiralMatrix.java /BinarySearch.java: -------------------------------------------------------------------------------- 1 | //Question : Binary Search in an Array 2 | package Arrays; 3 | 4 | public class BinarySearch { 5 | public static int binSearch(int arr[], int key) { 6 | int start = 0; 7 | int end = arr.length-1; 8 | 9 | while(start <= end) { 10 | int mid = (end + start)/2; 11 | //case 1 12 | if(arr[mid] == key) { 13 | return mid; 14 | } 15 | else if(arr[mid] < key) { 16 | start = mid + 1; 17 | } 18 | else { 19 | end = mid-1; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | public static void main(String args[]) { 26 | int arr[] = {1, 2, 3, 4, 5, 6}; 27 | int idx = binSearch(arr, 4); 28 | System.out.println("index is : " + idx); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /BubbleSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | //Problem : Bubble Sort 4 | 5 | public class BubbleSort { 6 | public static void bubbleSort(int arr[]) { 7 | for(int turn=0; turn arr[j+1]) { 10 | //swap 11 | int temp = arr[j]; 12 | arr[j] = arr[j+1]; 13 | arr[j+1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | 19 | public static void modifiedBubbleSort(int arr[]) { 20 | for(int turn=0; turn arr[j+1]) { 24 | //swap 25 | int temp = arr[j]; 26 | arr[j] = arr[j+1]; 27 | arr[j+1] = temp; 28 | swapped = true; 29 | } 30 | } 31 | if(swapped == false) { 32 | break; 33 | } 34 | } 35 | } 36 | 37 | public static void bubbleSortDescending(int arr[]) { 38 | for(int turn=0; turn"); 45 | temp = temp.next; 46 | } 47 | System.out.println("null"); 48 | } 49 | 50 | public void add(int idx, int data) { 51 | if(idx == 0) { 52 | addFirst(data); 53 | return; 54 | } 55 | Node newNode = new Node(data); 56 | size++; 57 | Node temp = head; 58 | int i = 0; 59 | 60 | while(i < idx-1) { 61 | temp = temp.next; 62 | i++; 63 | } 64 | 65 | //i = idx-1; temp -> prev 66 | newNode.next = temp.next; 67 | temp.next = newNode; 68 | } 69 | 70 | public int removeFirst() { 71 | if(size == 0) { 72 | System.out.println("LL is empty"); 73 | return Integer.MIN_VALUE; 74 | } else if(size == 1) { 75 | int val = head.data; 76 | head = tail = null; 77 | size = 0; 78 | return val; 79 | } 80 | int val = head.data; 81 | head = head.next; 82 | size--; 83 | return val; 84 | } 85 | 86 | public int removeLast() { 87 | if(size == 0) { 88 | System.out.println("LL is empty"); 89 | return Integer.MIN_VALUE; 90 | } else if(size == 1) { 91 | int val = head.data; 92 | head = tail = null; 93 | size = 0; 94 | return val; 95 | } 96 | //prev : i = size-2 97 | Node prev = head; 98 | for(int i=0; i last.next = null 273 | prev.next = null; 274 | } 275 | 276 | private Node getMid(Node head) { 277 | Node slow = head; 278 | Node fast = head.next; 279 | 280 | while(fast != null && fast.next != null) { 281 | slow = slow.next; 282 | fast = fast.next.next; 283 | } 284 | return slow; //mid node 285 | } 286 | 287 | private Node merge(Node head1, Node head2) { 288 | Node mergedLL = new Node(-1); 289 | Node temp = mergedLL; 290 | 291 | while(head1 != null && head2 != null) { 292 | if(head1.data <= head2.data) { 293 | temp.next = head1; 294 | head1 = head1.next; 295 | temp = temp.next; 296 | } else { 297 | temp.next = head2; 298 | head2 = head2.next; 299 | temp = temp.next; 300 | } 301 | } 302 | 303 | while(head1 != null) { 304 | temp.next = head1; 305 | head1 = head1.next; 306 | temp = temp.next; 307 | } 308 | 309 | while(head2 != null) { 310 | temp.next = head2; 311 | head2 = head2.next; 312 | temp = temp.next; 313 | } 314 | 315 | return mergedLL.next; 316 | } 317 | 318 | public Node mergeSort(Node head) { 319 | if(head == null || head.next == null) { 320 | return head; 321 | } 322 | 323 | //find mid 324 | Node mid = getMid(head); 325 | //left & right MS 326 | Node rightHead = mid.next; 327 | mid.next = null; 328 | Node newLeft = mergeSort(head); 329 | Node newRight = mergeSort(rightHead); 330 | 331 | //merge 332 | return merge(newLeft, newRight); 333 | } 334 | 335 | public void zigZag() { 336 | //find mid 337 | Node slow = head; 338 | Node fast = head.next; 339 | while(fast != null && fast.next != null) { 340 | slow = slow.next; 341 | fast = fast.next.next; 342 | } 343 | Node mid = slow; 344 | 345 | //reverse 2nd half 346 | Node curr = mid.next; 347 | mid.next = null; 348 | Node prev = null; 349 | Node next; 350 | 351 | while(curr != null) { 352 | next = curr.next; 353 | curr.next = prev; 354 | prev = curr; 355 | curr = next; 356 | } 357 | 358 | Node left = head; 359 | Node right = prev; 360 | Node nextL, nextR; 361 | 362 | //alt merge - zig-zag merge 363 | while(left != null && right != null) { 364 | nextL = left.next; 365 | left.next = right; 366 | nextR = right.next; 367 | right.next = nextL; 368 | 369 | left = nextL; 370 | right = nextR; 371 | } 372 | } 373 | public static void main(String args[]) { 374 | LinkedList ll = new LinkedList(); 375 | ll.addLast(1); 376 | ll.addLast(2); 377 | ll.addLast(3); 378 | ll.addLast(4); 379 | ll.addLast(5); 380 | ll.addLast(6); 381 | //1->2->3->4->5 382 | 383 | ll.print(); 384 | ll.zigZag(); 385 | ll.print(); 386 | } 387 | } 388 | -------------------------------------------------------------------------------- /Question1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //To push an element at the bottom of a stack 3 | public class Question1 { 4 | public static void pushAtBottom(Stack s, int data) { 5 | if(s.isEmpty()) { 6 | s.push(data); 7 | return; 8 | } 9 | 10 | int temp = s.pop(); 11 | pushAtBottom(s, data); 12 | s.push(temp); 13 | } 14 | 15 | public static void main(String args[]) { 16 | Stack stack = new Stack<>(); 17 | stack.push(1); 18 | stack.push(2); 19 | stack.push(3); 20 | pushAtBottom(stack, 4); 21 | 22 | while(!stack.isEmpty()) { 23 | System.out.println(stack.pop()); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cargame1 2 | 3 | Hacktoberfest-2023🔥 4 | ![image](https://user-images.githubusercontent.com/70385488/192114009-0830321a-d227-4a4d-8411-6c03b54d7ce6.png) 5 | 6 |
7 | 8 | 9 | ### This repository aims to help code beginners with their first successful pull request and open-source contribution. :partying_face: 10 | 11 | :star: Feel free to use this project to make your first contribution to an open-source project on GitHub. Practice making your first pull request to a public repository before doing the real thing! 12 | 13 | :star: Make sure to grab some cool swags during Hacktoberfest by getting involved in the open-source community. 14 | 15 | ### This repository is open to all members of the GitHub community. Any member can contribute to this project! The only thing which you need to keep in mind is that it should be genuine PR :grin: 16 | 17 | ## What is Hacktoberfest? :thinking: 18 | A month-long celebration from October 1st to October 31st presented by [Digital Ocean](https://hacktoberfest.digitalocean.com/) and [DEV Community](https://dev.to/) collaborated with [GitHub](https://github.com/blog/2433-celebrate-open-source-this-october-with-hacktoberfest) to get people involved in [Open Source](https://github.com/open-source). Create your very first pull request to any public repository on GitHub and contribute to the open-source developer community. 19 | 20 | [https://hacktoberfest.digitalocean.com/](https://hacktoberfest.digitalocean.com/) 21 | 22 | ## Rules :fire: 23 | To qualify for the __official limited edition Hacktoberfest shirt__, you must register [here](https://hacktoberfest.digitalocean.com/) and make four Pull Requests (PRs) between October 1-31, 2023 (in any time zone). PRs can be made to any public repo on GitHub, not only the ones with issues labeled Hacktoberfest. This year, the __first 40,000__ participants who complete the challenge will earn a T-shirt. 24 | -------------------------------------------------------------------------------- /SpiralMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | //Problem : Print a Matrix in Spiral Fashion 4 | 5 | public class SpiralMatrix { 6 | public static void printSpiral(int matrix[][]) { 7 | int startRow = 0; 8 | int startCol = 0; 9 | int endRow = matrix.length-1; 10 | int endCol = matrix[0].length-1; 11 | 12 | //print boundaries 13 | while(startRow <= endCol && startCol <= endCol) { 14 | for(int j=startCol; j<=endCol; j++) { 15 | System.out.print(matrix[startRow][j]+" "); 16 | } 17 | 18 | for(int i=startRow+1; i<=endRow; i++) { 19 | System.out.print(matrix[i][endCol]+" "); 20 | } 21 | 22 | for(int j=endCol-1; j>=startCol; j--) { 23 | if(startRow == endRow) { 24 | break; 25 | } 26 | System.out.print(matrix[endRow][j]+" "); 27 | } 28 | 29 | for(int i=endRow-1; i>startRow; i--) { 30 | if(startCol == endCol) { 31 | break; 32 | } 33 | System.out.print(matrix[i][startCol]+" "); 34 | } 35 | 36 | startCol++; startRow++; 37 | endCol--; endRow--; 38 | } 39 | 40 | System.out.println(); 41 | } 42 | 43 | public static void main(String args[]) { 44 | int matrix[][] = {{1, 2, 3, 4}, 45 | {5, 6, 7, 8}, 46 | {9, 10, 11, 12}, 47 | {13, 14, 15, 16}}; 48 | printSpiral(matrix); 49 | } 50 | } 51 | --------------------------------------------------------------------------------