× Home Related to First Java Program Conditionals and loops Functions Arrays

Assignments

Programs related to First java program

1→ Write a program to print whether a number is even or odd, also take input from the user.

                            

                                import java.util.Scanner;

                                public class IsEvenOdd {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int num = in.nextInt();
                                        in.close();
                                        if(num%2 == 0){
                                            System.out.println(num+" is even number");
                                        } else {
                                            System.out.println(num+" is odd number");
                                        }
                                    }
                                }
                            
                        

2→ Take name as input and print a greeting message for that particular name.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        String name = in.next();
                                        in.close();
                                        System.out.println("Hi "+name+" Good morining!");
                                        
                                    }
                                }
                            
                        

3→ Write a program to input principal, time, and rate (P, T, R) from the user and find Simple Interest.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int P = in.nextInt();
                                        int T = in.nextInt();
                                        int R = in.nextInt();
                                        int simpleInterest = P * T * R ;
                                        in.close();
                                        System.out.println("Simple Interest is = "+simpleInterest);		
                                    }
                                }
                            
                        

4→ Take in two numbers and an operator (+, -, *, /) and calculate the value. (Use if conditions)

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args){
                                        Scanner in = new Scanner(System.in);
                                        int num1 = in.nextInt();
                                        int num2 = in.nextInt();
                                        char op = in.next().trim().charAt(0);
                                        in.close();
                                        if(op=='+'){
                                            System.out.println(num1+num2);
                                        }
                                        if(op=='-'){
                                            System.out.println(num1-num2);
                                        }
                                        if(op=='/'){
                                            if(num2!=0){
                                                System.out.println(num1/num2);
                                            } else {
                                                System.out.println("Not defined");
                                            }
                                        }
                                        if(op=='*'){
                                            System.out.println(num1*num2);
                                        }			
                                    }
                                }
                            
                        

5→ Take 2 numbers as input and print the largest number.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        int num1 = in.nextInt();
                                        int num2 = in.nextInt();
                                        in.close();
                                        if (num1 > num2) {
                                            System.out.println(num1 + " is greater");
                                        } else {
                                            System.out.println(num2 + " is greater");
                                        }
                                    }
                                }
                            
                        

6→ Input currency in rupees and output in USD.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        float rupees = in.nextFloat();
                                        in.close();
                                        double dollars = rupees/79.38;
                                        System.out.println(rupees+" is "+dollars);
                                    }
                                }
                            
                        

7→ Input currency in rupees and output in USD.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args) {
                                
                                        Scanner in = new Scanner(System.in);
                                        int a = 0;
                                        int b = 1;
                                        int sum = 0;
                                        int n = in.nextInt();
                                        in.close();
                                        if (n == 1) {
                                            System.out.println("At 1 fib = 0");
                                        } else if (n == 2) {
                                            System.out.println("At 1 fib = 1");
                                        } else {
                                            for (int i = 3; i <= n; i++) {
                                                sum = a + b;
                                                a = b;
                                                b = sum;
                                            }
                                            System.out.println("At " + n + " fib no is =" + b);
                                        }
                                
                                    }
                                }
                            
                        

8→ To find out whether the given String is Palindrome or not.

                            

                                import java.util.Scanner;

                                public class Test {
                                    public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        String check = in.next();
                                        in.close();
                                        int j = check.length()-1;
                                        boolean isPalindrome=false;
                                        for (int i = 0; i < check.length() / 2; i++) {
                                            if (check.charAt(i) == check.charAt(j)) {
                                                isPalindrome = true;
                                            } else {
                                                isPalindrome = false;
                                            }
                                            j--;
                                        }
                                        if(isPalindrome){
                                            System.out.println(check+" is Palindrome");
                                        } else{
                                            System.out.println(check+" is not a Palindrome");
                                        }		
                                    }
                                }
                            
                        

9→ To find Armstrong Number between two given number.

An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of their digits. It is also known as pluperfect, or Plus Perfect, or Narcissistic number. It is an OEIS sequence A005188. Let’s understand it through an example.
Armstrong Number Example
1: 11 = 1

2: 21 = 2

3: 31 = 3

153: 13 + 53 + 33 = 1 + 125+ 27 = 153

125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)

1634: + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643

                            

                                import java.util.Scanner;

                                public class test2 {
                                    public static void main(String[] args) {
                                        Scanner in = new Scanner(System.in);
                                        int num1 = in.nextInt();
                                        int num2 = in.nextInt();
                                        in.close();
                                        printArm(num1, num2);
                                    }
                                
                                    // print every three digit armstrong numbers
                                
                                    static void printArm(int num1, int num2) {
                                
                                        for (int i = num1; i < num2; i++) {
                                            int digits = 0;
                                            int temp = i;
                                            int sum = 0;
                                            while (temp > 0) {
                                                temp = temp / 10;
                                                digits++;
                                            }
                                
                                            temp = i;
                                            while (temp > 0) {
                                                int d = temp % 10;
                                                sum = sum + (int) (Math.pow(d, digits));
                                                temp/=10;
                                            }
                                            if (sum == i) {
                                                System.out.println(i + " is Armstrong number");
                                            }
                                        }
                                    }
                                
                                }
                                
                            
                        

Conditionals and loops

Basic Java Programs

1→ Area of Circle

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            System.out.print("Enter radius : ");
                                            int radius = in.nextInt();
                                            in.close();
                                            float area = (float) (Math.PI)*radius*radius;
                                            System.out.println(area+" is area of circle whose radius is = "+radius);
                                        }
                                    }
                                
                        
  • This covers all these questions
    • Area Of Rectangle Program
    • Area Of Isosceles Triangle
    • Area Of Parallelogram
    • Area Of Rhombus
    • Area Of Equilateral Triangle

2→ Perimeter Of Parallelogram

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            // area of circle
                                            Scanner in = new Scanner(System.in);
                                            System.out.print("Enter sides of parallelogram : ");
                                            int side1 = in.nextInt();
                                            int side2 = in.nextInt();
                                            in.close();
                                            int para = 2*(side1 + side2);
                                            System.out.println(para+" is parameter of a paralellogram");
                                        }
                                    }
                                
                            
  • This covers all these questions
    • Perimeter Of Circle
    • Perimeter Of Equilateral Triangle
    • Perimeter Of Rectangle
    • Perimeter Of Square
    • Perimeter Of Rhombus

3→ Volume Of Cone

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            // area of circle
                                            Scanner in = new Scanner(System.in);
                                            System.out.print("Enter h of a cone : ");
                                            double h = in.nextDouble();
                                            System.out.print("Enter r of a cone : ");
                                            double r = in.nextDouble();
                                            in.close();
                                            double vol = (22*r*r*h)/(3*7);
                                            System.out.println(vol+" is the volume of a cone");
                                        }
                                    }
                                
                            
  • This covers all these questions
    • Volume Of Prism
    • Volume Of Cylinder
    • Volume Of Sphere
    • Volume Of Pyramid

4→ Subtract the Product and Sum of Digits of an Integer

                                

                                    public class Test {
                                        public static void main(String[] args) {
                                            int num = 999;
                                            int sum =0;
                                            int product = 1;
                                            int temp = num;
                                            while(temp>0){
                                                int last = temp%10;
                                                sum+=last;
                                                temp/=10;
                                            }
                                            temp = num;
                                            while(temp>0){
                                                int last = temp%10;
                                                product*=last;
                                                temp/=10;
                                            }
                                            int sub = product-sum;
                                            System.out.println(product+" - "+sum +" = "+ sub);
                                        }
                                    }
                                
                            

5→ Input a number and print all the factors of that number (use loops)

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            for (int i = 1; i <= num/2; i++) {
                                                if(num%i==0){
                                                    System.out.print(i+" ");
                                                }
                                            }
                                        }
                                    }
                                
                            

6→ Take integer inputs till the user enters 0 and print the sum of all numbers (HINT: while loop)

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            // Input a number and print all the factors of that number (use loops)
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            int large = 0;
                                            while (num != 0) {
                                                if(num>large){
                                                    large = num;
                                                }
                                                num = in.nextInt();
                                            }
                                            in.close();
                                            System.out.println("largest number is = "+large);
                                        }
                                    }
                                
                            

7→ Take integer inputs till the user enters 0 and print the largest number from all

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            // Input a number and print all the factors of that number (use loops)
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            int sum = 0;
                                            while (num != 0) {
                                                sum += num;
                                                num = in.nextInt();
                                            }
                                            in.close();
                                            System.out.println("total sum is = "+sum);
                                        }
                                    }
                                
                            

Intermediate Java Programs

1→ Factorial Program

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            in.close();
                                            System.out.println(factorial(num));
                                        }
                                    
                                        public static long factorial(int n) {		
                                            long product = 1;
                                            while(n!=1)
                                            {
                                                product = product * n;
                                                n--;
                                            }
                                            return product;
                                        }
                                    }
                                
                        

2→ Calculate Electricity Bill

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int units = in.nextInt();
                                            in.close();
                                            billGen(units);		
                                        }
                                    
                                        public static void billGen(int units){
                                            System.out.println("Fixed charges : 120");
                                            System.out.println("Charges per unit = 5.5 Rupee");
                                            System.out.println("Your amount for "+units+" is : "+units*5.5);
                                        }
                                    }
                                
                            

3→ Calculate Average Of N Numbers

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            int sum = 0;
                                            int totalNum = 0;
                                            while (num != 0) {
                                                sum += num;
                                                totalNum++;
                                                num = in.nextInt();
                                            }
                                            in.close();
                                            System.out.println(sum);
                                            System.out.println(totalNum);
                                            float avg =(float) (sum) / (float) (totalNum);
                                            System.out.println("The average of numbers that you entered is : " + avg);
                                        }
                                    }
                                
                            

4→ Calculate Distance Between Two Points

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            System.out.println("Enter the coordinate of first point ");
                                            System.out.print("x: ");
                                            int x1 = in.nextInt();
                                            System.out.print("y: ");
                                            int y1 = in.nextInt();
                                            System.out.println("Enter the coordinate of second point ");
                                            System.out.print("x: ");
                                            int x2 = in.nextInt();
                                            System.out.print("y: ");
                                            int y2 = in.nextInt();
                                            in.close();
                                            double distance = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
                                            // double distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                                            System.out.println("Distance = " + distance);
                                        }
                                    }
                                
                            

5→ Calculate commission

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            System.out.print("Enter commission base : ");
                                            int cb = in.nextInt();
                                            System.out.print("Enter commission period (in months) : ");
                                            int cp = in.nextInt();
                                            System.out.println("Commission Rate according to the agreement : 6 %");
                                            int com = (cb * 6) / 100;
                                            System.out.println("Commission for " + cp + " month period is : " + com);
                                            System.out.print("Number of Employee you want to split this with :");
                                            int split = in.nextInt();
                                            if(split==0){
                                                split=1;
                                            }
                                            System.out.println("Commission for each employee : "+ com/split);
                                    
                                        }
                                    }
                                
                            

6→ Write a program to print the sum of negative numbers, sum of positive even numbers and the sum of positive odd numbers from a list of numbers (N) entered by the user. The list terminates when the user enters a zero.

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            int negSum = 0;
                                            int posSum = 0;
                                            int oddSum = 0;
                                            while (num != 0) {
                                                num = in.nextInt();
                                                if (num > 0) {
                                                    posSum += num;
                                                }
                                                if (num < 0) {
                                                    negSum += num;
                                                }
                                                if (num % 2 == 1) {
                                                    oddSum += num;
                                                }
                                            }
                                            System.out.println("Sum of all positive numbers : " + posSum);
                                            System.out.println("Sum of all negative numbers : " + negSum);
                                            System.out.println("Sum of all odd numbers : " + oddSum);
                                    
                                        }
                                    }
                                
                            

7→ Sum Of A Digits Of Number

                                

                                    import java.util.Scanner;

                                    public class Test {
                                        public static void main(String[] args) {
                                            Scanner in = new Scanner(System.in);
                                            int num = in.nextInt();
                                            int sum = 0;
                                            while(num>0){
                                                int temp = num%10;
                                                sum+=temp;
                                                num/=10;
                                            }
                                            System.out.println("The sum of digits is : "+ sum);
                                        }
                                    }
                                
                            

Functions Notes

1→ Define two methods to print the maximum and the minimum number respectively among three numbers entered by the user.

                        

                            import java.util.Scanner;

                            public class Test {
                                public static void main(String[] args) {
                                    Scanner in = new Scanner(System.in);
                                    int num1 = in.nextInt();
                                    int num2 = in.nextInt();
                                    int num3 = in.nextInt();
                                    findMax(num1, num2, num3);
                                    findMin(num1, num2, num3);
                                }
                            
                                static void findMin(int num1, int num2, int num3) {
                                    int max = num1;
                                    if (num2 > max) {
                                        max = num2;
                                    }
                                    if (num3 > max) {
                                        max = num3;
                                    }
                                    System.out.println("max = " + max);
                                }
                            
                                static void findMax(int num1, int num2, int num3) {
                                    int min = num1;
                                    if (num2 < min) {
                                        min = num2;
                                    }
                                    if (num3 < min) {
                                        min = num3;
                                    }
                                    System.out.println("min = " + min);
                                }
                            }
                        
                    

2→ Define a method to find out if a number is prime or not.

                    

                        import java.util.Scanner;

                        public class Test {
                            public static void main(String[] args) {
                                Scanner in = new Scanner(System.in);
                                int num = in.nextInt();
                                isPrime(num);
                            }
                        
                            static void isPrime(int num) {
                                if (num < 2) {
                                    System.out.println("Enter number greater then 2");
                                } else {
                                    boolean isPrime = true;
                                    for (int i = 2; i < num / 2; i++) {
                                        if (num % i == 0) {
                                            isPrime = false;
                                        }
                                    }
                                    if (isPrime) {
                                        System.out.println("Prime");
                                    } else {
                                        System.out.println("Not Prime");
                                    }
                                }
                            }
                        }
                    
                

Arrays

1→ Build array from permutation

                        

                            import java.util.Arrays;
                            // import java.util.Scanner;
                            
                            public class Test {
                                public static void main(String[] args) {
                                    // Scanner in = new Scanner(System.in);
                                    int[] nums = {1,4,3,2,0};
                                    buildArray(nums);
                                }
                                public static int[] buildArray(int[] nums) {
                                    int[] ans = new int[nums.length];
                                    for (int i = 0; i < nums.length; i++) {
                                        ans[i]=nums[nums[i]];
                                    }
                                    return ans;
                                }
                            }                            
                        
                    

2→ Concatenation of array

                        

                            import java.util.Arrays;                            
                            
                            public class Test {
                                public static void main(String[] args) {
                                    int[] nums = { 1, 2, 3, 4 };
                                    System.out.println(Arrays.toString(getConcatenation(nums)));
                                }
                            
                                static int[] getConcatenation(int[] nums) {
                                    int n = nums.length;
                                    int[] ans = new int[2 * n];
                                    for (int i = 0; i < nums.length; i++) {
                                        ans[i] = nums[i];
                                        ans[i + n] = nums[i];
                                    }
                                    return ans;
                                }
                            }
                            
                        
                    

3→ Running sum of 1d array

                        

                            class Solution {
                                public int[] runningSum(int[] nums) {
                                    int[] sum = new int[nums.length];
                                    int prevSum = 0;
                                    for(int i = 0; i < nums.length; i++){
                                        prevSum += nums[i];
                                        sum[i]= prevSum;
                                    }
                                    return sum;
                                }
                            }
                        
                    

4→ Richest customer wealth

                        

                            class Solution {
                                public int maximumWealth(int[][] accounts) {
                                    int maxWealth = 0;
                                    for(int[] cus : accounts){
                                        int sum = 0;
                                        for(int i = 0; i < cus.length ; i++){
                                            sum = sum + cus[i];                 
                                        }
                                        if(sum>maxWealth){
                                            maxWealth = sum;
                                        }
                                    }
                                    return maxWealth;
                                }
                            }
                        
                    

5→ Suffle the array

                        

                            class Solution {
                                public int[] shuffle(int[] nums, int n) {
                                    int[] ans = new int[n*2];
                                    for(int i = 0; i < n; i++){           
                                        ans[i*2] = nums[i];
                                        ans[i*2+1] = nums[i+n];
                                    }
                                    return ans;    
                                }
                            }
                        
                    

6→ Kids with the greatest number of candies

                        

                            class Solution {
                                public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
                                    ArrayList<Boolean> result = new ArrayList<>();
                                    int max = 0;
                                    for (int i = 0; i < candies.length; i++) {
                                        if (candies[i] > max) {
                                            max = candies[i];
                                        }
                                    }
                                    for (int i = 0; i < candies.length; i++) {
                                        if (candies[i] + extraCandies >= max) {
                                            result.add(true);
                                        } else
                                            result.add(false);
                                    }
                                    return result;
                                }
                            }
                        
                    

7→ Number of good pairs

                        

                            class Solution {
                                public int numIdenticalPairs(int[] nums) {
                                    int ans = 0;
                                    for(int i = 0; i < nums.length; i++){
                                        for(int j = i+1; j < nums.length; j++){
                                            if(nums[i]==nums[j])
                                                ans++;
                                        }
                                    }
                                    return ans;
                                }
                            }
                        
                    

7→ How many numbers are smaller than the current number

                        

                            class Solution {
                                public int[] smallerNumbersThanCurrent(int[] nums) {
                                    int[] ans = new int[nums.length];
                                    for(int i = 0; i < nums.length; i++){
                                        int count=0;
                                        for(int j = 0; j < nums.length; j++){
                                            if(nums[j]<nums[i])
                                                count++;
                                        }
                                        ans[i] = count;
                                    }
                                    return ans;
                                }
                            }
                        
                    

9→ Create target array in the given order

                        

                            class Solution {
                                public int[] createTargetArray(int[] nums, int[] index) {
                                    ArrayList <Integer> list = new ArrayList <>();
                                    for(int i = 0; i < nums.length; i++){
                                        list.add(index[i],nums[i]);
                                        // in arraylist shifting is easier
                                    }
                                    int[] ans = new int[nums.length];
                                    for(int i = 0; i < ans.length; i++){
                                        ans[i] = list.get(i);
                                    }
                                    return ans;
                                }
                            }
                        
                    

10→ Check if the sentence is a pangram

                        

                            class Solution {
                                public boolean checkIfPangram(String sentence) {
                                    if(sentence.length()<26 || sentence.length()==0){
                                        return false;
                                    }
                                    
                                    // if some element is not availabe in an string its index is -1
                                    for(char a = 'a'; a <= 'z'; a++){
                                        if(sentence.indexOf(a)== -1)
                                            return false;
                                    }
                                    return true;
                                }
                            }
                        
                    

11→ Count items matching a rule

                        

                            class Solution {
                                public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
                                    int count = 0;
                                    if (ruleKey.equals("type")) {
                                        for (int i = 0; i < items.size(); i++) {
                                            if (ruleValue.equals(items.get(i).get(0)))
                                                count++;
                                        }
                                    } else if (ruleKey.equals("color")) {
                                        for (int i = 0; i < items.size(); i++) {
                                            if (ruleValue.equals(items.get(i).get(1)))
                                                count++;
                                        }
                                    } else if (ruleKey.equals("name")) {
                                        for (int i = 0; i < items.size(); i++) {
                                            if (ruleValue.equals(items.get(i).get(2)))
                                                count++;
                                        }
                                    }
                                    return count;
                                }
                            }
                        
                    

12→ Find the highest altitude

                        

                            class Solution {
                                public int largestAltitude(int[] gain) {
                                    int highestAl = 0;
                                    int sum = 0;
                                    for(int i = 0; i < gain.length; i++){            
                                        sum+=gain[i];
                                        if(sum>highestAl)
                                            highestAl = sum;
                                    }
                                    return highestAl;
                                }
                            }
                        
                    

13→ Flipping an image

                        

                            class Solution {
                                public int[][] flipAndInvertImage(int[][] image) {       
                                    
                                    for(int i = 0; i < image.length; i++){        
                                        int start = 0;
                                        int end = image[i].length-1;
                                        while(start<end){
                                            int temp = image[i][start];
                                            image[i][start] = image[i][end];
                                            image[i][end] = temp;
                                            start++;
                                            end--;
                                        }
                                    }
                                    for(int i = 0; i < image.length; i++){
                                        for(int j = 0; j < image.length; j++){
                                            if(image[i][j]==0)
                                                image[i][j] = 1;
                                            else
                                                image[i][j]= 0;
                                        }
                                    }
                                    return image;
                                }
                            }
                        
                    

14→ Cells with odd values in a matrix
Tricky question with many dislikes

                        


                        
                    

15→ Matrix diagonal sum

                        

                            class Solution {
                                public int diagonalSum(int[][] mat) {
                                    int sum = 0;
                                    for(int i = 0; i < mat.length; i++){
                                        for(int j = 0; j < mat[i].length; j++){
                                            if(i==j)
                                                sum+=mat[i][j];
                                            if(i+j==mat.length-1 && i!=j)
                                                sum+=mat[i][j];
                                        }
                                    }
                                    return sum;
                                }
                            }
                        
                    

16→ Find numbers with even number of digits

                        

                            class Solution {
                                public int findNumbers(int[] nums) {
                                    int times = 0;        
                                    for (int num : nums) {
                                        if(even(num)){
                                            times++;
                                        }
                                    }        
                                    return times;
                                }
                                    public boolean even(int num){
                                        return noOfDigits(num)%2==0;
                                    }
                                     public int noOfDigits(int num){
                                        int count = 0;
                                        while(num>0){
                                            count++;
                                            num/=10;            
                                        }
                                 return count;
                                }
                            }
                        
                    

17→ Transpose of matrix

                        

                            public class Test {
                                public int[][] transpose(int[][] matrix) {
                                    int r = matrix.length;
                                    int c = matrix[0].length;
                                    int[][] transposeM = new int[r][c];
                                    for (int i = 0; i < c; i++) {
                                        for (int j = 0; j < r; j++) {
                                            transposeM[j][i] = matrix[i][j];
                                        }
                                    }
                                    return transposeM;
                                }
                            }
                        
                    

18→ Add to array-form of integer

                        

                            class Solution {
                                public List<Integer> addToArrayForm(int[] num, int k) {
                                    List <Integer> list = new ArrayList<>();
                                    int fullNo = 0;
                                    for(int i = 0; i < num.length; i++){
                                        fullNo*=10;
                                        fullNo+=num[i];            
                                    }
                                    fullNo+=k;
                                    
                                    while(fullNo>0){
                                        int temp = fullNo%10;
                                        list.add(temp);
                                        fullNo/=10;
                                    }
                                    Collections.reverse(list);
                                    return list;
                                }
                            }
                        
                    

19→ Maximum population year

                        


                        
                    

20→ Determine whether matrix can be obtained by rotation

                        

                            class Solution {
                                public boolean findRotation(int[][] mat, int[][] target) {        
                                    for(int i = 0; i < 4 ; i++){
                                        if(check(mat,target))
                                            return true;
                                        rotate(mat);            
                                    }
                                    
                                    return false;
                                }
                                
                                // rotate the matrix (transpose + flip)
                                private void rotate(int[][] mat){        
                                    // transpose        
                                    for(int i = 0; i < mat.length; i++){
                                        for(int j = 0; j < i; j++){                
                                            int temp = mat[i][j];
                                            mat[i][j] = mat[j][i];
                                            mat[j][i] = temp;
                                        }
                                    }       
                                    // flip        
                                    for(int i = 0; i < mat.length; i++){
                                        int start = 0;
                                        int end = mat[0].length-1;
                                        while(start<=end)        {
                                            int temp = mat[i][start];
                                            mat[i][start] = mat[i][end];
                                            mat[i][end] = temp;
                                            start++;
                                            end--;
                                        }
                                    }
                                }  
                                
                                // check if mat after rotating is equal to target
                                private boolean check(int[][] mat,int[][] target){        
                                    for(int i = 0; i < mat.length; i++){
                                        for(int j = 0; j < mat[i].length; j++){
                                            if(target[i][j] != mat[i][j])
                                                return false;                
                                        }
                                    }
                                    return true;
                                }
                            }
                        
                    

21→ Two sum

                        

                            class Solution {
                                public int[] twoSum(int[] nums, int target) {
                                    for(int i = 0; i < nums.length; i++){
                                        for(int j = i+1; j < nums.length; j++){
                                            if(nums[i]+nums[j]==target)
                                                return new int[]{i,j};
                                        }
                                    }
                                    return new int[]{-1,-1};
                                }
                            }
                        
                    

22→ Find n unique integers sum up to zero

                        

                            class Solution {
                                public int[] sumZero(int n) {
                                    int num = n;        
                                    int[] ans = new int[n];
                                    
                                    int start = 0;
                                    int end = ans.length-1;
                                    while(start <= end){
                                        ans[start] = num;
                                        ans[end] = 0-num;
                                        num--;
                                        start++;
                                        end--;
                                    }
                                    ans[ans.length/2] = 0;
                                    return ans;
                                }
                            }
                        
                    

23→ lucky numbers in a matrix

                        

                            class Solution {
                                public List<Integer> luckyNumbers (int[][] matrix) {
                                    List <Integer> list = new ArrayList<>();
                                    for(int i = 0; i < matrix.length; i++){
                                        int least = matrix[i][0];
                                        int minCol = 0;
                                        for(int j = 1; j < matrix[i].length; j++){
                                            if(matrix[i][j] < least){
                                                minCol = j;
                                                least = matrix[i][j];
                                            }
                                        }
                                        boolean isTrue = true;
                                        for(int j = 0; j < matrix.length; j++){
                                            if(matrix[i][minCol] < matrix[j][minCol]){
                                                isTrue = false;
                                                break;
                                            }
                                        }
                                        if(isTrue)
                                            list.add(least);
                                    }
                                    return list;
                                }                                
                            }
                        
                    

24→ Maximum Subarray

This uses Kadane's algorithm for a omtimised solution Explaination

                        

                            class Solution {
                                public int maxSubArray(int[] nums) {
                                    int maxSum = -99999;
                                    for(int i = 0; i < nums.length; i++){
                                        int curSum = 0;
                                        for(int j = i; j < nums.length; j++){
                                            curSum += nums[j];
                                            if(curSum>maxSum)
                                                maxSum = curSum;
                                        }
                                        
                                    }
                                    return maxSum;
                                }
                            }
                        
                    
                        

                            class Solution {
                                public int maxSubArray(int[] nums) {
                                    int maxSum = nums[0];
                                    int curSum = 0;
                                    for(int i = 0; i < nums.length; i++){
                                        curSum += nums[i];
                                        if(curSum > maxSum)
                                            maxSum = curSum;
                                        if(curSum < 0)
                                            curSum = 0;
                                    }
                                    return maxSum;
                                }
                            }         
                        
                    

25→ Reshape the matrix

Explaination

                        

                            class Solution {
                                public int[][] matrixReshape(int[][] mat, int r, int c) {
                                    if((r*c) != (mat.length)*(mat[0].length)) return mat;
                                    int[][] reshaped = new int [r][c];
                                    int row_num = 0;
                                    int column_num = 0;
                                    for(int i = 0; i < mat.length; i++){
                                        for(int j = 0; j < mat[i].length; j++){
                                            reshaped[row_num][column_num] = mat[i][j];
                                            column_num++;
                                            if(column_num == c){
                                                column_num = 0;
                                                row_num++;
                                            }
                                        }            
                                    }
                                    return reshaped;
                                }
                            }
                        
                    

26→ Plus one

Explaination

                        

                            class Solution {
                                public int[] plusOne(int[] digits) {
                                    for(int i = digits.length-1; i >= 0; i--){
                                        if(digits[i] < 9){
                                            digits[i]++;
                                            return digits;
                                        }
                                        digits[i] = 0;
                                    }
                                    int[] ans = new int[digits.length+1];
                                    ans[0] = 1;
                                    return ans;
                                }
                            }
                        
                    

2→

                        


                        
                    

2→

                        


                        
                    

2→

                        


                        
                    

2→