Java New
Java New
Max In Array
public class MaxInArray {
public static int findMax(int[] arr) {
// Initialize maximum element with first element of array
int max = arr[0];
// Traverse the array
for (int i = 1; i < arr.length; i++) {
// If the current element is greater than the maximum then update the
maximum
if (arr[i] > max) {
max = arr[i];
}
}
// Return the maximum element
return max;
}
public static void main(String[] args) {
// Declare and initialize the array
int[] arr = { 25, 11, 7, 75, 56 };
// Call the function to find the maximum element
int MaxInArray = findMax(arr);
System.out.println("23MCA1030 Vinayak Kumar Singh");
// Print the maximum element
System.out.println("The maximum element in the array is: " +
MaxInArray);
}
}
2. Java program to reverse an array without using any additional array.
public class ArrayReverseNoExtraSpace {
// Function to reverse the array in-place
public static void reverseArray(int[] arr) {
int start = 0;
int end = arr.length - 1;
// Swap elements at the start and end indices
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("23MCA1030 Vinayak Kumar Singh");
System.out.print("Original array: [");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
reverseArray(arr);
System.out.print("Reversed array: [");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}}
3. Java program to sort an array of integers in ascending order using the
bubble sort algorithm.
public class BubbleSort {
// Function to sort the array using bubble sort algorithm
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no two elements were swapped by the inner loop, break
if (!swapped) {
break;
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("23MCA1030 Vinayak Kumar Singh");
System.out.print("Original array: [");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
bubbleSort(arr);
System.out.print("Sorted array: [");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
}
4. Java program to find the second largest element in an array
public class SecondLargestInArray {
public static void main(String[] args) {
int[] arr = {5, 8, 2, 10, 7, 3};
int secondLargest = findSecondLargest(arr);
System.out.println("23MCA1030 Vinayak Kumar Singh");
System.out.print("Original array is: ");
printArray(arr);
System.out.println("\nThe second largest element in array is: " +
secondLargest);
}
public static int findSecondLargest(int[] arr) {
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
// Find the largest element
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
}
}
// Update the second largest element
for (int num : arr) {
if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
return secondLargest;
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(arr[i]);
}
}
}
5. Java program to remove duplicate elements from an array
without using any additional data structure.
public class RemoveDuplicatesInArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 5, 3, 6};
System.out.println("23MCA1030 Vinayak Kumar Singh");
System.out.println("Original array: ");
printArray(arr);
int length = removeDuplicates(arr);
System.out.println("\nArray after removing duplicates: ");
printArray(arr, length);
}
public static int removeDuplicates(int[] arr) {
if (arr.length == 0) {
return 0;
}
int j = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
}
public static void printArray(int[] arr, int length) {
for (int i = 0; i < length; i++) {
System.out.print(arr[i] + " ");
}
}
}
6. Java program to compute the sum of elements in a 2D array.
public class SumOf2DArray {
public static int sumOf2DArray(int[][] arr) {
int sum = 0;
for (int[] row : arr) {
for (int num : row) {
sum += num;
}
}
return sum;
}
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = sumOf2DArray(arr);
System.out.println("23MCA1030 Vinayak Kumar Singh");
System.out.println("Sum of elements in the 2D array: " + sum);
}
}