Posts

Showing posts from January, 2024

TRAPPING WATER

  public class trappingwater {     public static int trappingwater ( int height []) {         // LEFT MAX BOUNDARY         int leftMax [] = new int [ height . length ];         leftMax [ 0 ] = height [ 0 ];         for ( int i = 1 ; i < height . length ; i ++ ) {             leftMax [ i ] = Math . max ( height [ i ], leftMax [ i - 1 ]);         }         // RIGHT MAX BOUNDARY         int rightMax [] = new int [ height . length ];         rightMax [ height . length - 1 ] = height [ height . length - 1 ];         for ( int i = height . length - 2 ; i >= 0 ; i -- ) {             rightMax [ i ] = Math . max ( height [ i ], rightMax [ i + 1 ]);         }       ...

Kadane's Algorithm(Max subarray sum)

  public class kadanesalg {     // MAXSUBARRAYSUM     public static void kadanesalg ( int arr []) {         int currSum = 0 ;         int maxSum = Integer . MIN_VALUE ;         for ( int i = 0 ; i < arr . length ; i ++ ) {             currSum = currSum + arr [ i ];             if ( currSum < 0 ) {                 currSum = 0 ;             }             maxSum = Math . max ( maxSum , currSum );         }         System . out . println ( "maximum sum:" + maxSum );     }     public static void main ( String [] args ) {         int arr [] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , 3 };         kadanesalg ( arr )...

Sum of SubArrays

  public class sumofsubarrays {     public static void Sum_Of_SubArrays ( int arr []) {         int maxSum = Integer . MIN_VALUE ;         for ( int i = 0 ; i < arr . length ; i ++ ) {             for ( int j = 0 ; j < arr . length ; j ++ ) {                 int currSum = 0 ;                 for ( int k = i ; k <= j ; k ++ ) {                     System . out . print ( arr [ k ] + " " );                     currSum += arr [ k ];                     if ( maxSum < currSum ) {                         maxSum = currSum ;               ...

Sub Arrays

  public class subarrays {     public static void subarrays ( int arr []) {         for ( int i = 0 ; i < arr . length ; i ++ ) {             for ( int j = i ; j < arr . length ; j ++ ) {                 for ( int k = i ; k <= j ; k ++ ) {                     System . out . print ( arr [ k ] + " " );                 }                 System . out . println ();             }         }     }     public static void main ( String [] args ) {         int arr [] = { 2 , 4 , 6 , 8 , 10 };         subarrays ( arr );     } }

pairs in Arrays

  public class pairs {     public static void Pairs ( int arr []) {         for ( int i = 0 ; i < arr . length ; i ++ ) {             int next = arr [ i ];             for ( int j = i + 1 ; j < arr . length ; j ++ ) {                 System . out . print ( "(" + next + "," + arr [ j ] + ")" );             }             System . out . println ();         }     }     public static void main ( String [] args ) {         int arr [] = { 2 , 4 , 6 , 8 , 10 };         Pairs ( arr );     } }

Binary Search

  public class binarysearch {     public static int Binary_Search ( int arr [], int key ) {         int start = 0 ;         int end = arr . length - 1 ;         while ( start <= end ) {             int mid = ( start + end ) / 2 ;             if ( arr [ mid ] == key ) {                 return mid ;             } else if ( arr [ mid ] < key ) {                 start = mid + 1 ;             } else if ( arr [ mid ] > key ) {                 end = mid - 1 ;             }         }         return - 1 ;     }     public static void main ( Str...

Linear Search

  public class linearsearch {     public static int linearSearch ( int numbers [], int key ) {         for ( int i = 0 ; i < numbers . length ; i ++ ) {             if ( numbers [ i ] == key ) {                 return i ;             }         }         return - 1 ;     }     public static void main ( String [] args ) {         int numbers [] = { 1 , 2 , 5 , 71 , 69 , 97 };         int key = 69 ;         int index = linearSearch ( numbers , key );         if ( index == - 1 ) {             System . out . println ( "number not found" );         } else {             System . out . println ( "number found ...

Sum of Digits

  public class sumofint {     public static void sum ( int n ) {         int sum = 0 ;         int lastDigit = 0 ;         while ( n > 0 ) {             lastDigit = n % 10 ;             sum = sum + lastDigit ;             n = n / 10 ;         }         System . out . println ( "sum of digits = " + sum );     }     public static void main ( String [] args ) {         sum ( 934 );     } }

Reversing an integer

  public class revereint {     public static int reverse ( int n ) {         int rev = 0 ;         while ( n > 0 ) {             rev = rev * 10 + n % 10 ;             n = n / 10 ;         }         return rev ;     }     public static void main ( String [] args ) {         System . out . println ( reverse ( 1345 ));     } }

Palindrome

public class palindrome {     public static void palindrome ( int n ) {         int rev = 0 ;         int myNum = n ;         while ( n > 0 ) {             rev = rev * 10 + n % 10 ;             n = n / 10 ;         }         System . out . println ( rev );         if ( myNum == rev ) {             System . out . println ( "It is a palindrome" );         } else {             System . out . println ( "It is not a palindrome" );         }     }     public static void main ( String [] args ) {         palindrome ( 121 );     } }  

Decimal to Binary

  public class decimaltobinary {     public static void binary ( int n ) {         int binNum = 0 ;         int decNum = n ;         int rem ;         for ( int pow = 0 ; n > 0 ; pow ++ ) {             rem = n % 2 ;             binNum = binNum + ( rem * ( int ) Math . pow ( 10 , pow ));             n = n / 2 ;         }         System . out . println ( "Binary of " + decNum + "=" + binNum );     }     public static void main ( String [] args ) {         binary ( 51 );     } }

Binary to Decimal

  public class binarytodecimal {     public static void decimalnum ( int n ) {         int binNum = n ;         int decNum = 0 ;         int ld ;         for ( int pow = 0 ; n > 0 ; pow ++ ) {             ld = n % 10 ;             decNum = decNum + ( ld * ( int ) Math . pow ( 2 , pow ));             n = n / 10 ;         }         System . out . println ( "decimal of " + binNum + "=" + decNum );     }     public static void main ( String [] args ) {         decimalnum ( 11111 );     } }

Primes from 1 to n

  public class primerange {     public static boolean isprime ( int n ) {         for ( int i = 2 ; i <= Math . sqrt ( n ); i ++ ) {             if ( n % i == 0 ) {                 return false ;             }         }         return true ;     }     public static void primerange ( int n ) {         for ( int i = 2 ; i <= n ; i ++ ) {             if ( isprime ( i )) {                 System . out . println ( i + " " );             }         }         System . out . println ();     }     public static void main ( String [] args ) {         primerange ( 20 );...

Prime number(METHODS)

  public class prime {     public static boolean isprime ( int n ) {         boolean isprime = true ;         if ( n == 2 || n == 1 ) {             System . out . println ( "prime number" );         }         for ( int i = 2 ; i <= n - 1 ; i ++ ) {             if ( n % i == 0 ) {                 isprime = false ;                 System . out . println ( "not a prime number" );                 break ;             } else {                 System . out . println ( "prime number" );                 break ;             }         ...

Binomial co-efficient(methods)

  package code . mypack ; public class binomial {     public static int fact ( int n ){         int f = 1 ;         for ( int i = 1 ; i <= n ; i ++ ){             f = f * i ;         }         return f ;     } public static void main ( String [] args ) { int n = 10 , r = 7 , nmr = n - r ; int nfact = fact ( n ); int rfact = fact ( r ); int nmrfact = fact ( nmr ); System . out . println ( nfact ); System . out . println ( rfact ); System . out . println ( nmrfact ); int bincoef = nfact / ( rfact * nmrfact ); System . out . println ( bincoef ); } }

pattern alphabets

  import java . util .* ; public class p15 {     public static void main ( String [] args ) {         char ch = 'A' ;         System . out . println ( "enter n:" );         Scanner sc = new Scanner ( System . in );         int n = sc . nextInt ();         for ( int i = 1 ; i <= n ; i ++ ){         for ( int j = 1 ; j <= i ; j ++ ){             System . out . print ( ch );             ch ++ ;         }         System . out . println ();        }     } }

Finding the sum of odd and even numbers

  package code ; import java . util .* ; public class p10a {     public static void main ( String [] args ) {                 Scanner sc = new Scanner ( System . in );                         int evensum = 0 ;         int choice ;         int oddsum = 0 ;         do {               System . out . println ( "Enter n:" );   int n = sc . nextInt ();             if ( n % 2 == 0 ){ evensum += n ;             }             else {                 oddsum += n ;             }             System . out . println ( "enter choice(0 to exit and 1 to continue)" );       ...

Prime number

  package code ; import java . util .* ; public class prime {     public static void main ( String [] args ) {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter n:" );         int n = sc . nextInt ();         boolean prime = true ;         if ( n == 2 ) {             System . out . println ( "Prime number" );         } else {             for ( int i = 2 ; i <= n - 1 ; i ++ ) { // i<=math.sqrt(n)                 if ( n % i == 0 ) {                     prime = false ;                 }             }             if ( prime == t...

Reversing a given number

  package code . mypack . practice ; import java . util .* ; public class reversing {     public static void main ( String [] args ) {         System . out . println ( "enter n:" );         Scanner sc = new Scanner ( System . in );         int n = sc . nextInt ();         int rev = 0 ;         while ( n > 0 ) {             int lastdigit = n % 10 ;             rev = ( rev * 10 ) + lastdigit ;             n = n / 10 ;         }         System . out . println ( rev );     } }

printing the reverse of an integer

  package code . mypack . practice ; import java . util .* ; public class reverse {     public static void main ( String [] args ) {         System . out . println ( "enter n:" );         Scanner sc = new Scanner ( System . in );         int n = sc . nextInt ();         while ( n > 0 ) {             int lastdigit = n % 10 ;             System . out . print ( lastdigit );             n = n / 10 ;         }         }     }