Arrays - I

Arrays - I

Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of same data type stored at contiguous memory locations.

Check If Array Is Sorted

Given an array of size n, write a program to check if the given array is sorted or not. If the array is sorted then return True, Else return False.

package Arrays.Easy;

public class CheckIfArrayIsSorted {
    public static void main(String[] args) {
        int[] arr = {4, 2, 7, 10, 8};
        System.out.println(OptimalSolution(arr));
    }

    public static boolean OptimalSolution(int[] arr){
        int n = arr.length;

        for (int i = 1; i < n; i++) {
            if(arr[i] < arr[i - 1]){
                return false;
            }
        }
        return true;
    }
}

Remove Duplicates From Sorted Array

Given an integer array sorted in non-decreasing order, remove the duplicates in place such that each unique element appears only once. The relative order of the elements should be kept the same.

If there are k elements after removing the duplicates, then the first k elements of the array should hold the final result. Return k after placing the final result in the first k slots of the array.

public class RemoveDuplicatesFromSortedArray {
     public static void main(String[] args){
          int[] arr = {1,1,2,2,2,3,3};
          int x = OptimalSolution(arr);
          for (int i = 0; i < x; i++) {
              System.out.print(arr[i] + " ");
          }
     }

     public static int OptimalSolution(int[] arr){
         int i = 0;

         for(int j = 1; j < arr.length; j ++){
             if(arr[i] != arr[j]){
                i ++;
                arr[i] = arr[j];
             }
         } 
         return i + 1;
     }
}

Left Rotate Array By One Place

Given an array of N integers, left rotate the array by one place.

public class LeftShift{
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5};
        OptimalSolution(arr);  
    }

    public static void OptimalSolution(int[] arr){
         int n = arr.length;
         int temp = arr[0];

         for(int i = 0; i < n - 1; i++){
             arr[i] = arr[i + 1];
         }
         arr[n - 1] = temp;

         for(int i = 0; i < n; i++){
             System.out.print(arr[i] + " ");
         }
    }
}

Move Zeros To End

You are given an array of integers, your task is to move all the zeros in the array to the end of the array.

public class movezeros{
    public static void main(String[] args){
        int[] arr = {1, 0, 2, 3, 2, 0, 0, 4, 5, 1};
        int[] ans = BruteSolution(arr);

        for (int i = 0; i < ans.length; i++) {
            System.out.print(ans[i] + " ");
        }
    }

    public static int[] OptimalSolution(int[] arr){
         int n = arr.length;

         int j = -1;
         for(int i = 0; i < n; i++){
             if(arr[i] != 0){
                 j = i;
                 break;
             }
         }

         if(j == -1){
             return arr;
         }

         for(int i = j + 1; i < n; i++){
             if(arr[i] != 0){
                 int temp = arr[i];
                 arr[i] = arr[j];
                 arr[j] = temp;
                 j ++;
             }
         }

         return arr;
    }
}

Find The Union

Given two sorted arrays, arr1, and arr2 of size n and m. Find the union of two sorted arrays.

public class union{
    public static void main(String[] args){
        int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] arr2 = {2, 3, 4, 4, 5, 11, 12};

        ArrayList<Integer> union = OptimalSolution(arr1, arr2);
        for(int val : union){
            System.out.print(val + " ");
        }

    }

    public static ArrayList<Integer> OptimalSolution(int[] arr1, int[] arr2){
         int n = arr1.length;
         int m = arr2.length;

         int i = 0, j = 0;
         ArrayList<Integer> union = new ArrayList<>();

         while(i < n && j < m){
              if(arr1[i] < arr2[j]){
                   if(union.size() == 0 || union.get(union.size() - 1) != arr1[i]){
                        union.add(arr1[i]);
                   }
                   i ++;
              } else{
                   if(union.size() == 0 || union.get(union.size() - 1) != arr2[j]){
                        union.add(arr2[j]);
                   }
                   j ++;
              }
         } 

         while(i < n){
            if(union.get(union.size() - 1) != arr1[i]){
                union.add(arr1[i]);
            }
            i++;
        }

        while (j < m){
            if(union.get(union.size() - 1) != arr2[j]){
                union.add(arr2[j]);
            }
            j++;
        }

        return union;
    }
}

Maximum Consecutive One's

Given an array that contains only 1 and 0 return the count of maximum consecutive ones in the array.

package Arrays.Easy;

public class CountMaximumConsecutiveOnes {
    public static void main(String[] args) {
        int[] arr = { 1, 1, 0, 1, 1, 1 };
        System.out.println(Solution(arr));

    }

    public static int Solution(int[] arr){
        int n = arr.length;
        int count = 0;
        int maxi = 0;

        for (int i = 0; i < n; i++) {
            if(arr[i] == 1){
                count ++;
            }else{
                count = 0;
            }

            maxi = Math.max(maxi, count);
        }

        return maxi;
    }
}

Find Number That Appears Once

Given a non-empty array of integers arr, every element appears twice except for one. Find that single one.

package Arrays.Easy;

import java.util.HashMap;

public class FindNumbersThatAppearOnceOrTwice {
    public static void main(String[] args) {
        int[] arr = {4, 1, 2, 1, 2};
        System.out.println(OptimalSolution(arr));
    }

    public static int OptimalSolution(int[] arr){
        int n = arr.length;
        int xor = 0;

        for (int i = 0; i < n; i++) {
            xor ^= arr[i];
        }

        return xor;
    }
}

Find Missing Number In An Array

Given an integer N and an array of size N-1 containing N-1 numbers between 1 to N. Find the number(between 1 to N), that is not present in the given array.

package Arrays.Easy;

public class FindMissingNumber {
    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 5};
        System.out.println(OptimalSolution(arr, 5));
    }

    public static int OptimalSolution(int[] arr, int N){
        int xor1 = 0, xor2 = 0;

        for (int i = 0; i < N - 1; i++) {
            xor1 = xor1 ^ arr[i];
            xor2 = xor2 ^ (i + 1);
        }
        xor2 = xor2 ^ N;

        return (xor1 ^ xor2);
    }
}

package Arrays.Easy;

public class LinearSearch {
    public static void main(String[] args) {
        int[] arr = {2, 3, 5, 6, 1, 8, 4};
        int num = 8;
        System.out.println(Solution(arr, num));

    }

    public static int Solution(int[] arr, int num){
        int n = arr.length;

        for (int i = 0; i < n; i++) {
            if(arr[i] == num){
                return i;
            }
        }
        return -1;
    }
}

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!