Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm.


public class MaxSubArraySumKadanesALgorithm {
    public static void Kadane(int nums[]) {
        int ms = Integer.MIN_VALUE; // maxumum sum
        int cs = 0; // current sum
        // This is basic code of Kadane algorithm, //
        // this code is not useful when all number is -ve.
        // 4 to 5 = answer 7 ms.
        for (int i = 0; i < nums.length; i++) {
            cs = cs + nums[i];
            if (cs < 0) {
                cs = 0;
            }
            ms = Math.max(cs, ms);
        }
        System.out.println("Our max subarray sum is: " + ms);
    }

    public static void main(String[] args) {
        int number[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
        Kadane(number);
    }
}


...

Print Subarrays JAVA Code

Subarray with total subarray.



import java.util.*;

public class matircode {
    public static void printSubarrays(int nums[]) {
        int ts = 0;
        for (int i = 0; i < nums.length; i++) { // nums length = 5

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

                for (int k = i; k <= j; k++) { // print
                    System.out.print(nums[k] + " "); // Subarray
                }
                ts++;
                System.out.println("-");
            }
            System.out.println("-");
        }
        System.out.println("total subarray: " + ts);
    }

    public static void main(String[] args) {
        int number[] = { 2, 4, 6, 8, 10 };
        printSubarrays(number);
    }
}



Trapping Rainwater Java Code

Trapping Rainwater 

Trapping Rainwater Java Programming Code:


import java.util.*;

public class TrappingRainWater {
    public static int TrappingRain(int height[]) {
        int n = height.length;
        // claculate left max boundary - array
        int leftMax[] = new int[n];
        leftMax[0] = height[0];
        for (int i = 1; i < n; i++) {
            leftMax[i] = Math.max(height[i], leftMax[i - 1]);
        }
        // claculate right max boundary - array
        int rightMax[] = new int[n];
        rightMax[n - 1] = height[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            rightMax[i] = Math.max(height[i], rightMax[i + 1]);
        }
        int trappedWater = 0;
        // loop
        for (int i = 0; i < n; i++) {
            // waterLevel = min(leftmax bound, right bound)
            int waterLevel = Math.min(leftMax[i], rightMax[i]);
            // trapped water = waterLevel - hight[i]
            trappedWater += waterLevel - height[i];
        }
        return trappedWater;

    }

    public static void main(String[] args) {
        int height[] = { 1, 4, 2, 0, 6, 3, 2, 5 };
        System.out.println(TrappingRain(height));
    }
}

 

Print Subarrays Java Programming Code

 Print Subarrays:






Code: 

public class Subarray {
    public static void subR(int n[]) {
        for (int i = 0; i < n.length; i++) {
            int start = i;
            for (int j = i; j < n.length; j++) {
                int end = j;
                for (int k = start; k <= end; k++) {
                    System.out.print("(" + n[k] + ")" + " ");
                }
                System.out.print(" ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int Number[] = { 2, 4, 6, 8, 10 };
        subR(Number);

    }
}

😘


Binary Search Java Code

 Pseudo Code:

start =0, end = n-1

While(start<=end) 

find mid  {mid=(start+end)/2}

compare mid and key

mid==key {found}

mid>key {Left} end=mid-1

mid<key {right} start=mid+1

if not found: return -1

public class BinarySearch {
    public static int SearchBinary(int num[], int key) {
        int start = 0, end = num.length - 1;

        while (start <= end) {
            int mid = (start + end) / 2;

            // comparison
            if (num[mid] == key) {
                return mid;
            }
            if (num[mid] > key) {
                end = mid - 1; // left
            } else {
                start = mid + 1; // right : mid < key
            }

        }
        return -1;
    }

    public static void main(String[] args) {
        int numbers[] = { 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 80, 24, 30 };
        System.out.println(SearchBinary(numbers, 14));
        System.out.println("Hello World");
    }
}

0-1 Triangle Pattern Java Programming Code

 Zero One Triangle Pattern 



0-1 Triangle Pattern Java Programming Code:

public class ZeroOne {
    public static void zero_one_triangle(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if ((i + j) % 2 == 0) { // even
                    System.out.print("1");
                } else {
                    System.out.print("0");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        zero_one_triangle(7);
    }
}

...

Floyd's Triangle Pattern Simple Java Programming Code


Floyd's Triangle Pattern Output:


Floyd's Triangle Pattern Java Code:

public class FloydsTrianglePattern {
    public static void floyds_triangle(int n) {
        // outer
        int counter = 1;
        for (int i = 1; i <= n; i++) {
            // inner - how many times will counter be printed
            for (int j = 1; j <= i; j++) {
                System.out.print(counter + " ");
                counter++;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        floyds_triangle(9);

    }
}


Inverted Half Pyramid with Numbers Pattern Java Programming Code

Inverted Half Pyramid with Numbers Output Screenshot



Inverted Half Pyramid with Numbers Pattern Simple Java Code

public class Inverted_Half_Pyramid_with_Numbers {
    public static void main(String[] args) {
        IHPN(9);

    }

    public static void IHPN(int n) {
        for (int i = 1; i <= n; i++) {
            // inner - numbers
            for (int j = 1; j <= n - i + 1; j++) {
                System.out.print(j);
                // System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Inverted And Rotated Half Pyramid Java Programming Pattern Code


Inverted And Rotated Half Pyramid Pattern Output Result Image Screenshot.





Code:

public class Inverted_Rotated_Half_Pyramid {
    public static void irhp(int n) {
        // outer
        for (int i = 1; i <= n; i++) {
            // spaces
            for (int j = 1; j <= n; j++) {
                System.out.print(" ");
            }
            // stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        irhp(7);

    }
}

sgd

Hollow Rectangle Pattern Java Programming Code

Hollow Rectangle Pattern Java Programming Code Output:


Hollow Rectangle Pattern Simple Java Code:


public class HollowRectanglePattern {
    public static void hollow_rectangle(int totRows, int totCols) {
        // outer Loop
        for (int i = 1; i <= totRows; i++) {
            // inner - columns
            for (int j = 1; j <= totCols; j++) {
                // cell = (i,j)
                if (i == 1 || i == totRows || j == 1 || j == totCols) {
                    // boundary cells
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }

    public static void main(String[] args) {
        hollow_rectangle(10, 15);
    }
}


Please Don't Received Call of Your Enemy. They hell Your life. Please Block then as soon as possible.

Noman, Nishan, Maruf, Amir, Samia Batch of 18 CSE in PCIU students are always disturbing me, without any reason They are very jealous of me, envy me and gossip about me all day long.

They abuse me all the time. I feel very sad about it. 

PALINDROMIC Pattern with Numbers pattern Java Programming Code

PALINDROMIC Pattern with Numbers pattern


Code :

public class PALINDROMICpatternWithNumbers {
    public static void main(String[] args) {
        // Outer Loop
        int n = 9;
        for (int i = 1; i <= n; i++) {
            // spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // descending
            for (int j = i; j >= 1; j--) {
                System.out.print(j);
            }

            // ascending
            for (int j = 2; j <= i; j++) {
                System.out.print(j);
            }

            System.out.println(" ");

        }
    }
}

ASDF...

Print All Prime in Range Simple Java Programming Code

Prime In Range Code:

public class PrintPrimeRange {

    public static boolean isPrime(int n) {
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void PrimeInRange(int n) {
        for (int i = 2; i <= n; i++) {
            if (isPrime(i)) { // true
                System.out.print(i + " ");
            }

        }
        System.out.println();
    }

    public static void main(String[] args) {
        PrimeInRange(999); // 2 to 999

    }
}


Prime Number 1 to 999:

Output:

 java .\PrintPrimeRange.java

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 

Check Prime Optimized Function Code Java Programming

Optimized Function: Check if a number is Prime or not Java code: 🧊

public class CheckPrimeOptimized {

    public static boolean isPrime(int n) {
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPrime(6833));
        System.out.println(isPrime(6834));

    }
}

java .\CheckPrimeOptimized.java

true

false

Call by value Example using java programming code

Call by value Example using java programming code 

public class CallByValue {

    public static void swap(int a, int b) {
        // Swap
        int temp = b;
        b = a;
        a = 35;

    }

    public static void main(String[] args) {
        // swap - value exchange
        int a = 45;
        int b = 25;
        swap(a, b);
        System.out.println("a is : " + a);
        System.out.println("b is : " + b);

    }
}


Product of a and b simple java programming code

Product of a and b 

public class FindProductAB {
    public static int multiply(int a, int b) {
        int product = a * b;
        return product;
    }

    public static void main(String[] args) {
        int Nov = 25;
        int Dec = 4;
        // int prod = multiply(3, 5);
        int prod = multiply(Nov, Dec);
        System.out.println(Nov + " * " + Dec + " = " + prod);
        prod = multiply(3, 5);
        System.out.println(prod);
    }
}


Simple Factorial Function using JAVA programming code

Factorial Function

public class FacrorialOfNumberN {

    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static void main(String[] args) {
        // int number = 9;
        // int fac = factorial(number);
        // System.out.println(fac);
        System.out.println(factorial(4));

    }
}

I love u bby 🤭

V.1 Find Binomial Coefficient using Factorial Function Java Programming Code

easy method Find Binomial Coefficient using Factorial Function Java Programming Code

public class BinomialCoefficient {
    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static void main(String[] args) {
        int n = 5, r = 2;
        int nmr = n - r;
        int factN = factorial(n);
        int factR = factorial(r);
        int factNMR = factorial(nmr);
        int BinCo = factN / (factR * factNMR);
        System.out.println(BinCo);

    }
}

fghd

V.2 Find Binomial Coefficient using Factorial Function Java Programming Code

Find Binomial Coefficient using Factorial Function Java Programming Code

public class BinCoeff {

    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static int BinCoeff(int n, int r) {
        int fact_n = factorial(n);
        int fact_r = factorial(r);
        int fact_nmr = factorial(n - r);
        int BinCoeff = fact_n / (fact_r * fact_nmr);
        return BinCoeff;
    }

    public static void main(String[] args) {
        System.out.println(BinCoeff(5, 2));

    }
}

🤭

Function Overloading using Parameters, Java Programming Code

Function Overloading using Parameters, Java Programming Code

public class functionOverLoading {
    // func to calc sum of 2 nums
    public static int sum(int a, int b) {
        return a + b;
    }

    // func to calc sum of 3 nums
    public static int sum(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        System.out.println(sum(5, 6));
        System.out.println(sum(5, 7, 12));
    }
}


Function Overloading using Data Types, Java programming Code

Data Types: Function Overloading using Data Types

public class functionOverLoadingDataTypes {

    // func to cal int sum
    public static int sum(int a, int b) {
        return a + b;
    }

    // func to cal float sum
    public static float sum(Float a, Float b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(sum(10, 15));
        System.out.println(sum(10.5f, 15.57f));

    }
}


Function: Check if a number is Prime or not Java programming code

Check if a number is Prime or not Function Code:

public class PrimeNumF1 {
    public static boolean isPrime(int n) {

        // corner cases
        // 2
        if (n == 2) {
            return true;
        }

        // boolean isPrime = true;
        for (int i = 2; i <= n - 1; i++) {
            if (n % i == 0) { // Completely dividing
                // isPrime = false;
                // We any use any technic in comment options.
                // break;
                // return isPrime;
                return false;
            }
        }
        // return isPrime;
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPrime(7907));
    }
}


java .\PrimeNumF1.java

true

NUMBER PYRAMID Pattern simple Java Programming code

 Output of Code:

java .\NumberPyramid.java
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
6 6 6 6 6 6






Java Code:

public class NumberPyramid {
    public static void main(String[] args) {
        int n = 6;
        // char ch = 'A';
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }

            System.out.println(" ");

        }
    }
}

Print Half Pyramid Character Pattern Java Programming Code

  Output:

Simple Java Code:

public class printCharacterPatternHalfPyramid {
    public static void main(String[] args) {
        int n = 7;
        char ch = 'A';
        for (int i = 1; i <= n; i++) {
            // number print
            for (int num = 1; num <= i; num++) {
                System.out.print(ch);
                ch++;
            }
            System.out.println();
        }
    }
}

...



java .\printCharacterPatternHalfPyramid.java

A

BC

DEF

GHIJ

KLMNO

PQRSTU

Half Pyramid Pattern Simple Java Programming Code

 This is Half Pyramid image, but in programming: this code is not fixed name, it's used to just define this code.




Half Pyramid Pattern Java Code:

public class HalfPyramid {
    public static void main(String[] args) {
        int n = 11;
        for (int i = 1; i <= n; i++) {
            // number print
            for (int num = 1; num <= i; num++) {
                System.out.print(num);
            }
            System.out.println();
        }
    }
}


Output: 

java .\HalfPyramid.java

1

12

123

1234

12345

123456

1234567

12345678

123456789

12345678910

1234567891011

Inverted Stars Pattern Java Programming Code (Using Nested Loop)

Using Nested Loop Inverted Star Pattern

import java.util.Scanner;

public class InvertedStarPattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int line = 1; line <= n; line++) {
            // one line
            for (int star = 1; star <= n - line + 1; star++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

Output: 

7

*******

******

*****

****

***

**

*

Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm. public class MaxSubArraySumKadanesALgorithm {     public static void Kadane ( int ...