# 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.

<details>

<summary>Answers</summary>

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

```java
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:

```java
Elements in the array:
5
3
6
2
4
```

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

```java
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:

```java
Sum of all elements in the array: 30
```

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

```java
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:

```java
The largest number in the array is: 24
```

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

```java
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:

```java
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].

```java
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:

```java
Reversed array:
5 4 3 2 1
```

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

```java
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:

```java
The target 7 exists in the array.
```

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

```java
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:

```java
Sorted array in ascending order:
2 3 4 5 9
```

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

```java
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:

```java
true
```

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

```java
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:

```java
Joined string: Hello
```

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

```java
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:

```java
Second largest number: 5
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/java/data-structures-+-algorithms/data-structures/array-problems.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
