Posts

toString() method which is use in android to know the states of java Object.

  You can use log statements to figure out the state of any Java object. The easiest way to print out the contents of a Java object is to provide an implementation of the toStrin g () method. The purpose of this method is to represent the whole object as a string, usually for debugging purposes. Note:  If you concatenate (with the “+” operator) a string with a Word object, then Java will implicitly call the toString() method on the object. That means, these two statements are equivalent: Log.v("NumbersActivity", "Current word: " + word); OR Log.v("NumbersActivity", "Current word: " + word.toString());

GFG Sorting Comparable and Comparator interface

  package com.company ; import java.awt. *; import java.util. *; import java.util. List ; class point { int x , y ; public point ( int x , int y ) { this . x = x ; this . y = y ; } } class myComparator implements Comparator < point >{ public int compare ( point p1 , point p2 ){ return p1 . x - p2 . x ; } } //Implementing Comparable interface //class point implements Comparable<point> { // int x, y; // public point(int x, int y) { // this.x = x; // this.y = y; // } // public int compareTo(point p){ // return x - p.x; // } //} //All even number comes first than all odd numbers class MyClass implements Comparator < Integer > { @Override public int compare ( Integer a , Integer b ) { return a % 2 - b % 2 ; } } public class Main { public static void main ( String [] args ) { // int[] array = {1,3,4,2,98,78}; //// Arrays.sort(array); // Arrays.sor...

Repeating element GFG

  package com.company ; public class RepeatingElement { // EFFICIENT SOLUTION public static int getRepeatedElement ( int [] arr ) { int n = arr . length ; boolean [] visited = new boolean [ n ]; for ( int i = 0 ; i < n ; i ++) { visited [ i ] = false ; } for ( int i = 0 ; i < n ; i ++) { if ( visited [ arr [ i ]]){ return arr [ i ]; } visited [ arr [ i ]] = true ; } return - 1 ; } // More efficient solution public static int get ( int [] arr ){ int n = arr . length ; int slow = arr [ 0 ] + 1 ; int fast = arr [ 0 ] + 1 ; do { slow = arr [ slow ] + 1 ; fast = arr [ arr [ fast ] + 1 ] + 1 ; } while ( slow != fast ); slow = arr [ 0 ] + 1 ; while ( slow != fast ) { slow = arr [ slow ] + 1 ; fast = arr [ fast ] + 1 ; } return slow - 1 ; } ...

Median of two sorted array GFG *******

  package com.company ; import java.util.Arrays ; public class MedianOfSortedArray { //Median of two sorted array public static double median ( int [] arr , int [] array ) { int n = arr . length ; int m = array . length ; int [] find = new int [ n + m ]; for ( int i = 0 ; i < n ; i ++) { find [ i ] = arr [ i ]; } for ( int i = 0 ; i < m ; i ++) { find [ i + n ] = array [ i ]; } Arrays . sort ( find ); int a = find . length ; if ( a % 2 == 0 ) { int sum = find [ a / 2 ] + find [ a / 2 - 1 ]; return sum / 2.0 ; } else return find [ a / 2 ]; } //Efficient solution public static double getMedian ( int [] arr1 , int [] arr2 ) { int n1 = arr1 . length ; int n2 = arr2 . length ; int low = 0 ; int high = n1 ; while ( low <= high ){ int i1 = ( low + high ) / 2 ; ...

Peak element GFG

  package com.company ; public class PeakElement { // Finding the peak element public static int peak ( int [] arr ){ int n = arr . length ; if ( n == 1 ) { return arr [ 0 ]; } if ( arr [ 0 ] >= arr [ 1 ]){ return arr [ 0 ]; } if ( arr [ n - 1 ] >= arr [ n - 2 ]){ return arr [ n - 1 ]; } for ( int i = 1 ; i < n - 1 ; i ++) { if ( arr [ i ] >= arr [ i - 1 ] && arr [ i ] >= arr [ i + 1 ]) { return arr [ i ]; } } return - 1 ; } // Efficient solution public static int getPeakElement ( int [] arr ) { int n = arr . length ; int low = 0 , high = n - 1 ; while ( low <= high ){ int mid = ( low + high ) / 2 ; if (( mid == 0 || arr [ mid - 1 ] <= arr [ mid ]) && ( mid == n - 1 || arr [ mid + 1 ] <= arr [ mid ])) {...

Searching an element in sorted and rotated array GFG

  package com.company ; public class SearchingInSortedAndRotatedArray { // search in a sorted and roted array public static int search ( int [] arr , int x ) { int n = arr . length ; int low = 0 ; int high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) { return mid ; } if ( arr [ mid ] > arr [ low ]) { if ( arr [ low ] <= x && arr [ mid ] > x ) { high = mid - 1 ; } else low = mid + 1 ; } else { if ( arr [ high ] >= x && arr [ mid ] < x ) { low = mid + 1 ; } else high = mid - 1 ; } } return - 1 ; } public static void main ( String [] args ) { int [] array = { 10 , 20 , 40 , 60 , 5 , 8 }; System ...

Searching element in sorted infinite array GFG

  package com.company ; public class SearchingInInfiniteArray { public static int binarySearching ( int [] arr , int x , int l , int h ) { int n = arr . length ; int low = l ; int high = h ; while ( low <= high ) { int mid =( low + high ) / 2 ; if ( arr [ mid ] == x ){ return mid ; } else if ( arr [ mid ] < x ) { low = mid + 1 ; } else { high = mid - 1 ; } } return - 1 ; } // Searching an element in an infinite array // Recursive approach public static int search ( int [] arr , int x , int index ) { if ( arr [ index ] == x ) { return index ; } else if ( arr [ index + 1 ] > x && arr [ index ] < x ) { return - 1 ; } else { return search ( arr , x , index + 1 ); } } //Iterative approach public st...