Assignment DSA

ยท

3 min read

<aside> ๐Ÿ’ก Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2),..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.

Example 1: Input: nums = [1,4,3,2] Output: 4

Explanation: All possible pairings (ignoring the ordering of elements) are:

  1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3

  2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3

  3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4 </aside>

Solution:

class Solution {
    public int arrayPairSum(int[] nums) {
        Arrays.sort(nums);
        int maxSum=0;
        for(int i=0; i<nums.length; i+=2){
            maxSum+=nums[i];
        }
        return maxSum;
    }
}

Question 2 Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor. The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice. Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them. Example 1: Input: candyType = [1,1,2,2,3,3] Output: 3 Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.

class Solution {
    public int distributeCandies(int[] candyType) {
         Set<Integer> typeCandySet = new HashSet<>();
        int halfLength = candyType.length/2;

        for (int i : candyType) {
            if (typeCandySet.size() >= halfLength)
                return halfLength;
            typeCandySet.add(i);
        }
        return Math.min(typeCandySet.size(), halfLength);
    }
}

Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

class Solution {
    public int maximumProduct(int[] nums) {
        int min1, min2, max1, max2, max3;
        min1 = min2 = Integer.MAX_VALUE;
        max1 = max2 = max3 = Integer.MIN_VALUE;
        for(int i = 0; i < nums.length; i++){
            if(max1 < nums[i]){
                max3 = max2;
                max2 = max1;
                max1 = nums[i];
            }else if(max2 < nums[i]){
                max3 = max2;
                max2 = nums[i];
            }else if(max3 < nums[i]){
                max3 = nums[i];
            }


            if(min1 > nums[i]){
                min2 = min1;
                min1 = nums[i];
            }else if(min2 > nums[i]){
                min2 = nums[i];
            }

        }

        return Math.max(max1 * max2 * max3 , min1 * min2 * max1);
    }
}

Explanation: Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4

class Solution{

    static int findElement(int arr[],int target){
        int low=0; 
       int high=arr.length-1;
       while(low<high){
        int mid = low+(high-low)/2;

            if(arr[mid]==target){ 
                return mid;
            }
            else if(mid<target){
                low=mid;
               }
           else{
            high=mid;
           }
        }
     return -1;
    }
    public static void main(String[] args) {
        int arr[]={-1,0,3,5,9,12,5};
        int target=9;
       int result= findElement(arr, target);
        System.out.println( target+" value at index:"+result);

    }
}
ย