Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.9 kB
2
Indexable
Never
import java.util.*;

class ArraysExercise {

  public static int[] getArray(){
    int n=0;
    System.out.println("Enter number of elements (n):");
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    System.out.println("Enter the elements:");
    int[] a = new int[n];
    for(int i=0;i<n;i++){
      a[i] = sc.nextInt();
    }
    return a;
  }

  public static void printArray(int[] a){
    int count=0;
    for(int i=0; i<a.length;i++){
      System.out.print(a[i]+" ");
      count += 1;
      if(count%5==0){
        System.out.println();
      }
    }
  }

  public static int findMax(int[] a){
    int max = 0;
    int temp = 0;
    for(int i=0;i<a.length;i++){
      temp = a[i];
      if(temp>max){
        max = temp;
      }
    }
    return max;
  }

  public static int findMin(int[] a){
    int min = 999999;
    int temp = 0;
    for(int i=0;i<a.length;i++){
      temp = a[i];
      if(temp<min){
        min = temp;
      }
    }
    return min;
  }

  public static int findMaxIndex(int[] a){
    int max = 0;
    int temp = 0;
    int maxIndex = 0;
    for(int i=0;i<a.length;i++){
      temp = a[i];
      if(temp>max){
        max = temp;
        maxIndex = i;
      }
    }
    return maxIndex;
  }

  public static int findMinIndex(int[] a){
    int min = 0;
    int temp = 0;
    int minIndex = 0;
    for(int i=0;i<a.length;i++){
      temp = a[i];
      if(temp<min){
        min = temp;
        minIndex = i;
      }
    }
    return minIndex;
  }

  public static boolean isSortedAscend(int[] a){
    for(int i=1; i<a.length; i++){
      if(a[i-1]>a[i]){
        return false;
      }
    }
    return true;
  }

  public static boolean isSortedDescend(int[] a){
    for(int i=1; i<a.length; i++){
      if(a[i-1]<a[i]){
        return false;
      }
    }
    return true;
  }

  public static void swapNeighbor(int[] a){
    int temp=0;
    for(int i=1; i<a.length; i++){
      if(a[i-1]>a[i]){
        temp = a[i-1];
        a[i-1] = a[i];
        a[i] = temp;
      }
    }
  }

  public static int[] merge(int[] a1, int[] a2){
    int temp=0;
    int[] a = new int[a1.length+a2.length];
    for(int i=0; i<a1.length;i++){
      a[i]=a1[i];
    }
    for(int j=0; j<a2.length;j++){
      a[j]=a2[j];
    }
    for(int k=1; k<a1.length+a2.length;k++){
      if(a[k-1]>a[k]){
        temp=a[k-1];
        a[k-1]=a[k];
        a[k]=temp;
      }
    }
    return a;
  }

  public static void main(String[] args) {
    
    int[] arr1 = getArray();
    System.out.println("The elements in the array are:");
    printArray(arr1);
    int max = findMax(arr1);
    System.out.println("The max element in the array is: "+max);
    int min = findMin(arr1);
    System.out.println("The min element in the array is: "+min);
    int maxI = findMaxIndex(arr1);
    System.out.println("The max element in the array exists at index: "+maxI);
    int minI = findMinIndex(arr1);
    System.out.println("The min element in the array exists at index: "+minI);
    int[] c = {2,5,7,9};
    System.out.println("Is the array sorted in ascending order? "+isSortedAscend(c));
    int[] arr3 = {2,15,7,29};
    System.out.println("Is the array sorted in ascending order? "+isSortedAscend(arr3));
    int[] arr4 = {2,5,7,9};
    System.out.println("Is the array sorted in descending order? "+isSortedDescend(arr4));
    int[] arr5 = {2,15,7,29};
    System.out.println("Is the array sorted in descending order? "+isSortedDescend(arr5));
    swapNeighbor(arr1);
    System.out.println("The array after swapping is:");
    printArray(arr1);
    int[] arr6 = {2,6,9};
    int[] arr7 = {-1,5,11,12};
    int[] b = merge(arr6,arr7);
    printArray(b);
    int[] arr2 = getArray();
    merge(arr1,arr2);
  }
}