605. Can Place Flowers

605. Can Place Flowers

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if(flowerbed.length == 1 && flowerbed[0] == 0) n--;
        for(int i = 0; i < flowerbed.length; i++){
            if(i ==0 && flowerbed[i] == 0 && i+1 <  flowerbed.length && flowerbed[i+1] == 0){
                flowerbed[i] = 1;
                n--;
            }else if( i == flowerbed.length -1 && 
                     flowerbed[i] == 0 && i-1 >= 0 &&
                     flowerbed[i-1] == 0){
            flowerbed[i] = 1;
                n--;
            }
            else if(flowerbed[i] == 0  &&
                    i-1 >= 0 &&
                    flowerbed[i-1] == 0  &&
                    i+1 <  flowerbed.length &&
                    flowerbed[i+1] == 0){
                flowerbed[i] = 1;
                n--;
            } 
        }
        if(n <= 0) return true;
        else return false;
    }
}

Mini-Max Sum

https://www.hackerrank.com/challenges/mini-max-sum/problem

   public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
         Collections.sort(arr);
         BigInteger sum = BigInteger.ZERO;
         for (int i = 0; i < arr.size(); i++){
              sum =sum.add(BigInteger.valueOf(arr.get(i)));
         }
         System.out.print(sum.subtract(BigInteger.valueOf(arr.get(arr.size()-1).intValue()))+" ");
         System.out.print(sum.subtract(BigInteger.valueOf(arr.get(0).intValue())));
      
    }

Compare the Triplets

https://www.hackerrank.com/challenges/compare-the-triplets/problem

    public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
    // Write your code here
        List<Integer> out = new ArrayList<Integer>(2);
        out.add(0);
        out.add(0);
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i) > b.get(i))
                out.set(0, out.get(0) + 1);
            else if (a.get(i) < b.get(i))
                out.set(1, out.get(1) + 1);
        }

        return out;
    }

Bitwise AND

Given an array of non-negative integers, count the number of unordered pairs of array
elements such that their bitwise AND is a power of 2.
For example, let’s say the array is arr = [10, 7, 2, 8, 3], and let ‘&’ denote the bitwise AND
operator. There are 6 unordered pairs of its elements that have a bitwise AND that is a
power of two:
For indices (0,1), 10 & 7 = 2, which is a power of 2.
For indices (0,2), 10 & 2 = 2, which is a power of 2.
For indices (0,3), 10 & 8 = 8, which is a power of 2.
For indices (0,4), 10 & 3 = 2, which is a power of 2.
For indices (1,2), 7 & 2 = 2, which is a power of 2.
For indices (2,4), 2 & 3 = 2, which is a power of 2.
Therefore, the answer is 6.

    public static long countPairs(List<Integer> arr) {
    // Write your code here
    int count = 0;
     for(int i = 0; i < arr.size(); i++){
         for(int j= i+1; j < arr.size(); j++){
             double a = Math.log(arr.get(i).intValue()&arr.get(j).intValue())/Math.log(2) ;
             if(a == (int) a)
             count++;
         }
     }
     return count;
    }