Advertisement
chayanforyou

Problem Solving

Aug 21st, 2021 (edited)
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                                 Array Insert
  4.  
  5. *******************************************************************************/
  6.  
  7. import java.io.*;
  8. import java.lang.*;
  9. import java.util.*;
  10.  
  11. public class Main {
  12.  
  13.     // Function to insert x in arr at position pos
  14.     public static int[] insertX(int n, int arr[], int x, int pos) {
  15.        
  16.         int i;
  17.  
  18.         // create a new array of size n+1
  19.         int newarr[] = new int[n + 1];
  20.  
  21.         // insert the elements from
  22.         // the old array into the new array
  23.         // insert all elements till pos
  24.         // then insert x at pos
  25.         // then insert rest of the elements
  26.         for (i = 0; i < n + 1; i++) {
  27.             if (i < pos - 1)
  28.                 newarr[i] = arr[i];
  29.             else if (i == pos - 1)
  30.                 newarr[i] = x;
  31.             else
  32.                 newarr[i] = arr[i - 1];
  33.         }
  34.         return newarr;
  35.     }
  36.  
  37.     // Driver code
  38.     public static void main(String[] args) {
  39.  
  40.         int n = 10;
  41.         int i;
  42.  
  43.         // initial array of size 10
  44.         int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  45.  
  46.         // print the original array
  47.         System.out.println("Initial Array:\n" + Arrays.toString(arr));
  48.  
  49.         // element to be inserted
  50.         int x = 50;
  51.  
  52.         // position at which element
  53.         // is to be inserted
  54.         int pos = 5;
  55.  
  56.         // call the method to insert x
  57.         // in arr at position pos
  58.         arr = insertX(n, arr, x, pos);
  59.  
  60.         // print the updated array
  61.         System.out.println("\nArray with " + x
  62.                            + " inserted at position "
  63.                            + pos + ":\n"
  64.                            + Arrays.toString(arr));
  65.     }
  66. }
  67.  
  68. /******************************************************************************
  69.  
  70.                                 Binary Search
  71.  
  72. *******************************************************************************/
  73.  
  74. // Java implementation of recursive Binary Search
  75. public class Main {
  76.    
  77.     // Returns index of x if it is present in arr[l..
  78.     // r], else return -1
  79.     static int binarySearch(int arr[], int n, int x)
  80.     {
  81.         int left = 0;
  82.         int right = n - 1;
  83.        
  84.         while (left <= right) {
  85.             int mid = (left + right) / 2;
  86.  
  87.             // If the element is present at the
  88.             // middle itself
  89.             if (arr[mid] == x)
  90.                 return mid;
  91.  
  92.             if (arr[mid] < x) {
  93.                 left  = mid + 1;
  94.             } else {
  95.                 right = mid - 1;
  96.             }
  97.         }
  98.  
  99.         // We reach here when element is not present
  100.         // in array
  101.         return -1;
  102.     }
  103.  
  104.     // Driver method to test above
  105.     public static void main(String args[]) {
  106.        
  107.         int arr[] = { 2, 3, 4, 10, 40 };
  108.         int n = arr.length;
  109.         int x = 10;
  110.         int result = binarySearch(arr, n, x);
  111.         if (result == -1)
  112.             System.out.println("Element not present");
  113.         else
  114.             System.out.println("Element found at index: " + result);
  115.     }
  116. }
  117.  
  118. /******************************************************************************
  119.  
  120.                             Remove Duplicate Elements
  121.  
  122. *******************************************************************************/
  123.  
  124. // Method 1
  125. import java.util.HashMap;
  126.  
  127. class Main {
  128.    
  129.     static void removeDups(int[] a, int n) {
  130.  
  131.         // Hash map which will store the
  132.         // elements which has appeared previously.
  133.         HashMap<Integer, Boolean> mp = new HashMap<>();
  134.  
  135.         for (int i = 0; i < n; ++i) {
  136.  
  137.             // Print the element if it is not
  138.             // present there in the hash map
  139.             // and Insert the element in the hash map
  140.             if (mp.get(a[i]) == null)
  141.             {
  142.                 System.out.print(a[i] + " ");
  143.                 mp.put(a[i], true);
  144.             }
  145.         }
  146.     }
  147.  
  148.     // Driver Code
  149.     public static void main(String[] args) {
  150.        
  151.         int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
  152.        
  153.         int n = arr.length;
  154.         removeDups(arr, n);
  155.     }
  156. }
  157.  
  158. // Method 2
  159. public class Main {
  160.     public static int removeDuplicates(int a[], int n)
  161.     {
  162.         // if(array size if 0 or 1 array is already sorted)
  163.         if (n == 0 || n == 1) {
  164.             return n;
  165.         }
  166.  
  167.         int j = 0;
  168.  
  169.         // check if the ith element is not equal to
  170.         // the (i+1)th element, then add that element
  171.         // at the jth index in the same array
  172.         // which indicates that te particular element
  173.         // will only be added once in the array
  174.         for (int i = 0; i < n - 1; i++) {
  175.             if (a[i] != a[i + 1]) {
  176.                 a[j++] = a[i];
  177.             }
  178.         }
  179.  
  180.         a[j++] = a[n - 1];
  181.  
  182.         return j;
  183.     }
  184.  
  185.     public static void main(String[] args)
  186.     {
  187.         int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6 };
  188.  
  189.         int n = a.length;
  190.          
  191.         int j=0;
  192.        
  193.         // the function will modify the array a[]
  194.         // such that the starting j elements
  195.         // will be having all unique elements
  196.         // and no element will be appearing more than
  197.         // once
  198.         j = removeDuplicates(a, n);
  199.  
  200.         // printing array elements
  201.         for (int i = 0; i < j; i++)
  202.             System.out.print(a[i] + " ");
  203.     }
  204. }
  205.  
  206. /******************************************************************************
  207.  
  208.                                 Find Duplicates
  209.  
  210. *******************************************************************************/
  211.  
  212. // Method 1
  213. class FindDuplicate {
  214.     // Function to print duplicates
  215.     void printRepeating(int arr[], int size)
  216.     {
  217.         int i;
  218.         System.out.println("The repeating elements are : ");
  219.          
  220.         for (i = 0; i < size; i++) {
  221.             int j = Math.abs(arr[i]);
  222.             if (arr[j] >= 0)
  223.                 arr[j] = -arr[j];
  224.             else
  225.                 System.out.print(j + " ");
  226.         }
  227.     }
  228.  
  229.     // Driver code
  230.     public static void main(String[] args)
  231.     {
  232.         FindDuplicate duplicate = new FindDuplicate();
  233.         int arr[] = { 1, 2, 3, 1, 3, 6, 6 };
  234.         int arr_size = arr.length;
  235.  
  236.         duplicate.printRepeating(arr, arr_size);
  237.     }
  238. }
  239.  
  240. /******************************************************************************
  241.  
  242.                                 Find Two Sum
  243.  
  244. *******************************************************************************/
  245.  
  246. import java.util.*;
  247.  
  248. public class Main {
  249.    
  250.     // Time complexity: O(n)
  251.     private static int[] findTwoSum(int[] nums, int target) {
  252.        
  253.         Map<Integer, Integer> numMap = new HashMap<>();
  254.         for (int i = 0; i < nums.length; i++) {
  255.             int complement = target - nums[i];
  256.             if (numMap.containsKey(complement)) {
  257.                 return new int[] { numMap.get(complement), i };
  258.             } else {
  259.                 numMap.put(nums[i], i);
  260.             }
  261.         }
  262.         return new int[] {};
  263.     }
  264.    
  265.     public static void main(String[] args) {
  266.        
  267.         int[] nums = {2, 7, 11, 15};
  268.         int target = 9;
  269.        
  270.         int[] sum = findTwoSum(nums, target);
  271.        
  272.         // Printing The array elements
  273.         for (int i = 0; i < sum.length; i++)
  274.             System.out.print(sum[i] + " ");
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement