sigma_course

mail@pastecode.io avatar
unknown
java
19 days ago
39 kB
4
Indexable
Never
import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int op=0,res;
        System.out.println("Enter a value :");
        int a = sc.nextInt();
        System.out.println("Enter b value :");
        int b = sc.nextInt();
        while(op != -1)
        {
            System.out.println("Chose an Operation (-1 to stop ):");
        System.out.println("1.Addition \n2.Subtraction \n3.Multiplication \n4.Division");
        op = sc.nextInt();
        switch (op) {
            case 1:res = a + b;
                System.out.println("Addition : "+res);
                break;
            case 2:res = a - b;
                System.out.println("Addition : "+res);
                break;
            case 3:res = a * b;
                System.out.println("Addition : "+res);
                break;
            case 4:res = a / b;
                System.out.println("Addition : "+res);
                break;
            default:System.out.println("Correct your choice ");
                break;
        }
    }
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int marks = 33;
        String str = (marks >= 33)?"pass":"fail";
        System.out.println(str);
    }

}




import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int a,b,c;
        System.out.println("Enter three numbers :");
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        if(a>b && a>c)
        {
            System.out.println(a+" is big");
        }
        else if(b>c)
        {
            System.out.println(b+" is big");
        }
        else
        {
            System.out.println(c+" is big");
        }
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int income;
        int tax=0;
        System.out.println("Enter your Income in Lacks:");
        income = sc.nextInt();
        if(income < 500000)
        {
            tax = 0;
        }
        else if(income >= 500000 && income <= 1000000)
        {
            tax =(int) (0.2*income);
        }
        else if(income > 1000000)
        {
            tax =(int) (0.3*income);
        }
        System.out.println("Tax you have to pay : "+tax);
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        if(a >= 0)
        {
            System.out.println("Positive Number");
        }
        else if(a < 0)
        {
            System.out.println("Nagative Number");
        }
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        double temp = 100;
        if(temp > 100)
        {
            System.out.println("You have fever");
        }
        else{
            System.out.println("No fever");
        }
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter day (1 - 7)");
        int day = sc.nextInt();
        switch(day){
            case 1:System.out.println("Monday");
            break;
            case 2:System.out.println("Tuesday");
            break;
            case 3:System.out.println("Wednesday");
            break;
            case 4:System.out.println("Thusrday");
            break;
            case 5:System.out.println("Friday");
            break;
            case 6:System.out.println("Satday");
            break;
            case 7:System.out.println("Sunday");
            break;
        }
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the year :");
        int year = sc.nextInt();
        if((year % 100 ==0 && year % 4 != 0) || (year % 400 ==0))
        {
            System.out.println("Leap Year");
        }
        else
        {
            System.out.println("Not a Leap Year");
        }
        
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        
        for(int i=1;i<=4;i++)
        {
            for(int j=1;j<=4;j++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
        
        
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        
       int n =1234;
       int rem;
       int sum=0;
       while(n>0)
       {
        rem = n % 10;
        sum = sum*10+rem;
        n = n/10;
       }
       System.out.println(sum);
        
    }

}







import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter a number");
            int n = sc.nextInt();
            if(n%10 == 0)
            {
                break;
            }
        }
        

        
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=4;
        int flag = 1;
        for(int i=2;i<=Math.sqrt(n);i++)
        {
            if(n%i == 0)
            {
                flag = 0;
                System.out.println("Not a prime");
                break;
            }
        }
        if(flag == 1)
        {
            System.out.println("Prime");
        }   
    }

}






import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=5;
        int sum = 1;
        while(n>0)
        {
            sum = sum*n;
            n--;
        }
        System.out.println(sum);
    }

}





import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number :");
        int n=sc.nextInt();
        for(int i = 1;i<=10;i++)
        {
            System.out.println(n+" * "+i+" = "+n*i);
        }
    }

}



#patterns.

1.
import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        for(int i = 1;i <= 4;i++)
        {
            for(int j = 1;j<=i ;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
    }

}

oputput:
*
**
***
****

2.
import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n=4;
        for(int i = 1;i <= 4;i++)
        {
            for(int j = n;j>=i ;j--)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
    }

}

(or)

import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 4;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j<=n-i+1 ;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
    }

}

output:
****
***
**
*




import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 4;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j<=i ;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        
    }

}

output:
1
12
123
1234





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

}
output:
A
BC
DEF
GHIJ



#patterns(part2)

import java.util.*;
public class Basics{
    
    public static void main(String args[]){
        int row = 3;
        int col = 7;
        for(int i=1; i<=row;i++)
        {
            for(int j=1;j<=col;j++)
            {
                if(i == 1 || i == row)
                {
                    System.out.print("*");
                }
                else{
                    if(j ==1 || j ==col )
                    {
                        System.out.print("*");
                    }
                    else{
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
    }

}
output:
*******
*       *
*******


#iverted half-pyramid:
import java.util.*;
public class Basics{
    
    public static void main(String args[]){
        int n = 7;
        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("*");
            }
            System.out.println();
        }
    }

}
output:
     *
    **
   ***
  ****
 *****
******




import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 4;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            for(int j =1;j<=2*(n-i);j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i=n;i>=1;i--)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            for(int j =1;j<=2*(n-i);j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
    }

}
output:
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *




import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 5;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j =1;j<=n;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
        
    }

}
output:
    *****
   *****
  *****
 *****
*****






import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 5;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j =1;j<=n;j++)
            {
                if(i==1 || i==n || j==1 || j == n)
                {
                    System.out.print("*");
                }
                else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        
        
    }

}
output:
    *****
   *   *
  *   *
 *   *
*****






import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 4;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=(2*i)-1;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i=n;i>=1;i--)
        {
            for(int j=1;j<=n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=(2*i)-1;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        
        
    }

}
output:
   *
  ***
 *****
*******
*******
 *****
  ***
   *








import java.util.*;
public class Basics{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = 4;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j<=i ;j++)
            {
                if((i+j)%2 == 0)
                {
                    System.out.print(1);
                }
                else{
                    System.out.print(0);
                }
            }
            System.out.println();
        }
        
    }

}
output:
1
01
101
0101




import java.util.*;
public class Basics{

    public static int calSum(int a,int b){
        int res = a+b;
        return res;
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int res = calSum(a,b);
        System.out.println("sum is : "+res);
    }

}






import java.util.*;
public class Basics{
    public static int fact(int n){
        int fact =1;
        for(int i = 1; i <= n; i++)
        {
            fact = fact * i;
        }
        return fact;
    }
    public static int binomialCoeficient(int n, int r){
        int nf = fact(n);
        int rf = fact(r);
        int nmr = fact(n-r);
        int bin = nf / (nmr * rf);
        return bin;
    }
    
    public static void main(String args[]){
        int n = 5;
        int r = 2;
        int res = binomialCoeficient(n,r);
        System.out.println("Binomial Coefficient : "+res);
    }

}





import java.util.*;
public class Basics{
    public static int addSum(int a, int b){
        return a+b;
    }
    public static float addSum(float a, float b){
        return a+b;
    }
    
    public static void main(String args[]){
        int a = 5;
        int b = 2;
        float c = 3.3f;
        float d = 2.4f;
        int res1 = addSum(a,b);
        float res2 = addSum(c,d);
        System.out.println(res1);
        System.out.println(res2);
    }

}





import java.util.*;
public class Basics{
    public static String prime(int n){
        int flag = 1;
        for(int i = 2; i<=Math.sqrt(n);i++)
        {
            if(n%i == 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
        {
            return "Prime Number";

        }
        else{
            return "Not Prime Number";
        }
    }
    
    public static void main(String args[]){
        int a = 6;
        String res = prime(a);
        System.out.println(res);
    }

}






import java.util.*;
public class Basics{
    public static boolean prime(int n){
        for(int i = 2; i<=Math.sqrt(n);i++)
        {
            if(n%i == 0)
            {
                return false;
            }
        }
        return true;
        
    }
    public static void countPrime(int n){
        for(int i=1;i<=n;i++)
        {
            boolean res = prime(i);
            if(res == true)
            {
                System.out.println(i);
            }
        }
    }
    
    public static void main(String args[]){
        int n = 100;
        countPrime(n);
    }

}





#binary --> Decimal
import java.util.*;
public class Basics{
    public static void convert(int n){
        int sum = 0;
        int rem, i = 0;
        while(n>0)
        {
            rem = n%10;
            sum += rem * Math.pow(2,i);
            i++;
            n = n/10;
        }
        System.out.println(sum);
    }
    
    public static void main(String args[]){
        int n = 1001;
        convert(n);
    }

}




#Decimal   --> Binary:
import java.util.*;
public class Basics{
    public static void convert(int n){
        int sum = 0;
        int rem, i = 0;
        while(n>0)
        {
            rem = n%2;
            sum = sum+rem*((int)Math.pow(10,i));
            n = n/2;
            i++;
        }
        System.out.println(sum);
    }
    
    public static void main(String args[]){
        int n = 10;
        convert(n);
    }

}





import java.util.*;
public class Basics{
    
    public static double average(int a, int b, int c){
        double avg = ((float)(a+b+c))/3;
        return avg;
    }
    public static void main(String args[]){
        int a = 10;
        int b = 20;
        int c = 31;
        double res = average(a,b,c);
        System.out.println(res);
    }

}






import java.util.*;
public class Basics{
    
    public static String palindrome(int n){
        int temp = n;
        int sum = 0,rem;
        while(n>0)
        {
            rem = n % 10;
            sum = (sum*10)+rem;
            n = n/10;
        }
        if(temp == sum)
        {
            return "pal";
        }
        else{
            return "non pal";
        }
    }
    public static void main(String args[]){
        int a = 103;
        String res = palindrome(a);
        System.out.println(res);
    }

}







#towers of hanoi

import java.util.*;
public class Basics{
    
    public static void toh(int n, char a,char b, char c){
        if(n>=1)
        {
            toh(n-1,a,c,b);
            System.out.println(a+"-->"+b);
            toh(n-1,c,b,a);
        }
    }
    public static void main(String args[]){
        int n  = 3;
        char a='A',b = 'B',c = 'C';
        toh(n,a,b,c);
    }

}






#call by value,call by reference;
import java.util.*;
public class Basics{
    public static void update(int marks[],int a){
        for(int i=0;i<marks.length;i++)
        {
            marks[i] += 1;
        }
        a++;

    }
    public static void main(String args[]){
        int nonChange = 5;//pass by value(Change in Formal parameters does not effect the actual parameters )
        int marks[] = {90,91,92};//pass by reference(Change in Formal parameters effect the actual parameters )
        update(marks,nonChange);
        for(int i=0; i<marks.length;i++)
        {
            System.out.println(marks[i]);
        }
        System.out.println(nonChange);
    }

}






#linear search:
import java.util.*;
public class Basics{
    public static int linearSearch(int nums[],int tar)
    {
        for(int i=0;i<nums.length;i++)
        {
            if(nums[i] == tar)
            {
                return i;
            }
        }
        return -1;
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        int target = 6;
        int res = linearSearch(arr,target);
        if(res != -1)
        System.out.println("The Elment is found at "+res+" index");
        else
        System.out.println("Element not found");
    }

}





#Binary Search:
import java.util.*;
public class Basics{
    public static int binarySearch(int nums[],int low, int high,int target){
        int mid;
        while(low<=high)
        {
            mid = (low + high)/2;
            if(nums[mid] == target)
            {
                return mid;
            }
            else if(target < nums[mid])
            {
                high = mid - 1;
            }
            else
            {
                low = mid+1;
            }
        }
        return -1;
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        int low = 0,high = arr.length-1;
        int target = 6;
        int res = binarySearch(arr,low,high,target);
        if(res != -1)
        System.out.println("the element is found at "+res+" th index");
        else
        System.out.println("the element not found");
    }

}






#largest element,smallest element:
import java.util.*;
public class Basics{
    public static void findLargest(int nums[])
    {
        int largest = Integer.MIN_VALUE;
        int smallest = Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
        {
            if(largest < nums[i])
            {
                largest = nums[i];
            }
            if(smallest > nums[i])
            {
                smallest = nums[i];
            }
        }
        System.out.println("Largest Value :"+largest);
        System.out.println("Smallest Value :"+smallest);
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        findLargest(arr);
    }

}




#reversing an array:
import java.util.*;
public class Basics{
    public static void reverse(int nums[]){
        int first = 0,last = nums.length-1;
        while(first<last)
        {
            int temp = nums[first];
            nums[first] = nums[last];
            nums[last] = temp;
            first++;
            last--;
        }
        for(int i = 0;i<nums.length;i++)
        {
            System.out.println(nums[i]);
        }
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8};
        reverse(arr);
        
    }

}



pairs:
import java.util.*;
public class Basics{
    public static void pairs(int nums[]){
        for(int i=0;i<nums.length-1;i++)
        {
            for(int j=i+1;j<nums.length;j++)
            {
                System.out.print("("+nums[i]+","+nums[j]+")");
            }
            System.out.println();
        }
       
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        pairs(arr);
        
    }

}





#sub arrays:
import java.util.*;
public class Basics{
    public static void subArrays(int nums[]){
        for(int i=0;i<nums.length;i++)
        {
            for(int j=i;j<nums.length;j++)
            {
                for(int k = i;k <= j;k++)
                {
                    System.out.print(nums[k]+" ");
                }
                System.out.print(" ");
            }
            System.out.println();
        }
       
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        subArrays(arr);
        
    }

}






#maxsum,minsum of subarrays:(brute force)
import java.util.*;
public class Basics{
    public static void subArrays(int nums[]){
        int maxSum = Integer.MIN_VALUE;
        int minSum = Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
        {
            for(int j=i;j<nums.length;j++)
            {
                int sum = 0;
                for(int k = i;k <= j;k++)
                {
                    sum += nums[k];
                }
                if(sum > maxSum)
                {
                    maxSum = sum;
                }
                if(sum < minSum)
                {
                    minSum = sum;
                }
            }
        }
        System.out.println(maxSum);
        System.out.println(minSum);
       
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        subArrays(arr);
        
    }

}






#maxsum,minsum of subarrays:(Prefix sum)
import java.util.*;
public class Basics{
    public static void subArrays(int nums[])
    {
        int n = nums.length;
        int minSum = Integer.MAX_VALUE;
        int maxSum = Integer.MIN_VALUE;
        int prefix[] = new int[n];
        prefix[0] = nums[0];
        for(int i=1;i<n;i++)
        {
            prefix[i] = prefix[i-1] + nums[i];
        }
        for(int i=0;i<n;i++)
        {
            int start = i;
            for(int j=i;j<n;j++)
            {
                int end = j;
                int sum = 0;
                sum = (start == 0) ? prefix[end] : prefix[end] - prefix[start - 1];
                if(sum > maxSum)
                {
                    maxSum = sum;
                }
                if(sum < minSum)
                {
                    minSum = sum;
                }
            }
        }
        System.out.println("Max sum : "+maxSum);
        System.out.println("Min sum : "+minSum);
    }
    public static void main(String args[]){
        int arr[] = {2,4,6,8,10};
        subArrays(arr);
        
    }

}



#maxsum of subarrays:(Kadane's Algorithm)
import java.util.*;
public class Basics{
    public static void subArrays(int nums[])
    {
        int n = nums.length;
        int curSum = 0;
        int maxSum = Integer.MIN_VALUE;
        for(int i=0;i<n;i++)
        {
            curSum += nums[i];
            if(curSum < 0)
            {
                curSum = 0;
            }
            if(curSum > maxSum)
            {
                maxSum = curSum;
            }

        }
        System.out.println("Max sum : "+maxSum);
    }
    public static void main(String args[]){
        int arr[] = {-2,-3,4,-1,-2,1,5,-3};
        subArrays(arr);
        
    }

}





#TrapRainWater:
import java.util.*;
public class Basics{
    public static void trapRainWater(int nums[])
    {
        int n = nums.length;
        int waterLevel,width = 1,trapWater = 0;
        int leftBound[] = new int[n];
        int rightBound[] = new int[n];
        leftBound[0] = nums[0];
        rightBound[n-1] = nums[n-1];
        for(int i=1;i<n;i++)
        {
            leftBound[i] = Math.max(leftBound[i-1],nums[i]);
        }
        for(int i=n-2;i>=0;i--)
        {
            rightBound[i] = Math.max(rightBound[i+1],nums[i]);
        }
        for(int i=0;i<n;i++)
        {
            waterLevel = Math.min(leftBound[i],rightBound[i]);
            trapWater += (waterLevel - nums[i])*width;
        }
        System.out.println("Trapped Water : "+trapWater+" cc");
        
    }
    public static void main(String args[]){
        int arr[] = {4,2,0,6,3,2,5};
        trapRainWater(arr);
        
    }

}




#sorting techniques:

import java.util.*;
public class Basics{
    public static void bubbleSort(int nums[])
    {
        int n = nums.length;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-1-i;j++)
            {
                if(nums[j]>nums[j+1])
                {
                    int temp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = temp;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            System.out.println(nums[i]);
        }
    }
    public static void selectionSort(int nums[])
    {
        int n = nums.length;
        for(int i=0;i<n-1;i++)
        {
            int minPos = i;
            for(int j=i+1;j<n;j++)
            {
                if(nums[minPos] > nums[j])
                {
                    minPos = j;
                }
            }
            int temp = nums[i];
            nums[i] = nums[minPos];
            nums[minPos] = temp;
        }
        for(int i = 0;i<n;i++)
        {
            System.out.println(nums[i]);
        }
    }
    public static void main(String args[]){
        int arr[] = {-1, 0,  1, 2, -1, -4};
        selectionSort(arr);
        
    }

}






#quickSort:
class HelloWorld {
    public static int partition(int a[],int p,int q)
    {
        int i = p;
        int j = q;
        int pivot = a[p];
        while(i < j)
        {
            while(i < q && pivot >= a[i])
            {
                i++;
            }
            while(j > p && pivot < a[j])
            {
                j--;
            }
            if(i < j)
            {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        int t = pivot;
        a[p] = a[j];
        a[j] = t;
        return j;
    }
    public static void quickSort(int a[],int p,int q)
    {
        if(p < q)
        {
            int j = partition(a,p,q);
            quickSort(a, p, j-1);
            quickSort(a, j+1, q);
        }
        else if(p == q)
        {
            // Base case: subarray has only one element
            return;
        }
    }
    public static void main(String[] args) {
        int arr[] = {4,1,5,3,2};
        quickSort(arr,0,arr.length-1);
        for(int i=0;i<arr.length;i++)
        {
            System.out.println(arr[i]);
        }
    }
}






#mergeSort:
import java.util.*;

public class Student {
    public static int[] merge(int arr1[],int arr2[]){
        int n1 = arr1.length;
        int n2 = arr2.length;
        int res[] = new int[n1+n2];
        int i=0;
        int j=0;
        int k=0;
        while(i<n1 && j<n2)
        {
            if(arr1[i]<arr2[j])
            {
                res[k] = arr1[i];
                i++;
            }else{
                res[k] = arr2[j];
                j++;
            }
            k++;
        }
        while(i<n1)
        {
            res[k] = arr1[i];
            i++;
            k++;
        }
        while(j<n2)
        {
            res[k] = arr2[j];
            j++;
            k++;
        }
        return res;
    }
    public static int[] mergeSort(int arr[],int si,int ei){
        if(si == ei)
        {
            int ba[] = new int[1];
            ba[0] = arr[si];
            return ba;
        }
        int mid = (si+ei)/2;
        int res1[] = mergeSort(arr, si,mid);
        int res2[] = mergeSort(arr, mid+1, ei);
        int res[] = merge(res1, res2);
        return res;
    }
    public static void main(String args[]) {
        int arr[] = {5,4,3,2,1};
        int res1[] = mergeSort(arr, 0, arr.length-1);
        for(int i=0;i<res1.length;i++)
        {
            System.out.print(res1[i]+" ");
        }
    }
}





#2D-Arrays:
import java.util.*;
public class Basics{
    public static int maxSearch(int arr[][])
    {
        int maxEle = Integer.MIN_VALUE;
        for(int i=0;i<arr.length;i++)
        {
            for(int j=0;j<arr[0].length;j++)
            {
                if(arr[i][j] > maxEle)
                {
                    maxEle = arr[i][j];
                }
            }
        }
        return maxEle;
    }
    public static boolean search(int arr[][],int target){
        for(int i=0;i<arr.length;i++)
        {
            for(int j=0;j<arr[0].length;j++)
            {
                if(arr[i][j] == target)
                {
                    System.out.println("The element is found at ("+i+","+j+") th index");
                    return true;
                }
            }
        }
        System.out.println("Element not found");
        return false;
    }
    public static void create(int arr[][]){
        int m = arr.length,n = arr[0].length;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter matrix elements :");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                arr[i][j] = sc.nextInt();
            }
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
        

    }
    
    public static void main(String args[]){
        int arr[][] = new int[3][3];
        create(arr);
        boolean a = search(arr,5);
        System.out.println(a);
        int maxEle = maxSearch(arr);
        System.out.println("Max element : "+maxEle);
    }

}








#spiral matrix:
import java.util.*;
public class Basics{
    public static void printSpiral(int nums[][]){
        int startRow = 0;
        int startCol = 0;
        int endRow = nums.length-1;
        int endCol = nums[0].length-1;
        while(startRow <= endRow && startCol <= endCol)
        {
            //top
        for(int j=startCol;j<=endCol;j++)
        {
            System.out.print(nums[startRow][j]+" ");
        }
        //right
        for(int i=startRow+1;i<=endRow;i++)
        {
            System.out.print(nums[i][endRow]+" ");
        }
        //bottom
        for(int j=endCol-1;j>=startCol;j--)
        {
            if(startRow == endRow)
            {
                break;
            }
            System.out.print(nums[endRow][j]+" ");
        }
        //left
        for(int i=endRow-1;i>=startRow+1;i--)
        {
            if(startCol == endCol)
            {
                break;
            }
            System.out.print(nums[i][startCol]+" ");
        }
        startRow++;
        endRow--;
        startCol++;
        endCol--;
        }
        System.out.println();
        

    }
    public static void main(String args[]){
        int arr[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        printSpiral(arr);
    }

}





#diogonalSum:

import java.util.*;
public class Basics{
    public static int diogonalSum(int nums[][]){
        int pd=0,sd=0,tSum =0;
        int n = nums.length;
        if(n%2 == 0){
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<nums[0].length;j++)
                {
                    if(i==j)
                    {
                    pd += nums[i][j];
                    }
                    if(i+j == n-1)
                    {
                        sd += nums[i][j];
                    }
                }
            }
        }
        else{
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<nums[0].length;j++)
                {
                    if(i==j)
                    {
                        pd += nums[i][j];
                    }
                    if(i+j == n-1 && i != j)
                    {
                        sd += nums[i][j];
                    }
                }
            }
        }
        tSum = pd + sd;
        System.out.println(pd);
        System.out.println(sd);
        return tSum;
        

    }
    
    public static void main(String args[]){
        int arr[][] ={{1,2,3},{4,5,6},{7,8,9}};
        int res = diogonalSum(arr);
        System.out.println("Diagonal sum : "+res);
    }

}


(or)
import java.util.*;
public class Basics{
    public static int diogonalSum(int nums[][]){
        int pd=0,sd=0,tSum =0;
        int n = nums.length;
        for(int i=0;i<n;i++)
        {
            pd += nums[i][i];
            if(i != n-i-1)
            {
                sd += nums[i][n-i-1];
            }
        }
        tSum = pd + sd;
        System.out.println(pd);
        System.out.println(sd);
        return tSum;
    }
    
    public static void main(String args[]){
        int arr[][] ={{1,2,3},{4,5,6},{7,8,9}};
        int res = diogonalSum(arr);
        System.out.println("Diagonal sum : "+res);
    }

}





#shortest sum:
import java.util.*;
public class Basics{
    public static int shortestPath(String s){
        int x = 0;
        int y = 0;
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i) == 'W')
            {
                x--;
            }
            if(s.charAt(i) == 'E')
            {
                x++;
            }
            if(s.charAt(i) == 'S')
            {
                y--;
            }
            if(s.charAt(i) == 'N')
            {
                y++;
            }
        }
        int res = (int)Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
        return res;
    }
    
    public static void main(String args[]){
        String s = "WNEENESENNN";
        int res = shortestPath(s);
        System.out.println("The shortest path from the origin is : "+res+" units");
        

    }

}





#largest of strings:
import java.util.*;
public class Basics{
    public static void main(String args[]){
        String str[] = {"apple","mango","banana"};
        String largest = str[0];
        for(int i=1;i<str.length;i++)
        {
            if(largest.compareTo(str[i]) < 0)
            {
                largest = str[i];
            }
        }
        System.out.println(largest);
    }

}











Assaignment questions(DSA):
1.
import java.util.*;
public class Basics{
    public static boolean isRepeated(int nums[]){
        int n = nums.length;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(nums[i] == nums[j])
                {
                    return true;
                }
            
            }
        }
        return false;
    }
    public static void main(String args[]){
        int arr[] = {1,2,3,4};
        boolean res = isRepeated(arr);
        System.out.println(res);
        
    }

}



2.

Leave a Comment