🔳Array Problems

  1. Given the array [5,3,6,2,4], print out each element.

  2. Calculate the sum of all elements in the array [2, 4, 6, 8, 10].

  3. Find the largest number in the array [11, 9, 13, 3, 24].

  4. Count the number of even numbers in the array [2, 6, 9, 11, 3, 8].

  5. Reverse the elements of the array [1, 2, 3, 4, 5] so it becomes [5, 4, 3, 2, 1].

  6. Check if the target 7 exists in the array [2, 9, 13, 6, 7, 11].

  7. Sort the array [9, 5, 3, 2, 4] in ascending order.

  8. Print true if the array [4, 6, 8, 10] contains 10, false otherwise.

  9. Join all elements of ["H", "e", "l", "l", "o"] into a single string.

  10. Write a function that takes an array and returns the second largest number.

Answers

Given the array[5, 3, 6, 2, 4], print out each element.

public class ArrayPrinting {
    public static void main(String[] args) {
        int[] arr = {5, 3, 6, 2, 4};
        
        System.out.println("Elements in the array:");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

When you run this program, it will output:

Elements in the array:
5
3
6
2
4

Calculate the sum of all elements in the array [2, 4, 6, 8, 10].

public class ArraySum {
    public static void main(String[] args) {
        int[] arr = {2, 4, 6, 8, 10};
        int sum = 0;
        
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        
        System.out.println("Sum of all elements in the array: " + sum);
    }
}

When you run this program, it will output:

Sum of all elements in the array: 30

Find the largest number in the array [11, 9, 13, 3, 24].

public class LargestNumber {
    public static void main(String[] args) {
        int[] arr = {11, 9, 13, 3, 24};
        int max = arr[0];
        
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        
        System.out.println("The largest number in the array is: " + max);
    }
}

When you run this program, it will output:

The largest number in the array is: 24

Count the number of even numbers in the array [2, 6, 9, 11, 3, 8].

public class CountEvenNumbers {
    public static void main(String[] args) {
        int[] arr = {2, 6, 9, 11, 3, 8};
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                count++;
            }
        }
        
        System.out.println("The number of even numbers in the array is: " + count);
    }
}

When you run this program, it will output:

The number of even numbers in the array is: 3

Reverse the elements of the array [1, 2, 3, 4, 5] so it becomes [5, 4, 3, 2, 1].

public class ReverseArray {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int start = 0;
        int end = arr.length - 1;
        
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            
            start++;
            end--;
        }
        
        System.out.println("Reversed array: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

When you run this program, it will output:

Reversed array:
5 4 3 2 1

Check if the target 7 exists in the array [2, 9, 13, 6, 7, 11].

public class CheckTarget {
    public static void main(String[] args) {
        int[] arr = {2, 9, 13, 6, 7, 11};
        int target = 7;
        boolean exists = false;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                exists = true;
                break;
            }
        }
        
        if (exists) {
            System.out.println("The target " + target + " exists in the array.");
        } else {
            System.out.println("The target " + target + " does not exist in the array.");
        }
    }
}

When you run this program, it will output:

The target 7 exists in the array.

Sort the array [9, 5, 3, 2, 4] in ascending order.

public class SortArray {
    public static void main(String[] args) {
        int[] arr = {9, 5, 3, 2, 4};
        
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        
        System.out.println("Sorted array in ascending order: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

When you run this program, it will output:

Sorted array in ascending order:
2 3 4 5 9
public class CheckArray {
    public static void main(String[] args) {
        int[] arr = {4, 6, 8, 10};
        int target = 10;
        boolean containsTarget = false;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                containsTarget = true;
                break;
            }
        }
        
        System.out.println(containsTarget);
    }
}

When you run this program, it will output:

true

Join all elements of ["H", "e", "l", "l", "o"] into a single string.

public class JoinArray {
    public static void main(String[] args) {
        String[] arr = {"H", "e", "l", "l", "o"};
        String joinedString = String.join("", arr);
        
        System.out.println("Joined string: " + joinedString);
    }
}

When you run this program, it will output:

Joined string: Hello

Write a function that takes an array and returns the second largest number.

public class SecondLargestNumber {
    public static int findSecondLargest(int[] arr) {
        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > largest) {
                secondLargest = largest;
                largest = arr[i];
            } else if (arr[i] > secondLargest && arr[i] != largest) {
                secondLargest = arr[i];
            }
        }
        
        return secondLargest;
    }
    
    public static void main(String[] args) {
        int[] arr = {9, 5, 3, 2, 4};
        int secondLargestNumber = findSecondLargest(arr);
        System.out.println("Second largest number: " + secondLargestNumber);
    }
}

When you run this program, it will output:

Second largest number: 5

Last updated