Monday, 8 January 2024

Next Permutation by O(n)


import java.util.*;

public class Solution {

    public static List< Integer > nextGreaterPermutation(List< Integer > A) {

        // Write your code here

        int n= A.size();

        int dip_idx = -1;

        for(int i=n-2; i>=0; i--)

        {

            if(A.get(i)<A.get(i+1))

            {

                dip_idx = i;

                break;

            }

        }

        if(dip_idx== -1)

        {

            Collections.reverse(A);

        }

        for(int i=n-1;i>=dip_idx;i--)

        {

            if(A.get(i)>A.get(dip_idx))

            {

                int temp=A.get(dip_idx);

                A.set(dip_idx,A.get(i));

                A.set(i,temp);

                break;

            }

        }

        Collections.reverse(A.subList(dip_idx+1, n));

        return A;

    }

}

BigInteger class is not in java.util.*
Hence you have to import it

import java.math.BigInteger;

public class MyFirstClass {

    public static void main(String[] args) {

        BigInteger sm = new BigInteger("4");

        System.out.println(sm);

    }

}


Sunday, 7 January 2024

 longest Subarray with maximum sum as k

import java.util.*;

public class MyFirstClass {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 1, 2, 4, 2, 9};

        long k = 10;

        int len = getLongestSubarray(a, k);

        System.out.println("The length of the longest subarray is: " + len);

    }

public static int getLongestSubarray(int []a, long k) {

        int n = a.length; // size of the array.

        Map<Long, Integer> preSumMap = new HashMap<>();

        long sum = 0;

        int maxLen = 0;

        for (int i = 0; i < n; i++) {

            //calculate the prefix sum till index i:

            sum += a[i];


            // if the sum = k, update the maxLen:

            if (sum == k) {

                maxLen = Math.max(maxLen, i + 1);

            }


            // calculate the sum of remaining part i.e. x-k:

            long rem = sum - k;


            //Calculate the length and update maxLen:

            if (preSumMap.containsKey(rem)) {

                int len = i - preSumMap.get(rem);

                maxLen = Math.max(maxLen, len);

            }


            //Finally, update the map checking the conditions:

            if (!preSumMap.containsKey(sum)) {

                preSumMap.put(sum, i);

            }

        }


        return maxLen;

    }

}


 Intersection of two sorted arrays code in java

import java.util.*;

public class MyFirstClass {

    public static void main(String[] args) {

        int arr1[] = {1,1,2,3,4,4,5};

int arr2[] = {2,3,4,4,5,10,20,100};

int n1 = arr1.length; int n2=arr2.length;

int leng = n1+n2;

int i=0;int j=0;

ArrayList<Integer> Intersection = new ArrayList<>();

while(i<n1 && j<n2)

{

if(arr1[i]<arr2[j])

{

i++;

}

else if(arr2[j] < arr1[i])

{

j++;

}

else

{

Intersection.add(arr1[i]);

i++;

j++;

}

}

System.out.println(Intersection);

    }

}


 Union of two sorted arrays code in java


import java.util.*;

public class MyFirstClass {

    public static void main(String[] args) {

        int arr1[] = {1,1,2,3,4,5};

int arr2[] = {2,3,4,4,5,10,20,100};

int n1 = arr1.length; int n2=arr2.length;

int leng = n1+n2;

int i=0;int j=0;

ArrayList<Integer> Union = new ArrayList<>();

while(i<n1 && j<n2)

{

if (arr1[i] <= arr2[j]) // Case 1 and 2

{

  if (Union.size() == 0 || Union.get(Union.size()-1) != arr1[i])

Union.add(arr1[i]);

  i++;

} else // case 3

{

  if (Union.size() == 0 || Union.get(Union.size()-1) != arr2[j])

Union.add(arr2[j]);

  j++;

}

  }

while(j<n2)

{

if (Union.size() == 0 || Union.get(Union.size()-1) != arr2[j])

        Union.add(arr2[j]);

      j++;

}

while(i<n1)

{

if (Union.size() == 0 || Union.get(Union.size()-1) != arr1[i])

        Union.add(arr1[i]);

      i++;

}

System.out.println(Union);

    }

}


Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...