1. Write a Java program to rearrange a given array of unique elements such that every second element of the array is greater than its left and right elements.
Example:
Input :
nums= { 1, 2, 4, 9, 5, 3, 8, 7, 10, 12, 14 }
Output:
Array with every second element is greater than its left and right elements:
[1, 4, 2, 9, 3, 8, 5, 10, 7, 14, 12]
2. Write a Java program to form the largest number from a given list of non-negative integers.
Example:
Input :
nums = {1, 2, 3, 0, 4, 6}
Output:
Largest number using the given array numbers: 643210
3. Write a Java program to shuffle a given array of integers.
4. Write a Java program to find maximum product of two integers in a given array of integers.
Example:
Input :
nums = { 2, 3, 5, 7, -7, 5, 8, -5 }
Output:
Pair is (7, 8), Maximum Product: 56
import java.util.Arrays;
public class Program1 {
private static void swap(int[] elements, int i, int j) {
int num = elements[i];
elements[i] = elements[j];
elements[j] = num;
}
public static void rearrangeArray(int[] elements)
{
for (int i = 1; i < elements.length; i += 2)
{
if (elements[i - 1] > elements[i]) {
swap(elements, i - 1, i);
}
if (i + 1 < elements.length && elements[i + 1] > elements[i]) {
swap(elements, i + 1, i);
}
}
}
public static void main(String ars[]){
int[] nums= { 1, 2, 4, 9, 5, 3, 8, 7, 10, 12, 14 };
System.out.println("Original array: " + Arrays.toString(nums));
rearrangeArray(nums);
System.out.println("Array with every second element is greater than its left and right elements: \n" + Arrays.toString(nums));
}
}
import java.util.Arrays;
public class Program2 {
public static String largestNumbers(int[] elements) {
String[] array_nums = Arrays.stream(elements).mapToObj(String::valueOf).toArray(String[]::new);
Arrays.sort(array_nums, (String str1, String str2) -> (str2 + str1).compareTo(str1 + str2));
return Arrays.stream(array_nums).reduce((a, b) -> a.equals("0") ? b : a + b).get();
}
public static void main(String ars[]){
int[] nums = {1, 2, 3, 0, 4, 6};
System.out.print("\nOriginal array: " + Arrays.toString(nums));
System.out.print("\nLargest number using the said array numbers: " + largestNumbers(nums));
}
}
import java.util.Arrays;
import java.util.Random;
public class Program3 {
private static void swap(int[] elements, int i, int j) {
int num = elements[i];
elements[i] = elements[j];
elements[j] = num;
}
public static void shuffle(int elements[]){
for (int i = elements.length - 1; i >= 1; i--)
{
Random rand = new Random();
int j = rand.nextInt(i + 1);
swap(elements, i, j);
}
}
public static void main(String ars[]){
int[] nums = { 1, 2, 3, 4, 5, 6 };
System.out.println("Original Array: " + Arrays.toString(nums));
shuffle(nums);
System.out.println("Shuffle Array: " + Arrays.toString(nums));
}
}
import java.util.Arrays;
public class Program4 {
public static void findMaxProduct(int[] elements)
{
int max_pair_product = Integer.MIN_VALUE;
int max_i = -1, max_j = -1;
for (int i = 0; i < elements.length - 1; i++)
{
for (int j = i + 1; j < elements.length; j++)
{
if (max_pair_product < elements[i] * elements[j])
{
max_pair_product = elements[i] * elements[j];
max_i = i;
max_j = j;
}
}
}
System.out.print("Pair is (" + elements[max_i] + ", " + elements[max_j] + "), Maximum Product: " + (elements[max_i] * elements[max_j]));
}
public static void main(String ars[]){
int[] nums = { 2, 3, 5, 7, -7, 5, 8, -5 };
System.out.println("\nOriginal array: " + Arrays.toString(nums));
findMaxProduct(nums);
}
}
Comments
Leave a comment