I have started Data structures and Algorithms on April 1, 2021, and this repository will be containing my resources, tutorial, codes, and my approach to Qs, for future reference. As I'm in the learning process, this repository will be refreshed daily with my new bits of knowledge.
import java.util.*;
public class Hi {
// here value is copied in localVariable 'a'.
public static void incrementPrimitiveDT(int a) {
a++;
}
// here reference is copied, means 'input' is also pointing to 'arr'.
public static void incrementReferenceDT(int input[]) {
for(int i=0;i<input.length;i++) {
input[i]++;
}
}
public static void main(String[] args) {
int i=10;
incrementPrimitiveDT(i); // passByValue
System.out.println(i); // 10
int arr[]= {1,2,3,4,5};
incrementReferenceDT(arr); // passByReference
System.out.println(Arrays.toString(arr)); // [2, 3, 4, 5, 6]
}
}
public static void main(String[] args) {
// case 1:
Integer i= new Integer(1);
Integer j=new Integer(1);
System.out.println(i==j); // false, coz == check reference address of i and j, and here 'i' is pointing to 1 (i---> 1) and 'j' is pointing to different 1 (j---> 1)
System.out.println(i.equals(j)); // true
// case 2:
Integer a=new Integer(2);
Integer b=2;
System.out.println(a==b); // false
System.out.println(a.equals(b)); // true
// case 3:
Integer x=new Integer(3);
Integer y=x;
System.out.println(x==y); // true
System.out.println(x.equals(y)); // true
// case 4
Integer p=128;
Integer q=128;
System.out.println(p==q); // false
System.out.println(p.equals(q)); // true
// case 5 -128 to 127 is in cached memory so they point to same object without creating it instances.
Integer s=50;
Integer t=50;
System.out.println(s==t); // true
System.out.println(s.equals(t)); // true
}
% operator in JAVA : dividend % divisor - Here two statement is needed to be taken care :-
Store 2 numbers in a number. (true only in +ve scenerio)
int a=5;
int b=4;// number to be injected
int INF=9999;
a=a+(b*INF);
System.out.println(a%INF); // 5
System.out.println(a/INF); //4
Comparator : - Collections.sort(al,(a,b)-> scores[b]-scores[a]);
Arrays.sort(arr,(a,b)-> a.length()-b.length());
// this is a PQ of ARRAY of size 2
Comparator<int[]> ShortDisFromOrigin = (int p1[], int p2[]) -> ((p1[0]*p1[0]) + (p1[1]*p1[1])) - ((p2[0]*p2[0]) + (p2[1]*p2[1]));
PriorityQueue<int[]> pq=new PriorityQueue<>(ShortDisFromOrigin);
// OR
PriorityQueue<int[]> pq=new PriorityQueue<>((int p1[], int p2[]) -> ((p1[0]*p1[0]) + (p1[1]*p1[1])) - ((p2[0]*p2[0]) + (p2[1]*p2[1])));
Arrays.sort(arr,(x,y)-> (x.c < y.c) ? 1 : -1); // 1 signifies swap -1 says no swap so this DEScending order
Arrays.sort(arr, new Comparator<Iteminfo>() {
public int compare(Iteminfo o1, Iteminfo o2)
{
if(o2.c > o1.c) return 1; // that's why we write o2.c - o1.c if descending order
return -1;
}
});
// freq of words
HashMap<String, Integer> hm=new HashMap<>();
for(String word : words) hm.put(word, hm.getOrDefault(word,0)+1);
// adding entries in list for sorting
List<Map.Entry<String, Integer>> l=new ArrayList<>();
for(Map.Entry<String, Integer> e : hm.entrySet()) l.add(e); // if u want key then write e.getKey() || e.getValue();
Collections.sort(l, (e1,e2)-> (e2.getValue()!=e1.getValue())?e2.getValue()-e1.getValue(): e1.getKey().compareTo(e2.getKey())); // conditional sorting
//
// itterating over keys
for(String s : hm.keySet())
hm.get(s);
char ch = '2';
int a = ch - '0'; // 2
int a=65;
char ch=(char)a; // A
int i=Integer.parseInt("0100"); // 100
String s="200";
int i=Integer.valueOf(s);
String.valueOf(20);
for(char c = 'a'; c<='z'; c++){
arr[c-'a'];
}
return sb.toString();
return sb.substring(0, sb.length() - 1); // except last
Char[] to String
return new String(ch);
return String.valueOf(ch);
public class Demo{
public static void main(String[] args) {
Human obj=new Human();
Human son=obj; // this is his mom who call obj as hisSon.
Human bro=obj; // this is his sis who call obj as hisBro.
son.hairCut="done"; // here his mom made him do haircut.
// here his sister is also able to see his haircut.
System.out.println(bro.hairCut); // done
}
}
class Human{
String name="Vikash";
String hairCut="notDone";
}