Showing posts with label coding question in java. Show all posts
Showing posts with label coding question in java. Show all posts

Convert String into ArrayList.

 A very basic and simple approach to convert String into ArrayList.

if your String looks like - 

 String input[] = {"one","two","three"};

import java.util.ArrayList;
import java.util.List;

public class Javaoneworld {



    public static void main(String[] args) {

        String input[] = {"one","two","three"};
        List<String> arrList = new ArrayList<>();

        for(String s:input){

            arrList.add(s);
        }
        System.out.print(arrList);
    }

}
   

And if String looks like below - 

 String input = "[one,two,three]";

Then - 
import java.util.ArrayList;
import java.util.List;

public class First {



    public static void main(String[] args) {

        List<String> arrList = new ArrayList<>();
        String input = "[one,two,three]";

        String stringArr[] = input
                .replace("[","")
                .replace("]","")
                .split(",");

        for(String s:stringArr){

            arrList.add(s);
        }
        System.out.print(arrList);
    }

}
   

And if String is - 

String input = "[\"one\",\"two\",\"three\"]";

then - 


import java.util.ArrayList;
import java.util.List;

public class First {



    public static void main(String[] args) {

        String input = "[\"one\",\"two\",\"three\"]";
        Gson gson = new Gson();
        JsonArray jsonArray = gson.fromJson(input, JsonArray.class);
        List<String> arrList = new ArrayList<>();
        for (JsonElement jsonElement : jsonArray) {
            String s = jsonElement.getAsString();
            arrList.add(s);
        }

        System.out.println(arrList);
    }

}
   

Find Maximum and minimum in an array by comparing in pairs

Find the Maximum and minimum of an array using a minimum number of comparisons.

For every problem we will have multiple solutions, everyone can have their own solution.

For this problem here we have three approaches to solve this problem - 


  1. First Approach "Simple Linear Search"
  2. Second Approach "Tournament Method (Divide and Conquer)"
  3. The third Approach "Compare in Pairs"


Description -  

If n is odd then initialize min and max as the first element. 

If n is even then initialize min and max as minimum and maximum of the first two elements respectively. 

For the rest of the elements, pick them in pairs and compare their maximum and minimum with max and min respectively. 

********Code********

package javaoneworld.learndsa.problems;


public class JavaOneWorldDSA {
    /* Class Pair is used to return two values from getMinMax() */
    static class Pair {

        int min;
        int max;
    }



    static Pair getMinMax(int arr[], int n) {
        Pair minmax = new Pair();
        int i;
        /* If array has even number of elements then 
    initialize the first two elements as minimum and 
    maximum */
        if (n % 2 == 0) {
            if (arr[0] > arr[1]) {
                minmax.max = arr[0];
                minmax.min = arr[1];
            } else {
                minmax.min = arr[0];
                minmax.max = arr[1];
            }
            i = 2;
            /* set the starting index for loop */
        } /* If array has odd number of elements then 
    initialize the first element as minimum and 
    maximum */ else {
            minmax.min = arr[0];
            minmax.max = arr[0];
            i = 1;
            /* set the starting index for loop */
        }
 
        /* In the while loop, pick elements in pair and 
     compare the pair with max and min so far */
        while (i < n - 1) {
            if (arr[i] > arr[i + 1]) {
                if (arr[i] > minmax.max) {
                    minmax.max = arr[i];
                }
                if (arr[i + 1] < minmax.min) {
                    minmax.min = arr[i + 1];
                }
            } else {
                if (arr[i + 1] > minmax.max) {
                    minmax.max = arr[i + 1];
                }
                if (arr[i] < minmax.min) {
                    minmax.min = arr[i];
                }
            }
            i += 2;
            /* Increment the index by 2 as two 
               elements are processed in loop */
        }

        return minmax;
    }

    /* Testing the implemented program  */
    public static void main(String args[]) {
        int arr[] = {999, 98, 986, 56, 550, 7986};
        int arr_size = 6;
        Pair minmax = getMinMax(arr, arr_size);
        System.out.println("Minimum element is :"+ minmax.min);
        System.out.println("Maximum element is :"+ minmax.max);

    }

}
   
Time Complexity: O(n)

Total number of comparisons: Different for even and odd n


Find Maximum and minimum in an array Tournament Method (Divide and Conquer)

Find the maximum and minimum of an array using a minimum number of comparisons.

For every problem we will have multiple solutions, everyone can have their own solution.

For this problem here we have three approaches to solve this problem - 


  1. First Approach "Simple Linear Search"
  2. Second Approach "Tournament Method (Divide and Conquer)"
  3. The third Approach "Compare in Pairs"


Second Approach "Tournament Method (Divide and Conquer)"

Description -  

Divide the array into two parts and compare the maximums and minimums of the two parts to get the maximum and the minimum of the whole array.

********Code********

package javaoneworld.learndsa.problems;


public class JavaOneWorldDSA {
    /* Class Pair is used to return two values from getMinMax() */
    static class Pair {

        int min;
        int max;
    }


    static Pair getMinMax(int arr[], int low, int high) {
        Pair minmax = new Pair();
        Pair mml = new Pair();
        Pair mmr = new Pair();
        int mid;

        // If there is only one element
        if (low == high) {
            minmax.max = arr[low];
            minmax.min = arr[low];
            return minmax;
        }

        /* If there are two elements */
        if (high == low + 1) {
            if (arr[low] > arr[high]) {
                minmax.max = arr[low];
                minmax.min = arr[high];
            } else {
                minmax.max = arr[high];
                minmax.min = arr[low];
            }
            return minmax;
        }

        /* If there are more than 2 elements */
        mid = (low + high) / 2;
        mml = getMinMax(arr, low, mid);
        mmr = getMinMax(arr, mid + 1, high);

        /* compare minimums of two parts*/
        if (mml.min < mmr.min) {
            minmax.min = mml.min;
        } else {
            minmax.min = mmr.min;
        }

        /* compare maximums of two parts*/
        if (mml.max > mmr.max) {
            minmax.max = mml.max;
        } else {
            minmax.max = mmr.max;
        }

        return minmax;
    }

    /* Testing the implemented program  */
    public static void main(String args[]) {
        int arr[] = {999, 98, 986, 56, 550, 7986};
        int arr_size = 6;
        Pair minMax = getMinMax(arr, 0, arr_size - 1);
        System.out.println("Minimum element is :"+ minMax.min);
        System.out.println("Maximum element is :"+ minMax.max);

    }

}
     
Time Complexity - O(n)

Find Maximum and minimum in an array

Find the Maximum and minimum of an array using a minimum number of comparisons.

For every problem we will have multiple solutions, everyone can have their own solution.

For this problem here we have three approaches to solve this problem - 


  1. First Approach "Simple Linear Search"
  2. Second Approach "Tournament Method (Divide and Conquer)"
  3. The third Approach "Compare in Pairs"

  1. First Approach "Simple Linear Search" 
Description - 
First, initialize values of minValue and maxValue as minimum and maximum of the first two elements respectively. Now Starting from 3rd, compare each element with maxValue and minValue, and change maxValue and minValue accordingly (i.e., if the element is smaller than minValue then change minValue, else if the element is greater than maxValue then change maxValue, else ignore the element) 

*********Code********
package javaoneworld.learndsa.problems;


public class JavaOneWorldDSA {
    /* Class Pair is used to return two values from getMinMax() */
    static class Pair {

        int minValue;
        int maxValue;
    }

    static Pair getMinMax(int arr[], int n) {
        Pair minmax = new  Pair();
        int i;

        /*If there is only one element then return it as min and max both*/
        if (n == 1) {
            minmax.maxValue = arr[0];
            minmax.minValue = arr[0];
            return minmax;
        }

        /* If there are more than one elements, then initialize min
    and max*/
        if (arr[0] > arr[1]) {
            minmax.maxValue = arr[0];
            minmax.minValue = arr[1];
        } else {
            minmax.maxValue = arr[1];
            minmax.minValue = arr[0];
        }

        for (i = 2; i < n; i++) {
            if (arr[i] > minmax.maxValue) {
                minmax.maxValue = arr[i];
            } else if (arr[i] < minmax.minValue) {
                minmax.minValue = arr[i];
            }
        }

        return minmax;
    }

    /* Testing the implemented program  */
    public static void main(String args[]) {
        int arr[] = {999, 98, 986, 56, 550, 7986};
        int arr_size = 6;
        Pair minMax = getMinMax(arr, arr_size);
        System.out.println("Minimum element is :"+ minMax.minValue);
        System.out.println("Maximum element is :"+ minMax.maxValue);

    }

}
     
Time Complexity - O(n)