Monday, March 28, 2022

Missing Number in an Array

Program -

public class missingVal{

     public static void main(String []args){
        int n = 6;
        int array[] = {2,4,6,5,1};
        int sumN = (n*(n+1))/2;
        int sum = 0;
        for(int i = 0; i<n-1; i++)
        {
            sum += array[i];
        }
        System.out.println(sumN - sum);
     }
}  

Video explanation -


 

Wednesday, March 23, 2022

Strong Numbers

Program -

public class Strong{
    
    public static int fact(int x)
    {
        int f = 1;
        for(int i = x; i>1; i--)
        {
            f = f*i;
        }
        return f;
    }
    
     public static void main(String []args){
        int n = 145;
        int copy = n,temp,strong = 0;
        while(n>0)
        {
            temp = n%10;
            strong = strong + fact(temp);
            n = n/10;
        }
        if(copy == strong)
            System.out.println("Yes");
        else
            System.out.println("No");
     }
}

Video explanation -


 

Tuesday, March 22, 2022

Find common elements in three sorted arrays

Program -

public class common{

     public static void main(String []args){
        
        int ar1[] = {1, 5, 10, 20, 40, 80};
        int ar2[] = {6, 7, 20, 80, 100};
        int ar3[] = {3, 4, 15, 20, 30, 70, 80, 120};
        int i = 0, j = 0, k = 0;
        
        while (i < ar1.length && j < ar2.length && k < ar3.length)
        {
            if (ar1[i] == ar2[j] && ar2[j] == ar3[k])
            {   
                System.out.print(ar1[i]+" ");   
                i++; j++; k++;
            }
            else if (ar1[i] < ar2[j])
                 i++;
            else if (ar2[j] < ar3[k])
                 j++;
            else
                 k++;
        }
     }
}

Video explanation -


 

 

Saturday, March 19, 2022

Largest of Three Numbers

Program -

public class largestAmong3{

     public static void main(String []args){
        int x = 51, y = 11, z = 9;
        
        if(x >= y && x >= z)
            System.out.println(x);
        else if(y >= x && y >= z)
            System.out.println(y);
        else
            System.out.println(z);
     }
}

 

Video explanation -


 

 

Thursday, March 17, 2022

Count Pairs with given Sum

 

Program -

public class pairSum{

     public static void main(String []args){
        int[] arr = { 1, 5, 7, -1, 5 };
        int sum = 6,count = 0;
        
        for (int i = 0; i < arr.length; i++)
        {    
            for (int j = i + 1; j < arr.length; j++)
            {
                if ((arr[i] + arr[j]) == sum)
                    count++;     
            }
        }
        System.out.println(count);
     }
}

 

Video explanation -


 

 

 

Reverse an Array

 

Program -

import java.util.Arrays;
public class reverseArray{

     public static void main(String []args){
        int arr[] = {1, 2, 3, 4, 5, 6};
        int i=0, j=arr.length-1,temp;
        while(i<j)
        {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        System.out.println(Arrays.toString(arr));
     }
}

 

Video explanation -


 

 

Wednesday, March 16, 2022

Peak Element in an Array | Java | Program to find Peak Element in an Array

 

Program -

public class peak{
     public static void main(String []args){
        int arr[] = {1,2,5,4,10,9,8,7,6};
        int n = arr.length, peak = 0;
        if(n == 1)
        {
            System.out.print(arr[0]);
            System.exit(0);
        }
       for(int i = 0; i<n; i++)
       {
           if(i==0)
            {   if(arr[i+1] > arr[i])
                    peak = arr[i+1];

            }
           else if(i == n-1)
            {   if(arr[i-1] < arr[i])
                    peak = arr[i];

            }
           else
            {    if(arr[i-1] < arr[i] && arr[i+1] < arr[i])
                    peak = arr[i];

            }
       }
        System.out.println(peak);
     }


Video explanation -


 

Tuesday, March 15, 2022

Cyclically Rotate Array by One

 

Program - 

import java.util.Arrays;
public class rotate{

     public static void main(String []args){
        int arr[] = {1, 2, 3, 4, 5};
        
        int x = arr[arr.length-1], i;
       for (i = arr.length-1; i > 0; i--)
       {
           arr[i] = arr[i-1];
       }
       arr[0] = x;
        System.out.println(Arrays.toString(arr));
     }
}

 

Video explanation -

 


 

 

Friday, March 11, 2022

Move all Negative Numbers to beginning of Array

 

Program - 

 import java.util.Arrays;
public class negativeToLeft{

     public static void main(String []args){
        int arr[] = { -1, 2, -3, 4, 5, 6, -7, 8, 9 };
        
        int j = 0, temp;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < 0) {
                if (i != j) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
                j++;
            }
        }
        System.out.println(Arrays.toString(arr));
     }
}
 

Video explanation -


 

 

Monday, March 7, 2022

Kth Largest Number in an Array

 

Program -

public class KLargest{

     public static void main(String []args){
         
        int a[] = {1,2,3,4,5,6,7,8};
        int max = 0;
        int index = 0;
        int k = 3;
        for(int j = 1; j <= k; j++)
        {
            max = a[0];
            for(int i = 0; i < a.length; i++)
            {
                if(a[i] > max)
                {
                    max = a[i];
                    index = i;
                }
            }
            a[index] = Integer.MIN_VALUE;
        }
        System.out.println(max);
     }
}

Video explanation -


 

Sunday, March 6, 2022

ANAGRAM

 

Program -

import java.util.*;
public class Anagram{
     public static void main(String []args){
        String str1 = "SILENT";
        String str2 = "LISTEN";
        char c1[] = str1.toCharArray();
        char c2[] = str2.toCharArray();
        if(c1.length != c2.length)
        {
            System.out.println("Not Anagram");
            System.exit(0);
        }
        Arrays.sort(c1);
        Arrays.sort(c2);
        for (int i = 0; i < c1.length; i++)
        {
            if (c1[i] != c2[i])
            {   
                System.out.println("Not Anagram");
                System.exit(0);
            }
        }
        System.out.println("Anagram");
     }
}

 

Video explanation -

 


 

A Program to check if strings are rotations of each other or not

 

Program - 

public class CheckRotation{

     public static void main(String []args){
        
        String str1 = "ABCD";
        String str2 = "CDAB";
        
        if(str1.length() != str2.length())
        {
            System.out.println("Not a Rotation");
            System.exit(0);
        }
        
        String join = str1 + str1;
        
        if(join.indexOf(str2) != -1)
            System.out.println("Rotation");
        else
            System.out.println("Not a Rotation");
     }
 
 

Video explanation -