Wednesday, 20 December 2023

Highest / Lowest Frequency Elements

Coding Ninjas Problem Link


import java.util.HashMap;

import java.util.Map;

import java.util.Arrays;

import java.util.Scanner;

public class MyFirstClass {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int v[] = {11,13,3,14,17,3,7,9,1,11,9,15,5,2,2,3,11,2};

        int r[] = getFrequencies(v);

        for (int i : r) {

            System.out.print(i+" ");

        }

    }

    public static int[] getFrequencies(int []v) {

        // Write Your Code Here

        HashMap<Integer,Integer> mp = new HashMap<>();

        int n = v.length;

        //int result[] = new int[n];

        for(int i=0; i<v.length; i++)

        {

            int freq = 0;

            if(mp.containsKey(v[i])) 

            {

                freq = mp.get(v[i]);

            }

            freq += 1;

            mp.put(v[i],freq);

        }

        int maxele=0;int minele=0;

        int minfreq=n;

        int maxfreq = 0;

        for(HashMap.Entry<Integer,Integer> entry : mp.entrySet())

        {

            int element = entry.getKey();

            int count = entry.getValue();

            if(count>=maxfreq)

            {

                if(count==maxfreq) maxele = Math.min(maxele, element);

                else maxele = element;

                maxfreq = count;

            }

            if(count<=minfreq)

            {

                //minele = Math.min(element,minele);

                if(count==minfreq) minele = Math.min(minele, element);

                else minele = element;

                minfreq = count;

            }

        }

        int result[] = new int[2];

        result[0] = maxele;

        result[1] = minele;

        return result;

    }

}


Count Frequency in a range

Coding Ninjas Link


import java.util.HashMap;

public class Solution {

    public static int[] countFrequency(int n, int x, int []nums){

        HashMap<Integer,Integer> mp = new HashMap<>();

        int result[] = new int[n];

        for(int i=0; i<nums.length; i++)

        {

            int freq = 0;

            if(mp.containsKey(nums[i])) 

            {

                freq = mp.get(nums[i]);

            }

            freq += 1;

            mp.put(nums[i],freq);

        }

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

            mp.putIfAbsent(i, 0);

        }

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

        {

            result[i] = mp.get(i+1);

        }

        return result;

    }

}

HashMap and Entry Interface code in JAVA

import java.util.HashMap;

import java.util.Map;

import java.util.Arrays;

import java.util.Scanner;

public class MyFirstClass {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        HashMap<Integer,String> m = new HashMap<>();

        System.out.println(m);

        m.put(101,"John");

        m.put(102,"Aman");

        m.put(103,"Satyam");

        m.put(104,"Aryaman");

        System.out.println(m);

        System.out.println("=======");

        System.out.println(m.entrySet());

        System.out.println(m.keySet().size());

        for(int i: m.keySet())

        {

            System.out.println(i);

        }

        for(String i: m.values())

        {

            System.out.println(i);

        }

        for(int i: m.keySet())

        {

            System.out.println(i+" "+m.get(i));

        }

    }

}


Tuesday, 19 December 2023

Question: Find the highest/lowest frequency element.If there are multiple elements that have the highest frequency or lowest frequency then pick the smallest element.

Coding Ninjas Problem link 



import java.util.HashMap;

import java.util.Map;

import java.util.TreeSet;


public class Solution {

    public static int[] getFrequencies(int[] v) {

        int n = v.length;

        return frequency(v, n);

    }


    public static int[] frequency(int[] arr, int n) {

        HashMap<Integer, Integer> map = new HashMap<>();


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

            map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);

        }


        TreeSet<Integer> maxFreqElements = new TreeSet<>();

        TreeSet<Integer> minFreqElements = new TreeSet<>();


        int maxFreq = 0, minFreq = n;


        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {

            int count = entry.getValue();

            int element = entry.getKey();


            if (count > maxFreq) {

                maxFreqElements.clear();

                maxFreqElements.add(element);

                maxFreq = count;

            } else if (count == maxFreq) {

                maxFreqElements.add(element);

            }


            if (count < minFreq) {

                minFreqElements.clear();

                minFreqElements.add(element);

                minFreq = count;

            } else if (count == minFreq) {

                minFreqElements.add(element);

            }

        }


        int[] res = new int[2];


        res[1] = minFreqElements.first(); // Smallest element with minimum frequency


        if (maxFreqElements.size() > 1) {

            res[0] = maxFreqElements.first(); // Smallest element with maximum frequency

        } else {

            res[0] = maxFreqElements.pollFirst();

        }


        return res;

    }

}


Saturday, 16 December 2023

         L 

      L L L 

   L L L L L 

  L L L L L L L 

L L L L L L L L L


import java.util.*;

public class MyFirstClass {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        for(int i = 1 ; i<=n ; i++)

        { 

            for(int j = i+1; j<=n; j++)

            {

                System.out.print(" ");

            }

            for(int j = 1; j<=i; j++)

            {

                System.out.print("L");

            }

            for(int j = 1; j<i; j++)

            {

                System.out.print("L");

            }

            System.out.println();

        }

    }

}




Friday, 15 December 2023

Pass by value vs Pass by reference in JAVA 

Theory:

Java is strictly pass-by-value. In Java, you can't directly pass variables by reference as you would in languages like C/C++. However, you can achieve a similar effect by passing an array or an object reference.

Code:

import java.util.*;

public class MyFirstClass {

    public static void main(String[] args) {

        //Pass by value example in JAVA

        System.out.println("PASS by value example in JAVA");

        int n = 1;

        passByValue(n);

        System.out.println(n); // You can see O/P of n is unmodified by passByValue func

        System.out.println("PASS by reference example in JAVA");

        // Pass by reference example in JAVA

        int num[] = new int[1];

        num[0] = 10;

        doSomething(num); // Passing the array reference

        System.out.println(num[0]); // Print the modified value

    }

    // Pass by value example in JAVA

    public static void passByValue(int num)

    {

        num = num + 10;

        System.out.println(num);

    }

    // Pass by reference example in JAVA

    public static void doSomething(int num[]) {

        num[0] = num[0] + 10;

        System.out.println(num[0]);

    }

}


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...