Array Answers
Array Answers
java
Copy code
public class ReverseArray {
public static void main(String[] args) {
char[] arr = {'a', 'b', 'c', 'd'};
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
java
Copy code
public class EvenElementsSum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
int sum = 0;
for (int num : arr) {
if (num % 2 == 0) {
sum += num;
}
}
System.out.println("Sum of even elements: " + sum);
}
}
java
Copy code
public class VowelsInArray {
public static void main(String[] args) {
char[] arr = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
for (char c : arr) {
if ("aeiou".indexOf(c) != -1) {
System.out.print(c + " ");
}
}
}
}
I'll continue with the rest of the questions in the following part. Would you like to proceed
further with more solutions?
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for the next set of questions (14–28):
Since this is an interactive input scenario, here's a general structure. The user enters even
numbers into an array:
java
Copy code
import java.util.ArrayList;
import java.util.Scanner;
while (true) {
int value = sc.nextInt();
if (value % 2 == 0) {
evenValues.add(value);
} else {
break;
}
}
System.out.println(occurrences);
}
}
17. Find Maximum Occurred Element
Input: {1, 2, 1, 3, 1, 4}
Output: 1-3
java
Copy code
import java.util.HashMap;
import java.util.Map;
int maxFreq = 0;
int maxElement = -1;
for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {
if (entry.getValue() > maxFreq) {
maxFreq = entry.getValue();
maxElement = entry.getKey();
}
}
System.out.println(Arrays.toString(arr));
}
}
25. Sum of Each Element Up to a Single Digit
Input: {23, 55, 57, 93, 10, 1}
Output: {5, 1, 3, 3, 1, 1}
java
Copy code
public class SumToSingleDigit {
public static void main(String[] args) {
int[] arr = {23, 55, 57, 93, 10, 1};
System.out.println(result);
}
}
27. Remove Duplicates public class RemoveDuplicates {
Input: {1, 2, 1, 3, 2, 4} public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
Output: {1, 2, 3, 4}
while (j < n) {
arr[j++] = 0;
}
java
Copy code
public class ShiftZerosLeft {
public static void main(String[] args) {
int[] arr = {2, 0, 5, 3, 1, 0, 3, 1};
int n = arr.length;
int j = n - 1;
set1.addAll(set2);
System.out.println(set1);
}
}
39. Print Common Elements in Two Arrays
Input 1: {1, 2, 4, 5, 6}
Input 2: {2, 5, 8, 9, 3, 1}
Output: {1, 2, 5}
public class CommonElements {
java public static void main(String[] args) {
int[] arr1 = {1, 2, 4, 5, 6};
Copy code int[] arr2 = {2, 5, 8, 9, 3, 1};
set1.retainAll(set2);
System.out.println(set1);
}
}
Would you like to proceed with the final set of solutions (49-57)?
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for the final set of questions (49–57):
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
System.out.println(Arrays.toString(arr));
}
}
System.out.println(Arrays.toString(arr));
}
}
System.out.println(Arrays.toString(arr));
}
}