├── Array 1 PPT Assignment ├── TwoSum.java ├── SearchInsertPosition.class ├── ContainsDuplicate.java ├── RemoveElement.java ├── MoveZeroes.java ├── SearchInsertPosition.java ├── PlusOne.java ├── FindErrorNums.java └── MergeSortedArray.java └── README.md /Array 1 PPT Assignment/TwoSum.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PPT-Program-By-PW-Array-1 -------------------------------------------------------------------------------- /Array 1 PPT Assignment/SearchInsertPosition.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vinaykumarmahato/PPT-Program-By-PW-Array-1/HEAD/Array 1 PPT Assignment/SearchInsertPosition.class -------------------------------------------------------------------------------- /Array 1 PPT Assignment/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | public class ContainsDuplicate { 4 | public boolean containsDuplicate(int[] nums) { 5 | HashSet set = new HashSet<>(); 6 | 7 | for (int num : nums) { 8 | if (set.contains(num)) { 9 | return true; 10 | } 11 | set.add(num); 12 | } 13 | 14 | return false; 15 | } 16 | 17 | public static void main(String[] args) { 18 | int[] nums = {1, 2, 3, 1}; 19 | 20 | ContainsDuplicate containsDuplicate = new ContainsDuplicate(); 21 | boolean result = containsDuplicate.containsDuplicate(nums); 22 | 23 | System.out.println("Contains duplicate? " + result); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/RemoveElement.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class RemoveElement { 4 | public int removeElement(int[] nums, int val) { 5 | int k = 0; // Variable to keep track of the number of elements not equal to val 6 | 7 | for (int i = 0; i < nums.length; i++) { 8 | if (nums[i] != val) { 9 | nums[k] = nums[i]; 10 | k++; 11 | } 12 | } 13 | 14 | return k; 15 | } 16 | 17 | public static void main(String[] args) { 18 | int[] nums = {3, 2, 2, 3}; 19 | int val = 3; 20 | 21 | RemoveElement removeElement = new RemoveElement(); 22 | int k = removeElement.removeElement(nums, val); 23 | 24 | System.out.println("k = " + k + ", nums = " + Arrays.toString(nums)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/MoveZeroes.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class MoveZeroes { 4 | public void moveZeroes(int[] nums) { 5 | int i = 0; // Pointer to track the position of the next non-zero element 6 | 7 | // Traverse the array 8 | for (int j = 0; j < nums.length; j++) { 9 | if (nums[j] != 0) { 10 | nums[i] = nums[j]; 11 | i++; 12 | } 13 | } 14 | 15 | // Fill the remaining positions with zeros 16 | while (i < nums.length) { 17 | nums[i] = 0; 18 | i++; 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | int[] nums = {0, 1, 0, 3, 12}; 24 | 25 | MoveZeroes moveZeroes = new MoveZeroes(); 26 | moveZeroes.moveZeroes(nums); 27 | 28 | System.out.println(Arrays.toString(nums)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | public class SearchInsertPosition { 2 | public int searchInsert(int[] nums, int target) { 3 | int left = 0; 4 | int right = nums.length - 1; 5 | 6 | while (left <= right) { 7 | int mid = left + (right - left) / 2; 8 | 9 | if (nums[mid] == target) { 10 | return mid; 11 | } else if (nums[mid] < target) { 12 | left = mid + 1; 13 | } else { 14 | right = mid - 1; 15 | } 16 | } 17 | 18 | return left; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int[] nums = {1, 3, 5, 6}; 23 | int target = 5; 24 | 25 | SearchInsertPosition searchInsertPosition = new SearchInsertPosition(); 26 | int index = searchInsertPosition.searchInsert(nums, target); 27 | 28 | System.out.println("Index: " + index); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/PlusOne.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class PlusOne { 4 | public int[] plusOne(int[] digits) { 5 | int n = digits.length; 6 | 7 | // Iterate through the digits in reverse order 8 | for (int i = n - 1; i >= 0; i--) { 9 | if (digits[i] < 9) { 10 | digits[i]++; 11 | return digits; 12 | } else { 13 | digits[i] = 0; 14 | } 15 | } 16 | 17 | // If all digits are 9, create a new array with one additional digit 18 | int[] newDigits = new int[n + 1]; 19 | newDigits[0] = 1; 20 | 21 | return newDigits; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int[] digits = {1, 2, 3}; 26 | 27 | PlusOne plusOne = new PlusOne(); 28 | int[] result = plusOne.plusOne(digits); 29 | 30 | System.out.println(Arrays.toString(result)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/FindErrorNums.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | public class FindErrorNums { 4 | public int[] findErrorNums(int[] nums) { 5 | int n = nums.length; 6 | int duplicate = -1; 7 | int missing = -1; 8 | 9 | HashSet set = new HashSet<>(); 10 | for (int num : nums) { 11 | if (set.contains(num)) { 12 | duplicate = num; 13 | } else { 14 | set.add(num); 15 | } 16 | } 17 | 18 | for (int i = 1; i <= n; i++) { 19 | if (!set.contains(i)) { 20 | missing = i; 21 | break; 22 | } 23 | } 24 | 25 | return new int[]{duplicate, missing}; 26 | } 27 | 28 | public static void main(String[] args) { 29 | int[] nums = {1, 2, 2, 4}; 30 | 31 | FindErrorNums findErrorNums = new FindErrorNums(); 32 | int[] result = findErrorNums.findErrorNums(nums); 33 | 34 | System.out.println("Duplicate: " + result[0] + ", Missing: " + result[1]); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Array 1 PPT Assignment/MergeSortedArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class MergeSortedArray { 4 | public void merge(int[] nums1, int m, int[] nums2, int n) { 5 | int i = m - 1; // Pointer for nums1 6 | int j = n - 1; // Pointer for nums2 7 | int k = m + n - 1; // Pointer for the merged array 8 | 9 | // Merge nums1 and nums2 from the end 10 | while (i >= 0 && j >= 0) { 11 | if (nums1[i] > nums2[j]) { 12 | nums1[k] = nums1[i]; 13 | i--; 14 | } else { 15 | nums1[k] = nums2[j]; 16 | j--; 17 | } 18 | k--; 19 | } 20 | 21 | // Copy the remaining elements from nums2 to nums1 if any 22 | while (j >= 0) { 23 | nums1[k] = nums2[j]; 24 | j--; 25 | k--; 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | int[] nums1 = {1, 2, 3, 0, 0, 0}; 31 | int m = 3; 32 | int[] nums2 = {2, 5, 6}; 33 | int n = 3; 34 | 35 | MergeSortedArray mergeSortedArray = new MergeSortedArray(); 36 | mergeSortedArray.merge(nums1, m, nums2, n); 37 | 38 | System.out.println(Arrays.toString(nums1)); 39 | } 40 | } 41 | --------------------------------------------------------------------------------