├── README.md └── Array 2 PPT Assignment ├── ArrayPairSum.java ├── MonotonicArray.java ├── MaximumProduct.java ├── BinarySearch.java ├── MaximumCandies.java ├── FlowerPlanting.java ├── MinimumScore.java └── LongestHarmoniousSubsequence.java /README.md: -------------------------------------------------------------------------------- 1 | # PPT-Program-By-PW-Array-2 -------------------------------------------------------------------------------- /Array 2 PPT Assignment/ArrayPairSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ArrayPairSum { 4 | public static int arrayPairSum(int[] nums) { 5 | // Sort the array in ascending order 6 | Arrays.sort(nums); 7 | 8 | int sum = 0; 9 | // Pair the elements in consecutive pairs and sum the minimum element in each pair 10 | for (int i = 0; i < nums.length; i += 2) { 11 | sum += nums[i]; 12 | } 13 | 14 | return sum; 15 | } 16 | 17 | public static void main(String[] args) { 18 | int[] nums = {1, 4, 3, 2}; 19 | int maxSum = arrayPairSum(nums); 20 | System.out.println("Maximized sum: " + maxSum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/MonotonicArray.java: -------------------------------------------------------------------------------- 1 | public class MonotonicArray { 2 | public static boolean isMonotonic(int[] nums) { 3 | boolean increasing = true; 4 | boolean decreasing = true; 5 | 6 | for (int i = 1; i < nums.length; i++) { 7 | if (nums[i] < nums[i - 1]) { 8 | increasing = false; 9 | } 10 | if (nums[i] > nums[i - 1]) { 11 | decreasing = false; 12 | } 13 | } 14 | 15 | return increasing || decreasing; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[] nums = {1, 2, 2, 3}; 20 | boolean isMonotonic = isMonotonic(nums); 21 | System.out.println("Is monotonic: " + isMonotonic); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/MaximumProduct.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class MaximumProduct { 4 | public static int maximumProduct(int[] nums) { 5 | Arrays.sort(nums); 6 | 7 | int n = nums.length; 8 | 9 | // Case 1: All numbers are positive or zero 10 | int product1 = nums[n - 1] * nums[n - 2] * nums[n - 3]; 11 | 12 | // Case 2: There are negative numbers 13 | int product2 = nums[0] * nums[1] * nums[n - 1]; 14 | 15 | // Return the maximum product 16 | return Math.max(product1, product2); 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] nums = {1, 2, 3}; 21 | int maxProduct = maximumProduct(nums); 22 | System.out.println("Maximum product: " + maxProduct); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static int search(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 -1; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int[] nums = {-1, 0, 3, 5, 9, 12}; 23 | int target = 9; 24 | int index = search(nums, target); 25 | System.out.println("Target index: " + index); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/MaximumCandies.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Set; 3 | 4 | public class MaximumCandies { 5 | public static int maxCandies(int[] candyType) { 6 | // Create a set to store the different types of candies 7 | Set uniqueCandies = new HashSet<>(); 8 | 9 | // Add each candy type to the set 10 | for (int candy : candyType) { 11 | uniqueCandies.add(candy); 12 | } 13 | 14 | // Return the minimum value between the number of unique candy types and n/2 15 | return Math.min(uniqueCandies.size(), candyType.length / 2); 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[] candyType = {1, 1, 2, 2, 3, 3}; 20 | int maxTypes = maxCandies(candyType); 21 | System.out.println("Maximum number of different types of candies: " + maxTypes); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/FlowerPlanting.java: -------------------------------------------------------------------------------- 1 | public class FlowerPlanting { 2 | public static boolean canPlaceFlowers(int[] flowerbed, int n) { 3 | int count = 0; 4 | int length = flowerbed.length; 5 | int i = 0; 6 | 7 | while (i < length) { 8 | if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == length - 1 || flowerbed[i + 1] == 0)) { 9 | flowerbed[i] = 1; 10 | count++; 11 | } 12 | 13 | i++; 14 | 15 | if (count >= n) { 16 | return true; 17 | } 18 | } 19 | 20 | return false; 21 | } 22 | 23 | public static void main(String[] args) { 24 | int[] flowerbed = {1, 0, 0, 0, 1}; 25 | int n = 1; 26 | boolean canPlace = canPlaceFlowers(flowerbed, n); 27 | System.out.println("Can place flowers: " + canPlace); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/MinimumScore.java: -------------------------------------------------------------------------------- 1 | public class MinimumScore { 2 | public static int minimumScore(int[] nums, int k) { 3 | int min = Integer.MAX_VALUE; 4 | int max = Integer.MIN_VALUE; 5 | 6 | // Find the minimum and maximum values in the array 7 | for (int num : nums) { 8 | min = Math.min(min, num); 9 | max = Math.max(max, num); 10 | } 11 | 12 | // Calculate the initial score 13 | int initialScore = max - min; 14 | 15 | // If the initial score is already 0, return 0 16 | if (initialScore == 0) { 17 | return 0; 18 | } 19 | 20 | // Update the minimum and maximum values 21 | min -= k; 22 | max += k; 23 | 24 | // Calculate the new score 25 | int newScore = max - min; 26 | 27 | return newScore; 28 | } 29 | 30 | public static void main(String[] args) { 31 | int[] nums = {1}; 32 | int k = 0; 33 | int minScore = minimumScore(nums, k); 34 | System.out.println("Minimum score: " + minScore); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Array 2 PPT Assignment/LongestHarmoniousSubsequence.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class LongestHarmoniousSubsequence { 5 | public static int findLHS(int[] nums) { 6 | // Create a HashMap to count the frequency of each number 7 | Map frequencyMap = new HashMap<>(); 8 | 9 | // Count the frequency of each number in the array 10 | for (int num : nums) { 11 | frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); 12 | } 13 | 14 | int maxLength = 0; 15 | // Iterate through the array and check if the current number and its adjacent numbers form a harmonious subsequence 16 | for (int num : nums) { 17 | int count = frequencyMap.getOrDefault(num, 0); 18 | int adjacentCount1 = frequencyMap.getOrDefault(num + 1, 0); 19 | int adjacentCount2 = frequencyMap.getOrDefault(num - 1, 0); 20 | 21 | if (adjacentCount1 != 0) { 22 | maxLength = Math.max(maxLength, count + adjacentCount1); 23 | } 24 | 25 | if (adjacentCount2 != 0) { 26 | maxLength = Math.max(maxLength, count + adjacentCount2); 27 | } 28 | } 29 | 30 | return maxLength; 31 | } 32 | 33 | public static void main(String[] args) { 34 | int[] nums = {1, 3, 2, 2, 5, 2, 3, 7}; 35 | int longestSubsequenceLength = findLHS(nums); 36 | System.out.println("Length of the longest harmonious subsequence: " + longestSubsequenceLength); 37 | } 38 | } 39 | --------------------------------------------------------------------------------