├── README.md └── Array 4 PPT Assignment ├── CompleteStaircase.java ├── ArrayPairSum.java ├── MaxCount.java ├── ShuffleArray.java ├── TransposeMatrix.java ├── SortedSquares.java ├── IntersectionOfThreeArrays.java └── DistinctIntegers.java /README.md: -------------------------------------------------------------------------------- 1 | # PPT-Program-By-PW-Array-4 -------------------------------------------------------------------------------- /Array 4 PPT Assignment/CompleteStaircase.java: -------------------------------------------------------------------------------- 1 | public class CompleteStaircase { 2 | public int arrangeCoins(int n) { 3 | int rows = 0; 4 | 5 | while (n >= rows + 1) { 6 | rows++; 7 | n -= rows; 8 | } 9 | 10 | return rows; 11 | } 12 | 13 | public static void main(String[] args) { 14 | int n = 5; 15 | CompleteStaircase solution = new CompleteStaircase(); 16 | int completeRows = solution.arrangeCoins(n); 17 | System.out.println("Number of complete rows: " + completeRows); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/ArrayPairSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ArrayPairSum { 4 | public int arrayPairSum(int[] nums) { 5 | Arrays.sort(nums); 6 | 7 | int sum = 0; 8 | for (int i = 0; i < nums.length; i += 2) { 9 | sum += nums[i]; 10 | } 11 | 12 | return sum; 13 | } 14 | 15 | public static void main(String[] args) { 16 | int[] nums = {1, 4, 3, 2}; 17 | ArrayPairSum solution = new ArrayPairSum(); 18 | int maxSum = solution.arrayPairSum(nums); 19 | System.out.println("Maximized sum: " + maxSum); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/MaxCount.java: -------------------------------------------------------------------------------- 1 | public class MaxCount { 2 | public int maxCount(int m, int n, int[][] ops) { 3 | int minRow = m; 4 | int minCol = n; 5 | 6 | for (int[] op : ops) { 7 | minRow = Math.min(minRow, op[0]); 8 | minCol = Math.min(minCol, op[1]); 9 | } 10 | 11 | return minRow * minCol; 12 | } 13 | 14 | public static void main(String[] args) { 15 | int m = 3; 16 | int n = 3; 17 | int[][] ops = {{2, 2}, {3, 3}}; 18 | MaxCount solution = new MaxCount(); 19 | int maxIntegers = solution.maxCount(m, n, ops); 20 | System.out.println("Number of maximum integers: " + maxIntegers); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/ShuffleArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ShuffleArray { 4 | public int[] shuffle(int[] nums, int n) { 5 | int[] result = new int[nums.length]; 6 | int p1 = 0; 7 | int p2 = n; 8 | 9 | for (int i = 0; i < nums.length; i += 2) { 10 | result[i] = nums[p1]; 11 | result[i + 1] = nums[p2]; 12 | p1++; 13 | p2++; 14 | } 15 | 16 | return result; 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] nums = {2, 5, 1, 3, 4, 7}; 21 | int n = 3; 22 | ShuffleArray solution = new ShuffleArray(); 23 | int[] shuffledArray = solution.shuffle(nums, n); 24 | System.out.println(Arrays.toString(shuffledArray)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/TransposeMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class TransposeMatrix { 4 | public int[][] transpose(int[][] matrix) { 5 | int rows = matrix.length; 6 | int cols = matrix[0].length; 7 | int[][] transposed = new int[cols][rows]; 8 | 9 | for (int i = 0; i < rows; i++) { 10 | for (int j = 0; j < cols; j++) { 11 | transposed[j][i] = matrix[i][j]; 12 | } 13 | } 14 | 15 | return transposed; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 20 | TransposeMatrix solution = new TransposeMatrix(); 21 | int[][] transposedMatrix = solution.transpose(matrix); 22 | for (int[] row : transposedMatrix) { 23 | System.out.println(Arrays.toString(row)); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/SortedSquares.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class SortedSquares { 4 | public int[] sortedSquares(int[] nums) { 5 | int[] result = new int[nums.length]; 6 | int left = 0; 7 | int right = nums.length - 1; 8 | 9 | for (int i = nums.length - 1; i >= 0; i--) { 10 | int square; 11 | if (Math.abs(nums[left]) > Math.abs(nums[right])) { 12 | square = nums[left] * nums[left]; 13 | left++; 14 | } else { 15 | square = nums[right] * nums[right]; 16 | right--; 17 | } 18 | result[i] = square; 19 | } 20 | 21 | return result; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int[] nums = {-4, -1, 0, 3, 10}; 26 | SortedSquares solution = new SortedSquares(); 27 | int[] sortedSquares = solution.sortedSquares(nums); 28 | System.out.println(Arrays.toString(sortedSquares)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/IntersectionOfThreeArrays.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class IntersectionOfThreeArrays { 5 | public List arraysIntersection(int[] arr1, int[] arr2, int[] arr3) { 6 | List result = new ArrayList<>(); 7 | int p1 = 0; 8 | int p2 = 0; 9 | int p3 = 0; 10 | 11 | while (p1 < arr1.length && p2 < arr2.length && p3 < arr3.length) { 12 | if (arr1[p1] == arr2[p2] && arr2[p2] == arr3[p3]) { 13 | result.add(arr1[p1]); 14 | p1++; 15 | p2++; 16 | p3++; 17 | } else if (arr1[p1] < arr2[p2]) { 18 | p1++; 19 | } else if (arr2[p2] < arr3[p3]) { 20 | p2++; 21 | } else { 22 | p3++; 23 | } 24 | } 25 | 26 | return result; 27 | } 28 | 29 | public static void main(String[] args) { 30 | int[] arr1 = {1, 2, 3, 4, 5}; 31 | int[] arr2 = {1, 2, 5, 7, 9}; 32 | int[] arr3 = {1, 3, 4, 5, 8}; 33 | IntersectionOfThreeArrays solution = new IntersectionOfThreeArrays(); 34 | List intersection = solution.arraysIntersection(arr1, arr2, arr3); 35 | System.out.println(intersection); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Array 4 PPT Assignment/DistinctIntegers.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashSet; 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | public class DistinctIntegers { 7 | public List> findDistinct(int[] nums1, int[] nums2) { 8 | Set set1 = new HashSet<>(); 9 | Set set2 = new HashSet<>(); 10 | 11 | for (int num : nums1) { 12 | set1.add(num); 13 | } 14 | 15 | for (int num : nums2) { 16 | set2.add(num); 17 | } 18 | 19 | Set symmetricDifference = new HashSet<>(set1); 20 | symmetricDifference.addAll(set2); 21 | Set intersection = new HashSet<>(set1); 22 | intersection.retainAll(set2); 23 | 24 | List result1 = new ArrayList<>(symmetricDifference); 25 | result1.removeAll(intersection); 26 | 27 | List result2 = new ArrayList<>(symmetricDifference); 28 | result2.removeAll(set1); 29 | 30 | List> result = new ArrayList<>(); 31 | result.add(result1); 32 | result.add(result2); 33 | 34 | return result; 35 | } 36 | 37 | public static void main(String[] args) { 38 | int[] nums1 = {1, 2, 3}; 39 | int[] nums2 = {2, 4, 6}; 40 | DistinctIntegers solution = new DistinctIntegers(); 41 | List> distinctIntegers = solution.findDistinct(nums1, nums2); 42 | System.out.println(distinctIntegers); 43 | } 44 | } 45 | --------------------------------------------------------------------------------