Arrays Lesson Hacks
My attempt at the Arrays Lesson hw
import java.util.Arrays;
public class ArrayMethods {
public static void main(String[] args)
{
int[] array_nums = {20, 30, 40};
System.out.println("Original Array: "+Arrays.toString(array_nums));
int x = array_nums[0];
array_nums[0] = array_nums[array_nums.length-1];
array_nums[array_nums.length-1] = x;
System.out.println("New array after swaping the first and last elements: "+Arrays.toString(array_nums));
}
}
ArrayMethods.main(null);
import java.util.Arrays;
public class Duplicates {
public static void main(String[] args)
{
int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2};
for (int i = 0; i < my_array.length-1; i++)
{
for (int j = i+1; j < my_array.length; j++)
{
if ((my_array[i] == my_array[j]) && (i != j))
{
System.out.println("Duplicate Element : "+my_array[j]);
}
}
}
}
}
Duplicates.main(null);
import java.math.*;
class GFG
{
public static void checkType(int arr[], int n)
{
if (arr[0] <= arr[1] &&
arr[n - 2] <= arr[n - 1])
System.out.println("Increasing");
else if (arr[0] >= arr[1] &&
arr[n - 2] >= arr[n - 1])
System.out.println("Decreasing");
else if (arr[0] <= arr[1] &&
arr[n - 2] >= arr[n - 1])
System.out.println("Increasing then decreasing");
else
System.out.println("Decreasing then increasing");
}
public static void main(String[] args)
{
int[] arr = new int[]{ 1, 2, 3, 4 };
int n = arr.length;
checkType(arr, n);
}
}
GFG.main(null);