0% found this document useful (0 votes)
78 views41 pages

Array Answers

Uploaded by

s14anj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views41 pages

Array Answers

Uploaded by

s14anj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Arrays Questions

1. Store Characters in an Array and Print in Reverse Order

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] + " ");
}
}
}

2. Print Even Index Values


Input: {1, 2, 3, 4, 5, 6}
Output: {1, 3, 5}
java
Copy code
public class EvenIndexValues {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < arr.length; i += 2) {
System.out.print(arr[i] + " ");
}
}
}

3. Print Odd Index Values


Input: {1, 2, 3, 4, 5, 6}
Output: {2, 4, 6}
java
Copy code
public class OddIndexValues {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < arr.length; i += 2) {
System.out.print(arr[i] + " ");
}
}
}

4. Print Even Elements


Input: {1, 2, 3, 4, 5, 6}
Output: {2, 4, 6}
java
Copy code
public class EvenElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
for (int num : arr) {
if (num % 2 == 0) {
System.out.print(num + " ");
}
}
}
}

5. Print Odd Elements


Input: {1, 2, 3, 4, 5, 6}
Output: {1, 3, 5}
java
Copy code
public class OddElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
for (int num : arr) {
if (num % 2 != 0) {
System.out.print(num + " ");
}
}
}
}

6. Print the Sum of All Elements


Input: {1, 2, 3, 4, 5, 6}
Output: 21
java
Copy code
public class ArraySum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum: " + sum);
}
}

7. Print Sum of Even and Odd Indices Separately


Input: {1, 2, 3, 4, 5, 6}
Output: {9 (sum of even indices), 12 (sum of odd indices)}
java
Copy code
public class IndexSum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
int evenSum = 0, oddSum = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
evenSum += arr[i];
} else {
oddSum += arr[i];
}
}
System.out.println("Even index sum: " + evenSum);
System.out.println("Odd index sum: " + oddSum);
}
}

8. Print Sum of Even Elements


Input: {1, 2, 3, 4, 5, 6}
Output: 12

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);
}
}

9. Print Sum of Odd Elements


Input: {1, 2, 3, 4, 5, 6}
Output: 9
java
Copy code
public class OddElementsSum {
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 odd elements: " + sum);
}
}

10. Print the 2nd Last Element


Input: {1, 2, 3, 4, 5, 6}
Output: 5
java
Copy code
public class SecondLastElement {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
if (arr.length >= 2) {
System.out.println("Second last element: " + arr[arr.length - 2]);
} else {
System.out.println("Array is too small!");
}
}
}

11. Print Prime Numbers Present in an Array


Input: {1, 2, 3, 4, 5, 6}
Output: {2, 3, 5}
java
Copy code
public class PrimeNumbers {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
for (int num : arr) {
if (isPrime(num)) {
System.out.print(num + " ");
}
}
}

public static boolean isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}

12. Print Vowels Present in an Array


Input: {a, b, c, d, e, f, g}
Output: {a, e}

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 + " ");
}
}
}
}

13. Count the Number of Zeros in an Array


Input: {1, 0, 3, 0, 5, 6, 0}
Output: 3
java
Copy code
public class CountZeros {
public static void main(String[] args) {
int[] arr = {1, 0, 3, 0, 5, 6, 0};
int count = 0;
for (int num : arr) {
if (num == 0) count++;
}
System.out.println("Number of zeros: " + count);
}
}

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):

14. Insert Even Values in an Array

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;

public class InsertEvenValues {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> evenValues = new ArrayList<>();
System.out.println("Enter values (enter non-even number to stop):");

while (true) {
int value = sc.nextInt();
if (value % 2 == 0) {
evenValues.add(value);
} else {
break;
}
}

System.out.println("Even values: " + evenValues);


}
}

15. Find Last Index of Given Element


Input: {1, 2, 9, 3, 9, 4, 9, 6, 7, 8}
Element to Find: 9
Output: 5
java
Copy code
public class LastIndexOfElement {
public static void main(String[] args) {
int[] arr = {1, 2, 9, 3, 9, 4, 9, 6, 7, 8};
int element = 9;
int lastIndex = -1;

for (int i = 0; i < arr.length; i++) {


if (arr[i] == element) {
lastIndex = i;
}
}

System.out.println("Last index of " + element + ": " + lastIndex);


}
}

16. Count Occurrences of Each Element


Input: {1, 2, 1, 3, 2, 4}
Output: {1-2, 2-2, 3-1, 4-1}
java
Copy code
import java.util.HashMap;
import java.util.Map;

public class CountOccurrences {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
Map<Integer, Integer> occurrences = new HashMap<>();

for (int num : arr) {


occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);
}

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;

public class MaxOccuredElement {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 1, 4};
Map<Integer, Integer> occurrences = new HashMap<>();

for (int num : arr) {


occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);
}

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(maxElement + "-" + maxFreq);


}
}

18. Find Minimum Occurred Element


Input: {1, 2, 1, 1, 2, 1}
Output: 2-2
java
Copy code
import java.util.HashMap;
import java.util.Map;

public class MinOccuredElement {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 1, 2, 1};
Map<Integer, Integer> occurrences = new HashMap<>();

for (int num : arr) {


occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);
}

int minFreq = Integer.MAX_VALUE;


int minElement = -1;
for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {
if (entry.getValue() < minFreq) {
minFreq = entry.getValue();
minElement = entry.getKey();
}
}

System.out.println(minElement + "-" + minFreq);


}
}

19. Square of Each Element


Input: {1, 2, 3, 4, 5}
Output: {1, 4, 9, 16, 25}
java
Copy code
public class SquareElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.print(num * num + " ");
}
}
}

20. Find the Largest Element


Input: {1, 2, 1, 3, 2, 4}
Output: 4
java
Copy code
public class LargestElement {
public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
int max = arr[0];

for (int num : arr) {


if (num > max) {
max = num;
}
}

System.out.println("Largest element: " + max);


}
}

21. Find the Smallest Element


Input: {1, 2, 1, 3, 2, 4}
Output: 1
java
Copy code
public class SmallestElement {
public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
int min = arr[0];

for (int num : arr) {


if (num < min) {
min = num;
}
}

System.out.println("Smallest element: " + min);


}
}

22. Find the 2nd Largest Element


Input: {1, 2, 1, 3, 2, 4}
Output: 3
java
Copy code
import java.util.Arrays;

public class SecondLargestElement {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
Arrays.sort(arr);
System.out.println("2nd Largest Element: " + arr[arr.length - 2]);
}
}

23. Find the 2nd Smallest Element


Input: {1, 2, 1, 3, 2, 4}
Output: 2
java
Copy code
import java.util.Arrays;

public class SecondSmallestElement {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
Arrays.sort(arr);
System.out.println("2nd Smallest Element: " + arr[1]);
}
}

24. Convert 0 to -1 and -1 to 0


Input: {0, 2, -1, 0, 2, -1, 3}
Output: {-1, 2, 0, -1, 2, 0, 3}
java
Copy code
public class ConvertZeroAndNegativeOne {
public static void main(String[] args) {
int[] arr = {0, 2, -1, 0, 2, -1, 3};

for (int i = 0; i < arr.length; i++) {


if (arr[i] == 0) {
arr[i] = -1;
} else if (arr[i] == -1) {
arr[i] = 0;
}
}

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};

for (int num : arr) {


int sum = num;
while (sum >= 10) {
sum = sumDigits(sum);
}
System.out.print(sum + " ");
}
}

public static int sumDigits(int num) {


int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
}

Would you like me to continue with the remaining questions?


4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for the next set of questions (26–40):

26. Remove Elements Present More Than Once


Input: {1, 2, 1, 3, 2, 4}
Output: {3, 4}
java
Copy code
import java.util.HashSet;
import java.util.Set;

public class RemoveDuplicatesMoreThanOnce {


public static void main(String[] args) {
int[] arr = {1, 2, 1, 3, 2, 4};
Set<Integer> unique = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();

for (int num : arr) {


if (!unique.add(num)) {
duplicates.add(num);
}
}

Set<Integer> result = new HashSet<>();


for (int num : arr) {
if (!duplicates.contains(num)) {
result.add(num);
}
}

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}

java // Temporary array to store unique elements


int[] uniqueArr = new int[arr.length];
Copy code int uniqueIndex = 0;

import java.util.HashSet; for (int i = 0; i < arr.length; i++) {


boolean isDuplicate = false;
import java.util.Set;
// Check if the element is already in the uniqueArr
for (int j = 0; j < uniqueIndex; j++) {
if (arr[i] == uniqueArr[j]) {
public class RemoveDuplicates { isDuplicate = true;
break;
public static void main(String[] args) {
}
int[] arr = {1, 2, 1, 3, 2, 4}; }

Set<Integer> unique = new HashSet<>(); // If not a duplicate, add it to the uniqueArr


if (!isDuplicate) {
uniqueArr[uniqueIndex] = arr[i];
uniqueIndex++;
for (int num : arr) { }
}
unique.add(num);
// Print the unique elements
} System.out.print("Array without duplicates: ");
for (int i = 0; i < uniqueIndex; i++) {
System.out.print(uniqueArr[i] + " ");
System.out.println(unique); }
}
} }

28. Reverse Each Element


Input: {23, 55, 57, 93, 1}
Output: {32, 55, 75, 39, 1}
java
Copy code
public class ReverseElements {
public static void main(String[] args) {
int[] arr = {23, 55, 57, 93, 1};

for (int num : arr) {


System.out.print(reverse(num) + " ");
}
}

public static int reverse(int num) {


int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
}

29. Add Elements of 2 Arrays


Test Case 1:
Input 1: {2, 3, 4, 1}
Input 2: {2, 3, 5, 6, 7, 8, 9}
Output: {4, 6, 9, 7, 7, 8, 9}
java
Copy code
public class AddArrays {
public static void main(String[] args) {
int[] arr1 = {2, 3, 4, 1};
int[] arr2 = {2, 3, 5, 6, 7, 8, 9};

int length = Math.max(arr1.length, arr2.length);


int[] result = new int[length];

for (int i = 0; i < length; i++) {


int val1 = i < arr1.length ? arr1[i] : 0;
int val2 = i < arr2.length ? arr2[i] : 0;
result[i] = val1 + val2;
}
for (int num : result) {
System.out.print(num + " ");
}
}
}

30. Sum of Even Digits of Each Element


Input: {231, 554, 572, 962, 24, 1}
Output: {2, 4, 2, 8, 6, 1}
java
Copy code
public class SumEvenDigits {
public static void main(String[] args) {
int[] arr = {231, 554, 572, 962, 24, 1};

for (int num : arr) {


System.out.print(sumEvenDigits(num) + " ");
}
}

public static int sumEvenDigits(int num) {


int sum = 0;
while (num > 0) {
int digit = num % 10;
if (digit % 2 == 0) {
sum += digit;
}
num /= 10;
}
return sum;
}
}
31. Merge Two Arrays
Input 1: {1, 2, 3}
Input 2: {4, 5, 6}
Output: {1, 2, 3, 4, 5, 6}
java
Copy code
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};

int[] result = new int[arr1.length + arr2.length];


System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);

for (int num : result) {


System.out.print(num + " ");
}
}
}

32. Shift Zeros to the Right


Input: {2, 0, 5, 3, 1, 0, 3, 1}
Output: {2, 5, 3, 1, 3, 1, 0, 0}
java
Copy code
public class ShiftZerosRight {
public static void main(String[] args) {
int[] arr = {2, 0, 5, 3, 1, 0, 3, 1};
int n = arr.length;
int j = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
arr[j++] = arr[i];
}
}

while (j < n) {
arr[j++] = 0;
}

for (int num : arr) {


System.out.print(num + " ");
}
}
}

33. Shift Zeros to the Left


Input: {2, 0, 5, 3, 1, 0, 3, 1}
Output: {0, 0, 2, 5, 3, 1, 3, 1}

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;

for (int i = n - 1; i >= 0; i--) {


if (arr[i] != 0) {
arr[j--] = arr[i];
}
}
while (j >= 0) {
arr[j--] = 0;
}

for (int num : arr) {


System.out.print(num + " ");
}
}
}

34. Find the Length of an Array Without Using Length Attribute


java
Copy code
public class LengthWithoutAttribute {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int length = 0;

for (int num : arr) {


length++;
}

System.out.println("Array length: " + length);


}
}

35. Print Repeated Elements


Input: {1, 2, 3, 4, 3, 2}
Output: {2, 3}
java
Copy code
import java.util.HashSet;
import java.util.Set;
public class RepeatedElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 3, 2};

System.out.print("Repeated elements: ");


public class RepeatedElements { for (int i = 0; i < arr.length; i++) {
boolean isPrinted = false;
public static void main(String[] args) {
// Check if the current element has already
int[] arr = {1, 2, 3, 4, 3, 2}; been printed
for (int k = 0; k < i; k++) {
Set<Integer> unique = new HashSet<>(); if (arr[i] == arr[k]) {
isPrinted = true;
Set<Integer> repeated = new HashSet<>(); break;
}
}
for (int num : arr) { if (isPrinted) {
continue;
if (!unique.add(num)) { }

repeated.add(num); // Check if the current element is repeated


for (int j = i + 1; j < arr.length; j++) {
} if (arr[i] == arr[j]) {
System.out.print(arr[i] + " ");
} break;
}
}
}
System.out.println(repeated); }
}
}
}

36. Insert 5 in Index 3


Input: {1, 2, 3, 4}
Output: {1, 2, 3, 5, 4}
java
Copy code
public class InsertAtIndex {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int index = 3;
int value = 5;

int[] newArr = new int[arr.length + 1];


for (int i = 0; i < index; i++) {
newArr[i] = arr[i];
}
newArr[index] = value;
for (int i = index; i < arr.length; i++) {
newArr[i + 1] = arr[i];
}

for (int num : newArr) {


System.out.print(num + " ");
}
}
}

Would you like to proceed with the rest of the questions?


4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are the solutions for the remaining questions (37–57):

37. Delete 2nd Index Element


Input: {1, 2, 3, 4}
Output: {1, 2, 4}
java
Copy code
public class DeleteElementAtIndex {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int index = 2;

int[] newArr = new int[arr.length - 1];


for (int i = 0, j = 0; i < arr.length; i++) {
if (i != index) {
newArr[j++] = arr[i];
}
}

for (int num : newArr) {


System.out.print(num + " ");
}
}
}

38. Print Distinct Elements in Two Arrays


Input 1: {1, 2, 4, 5, 6} public class DistinctElements {
public static void main(String[] args) {
Input 2: {2, 5, 8, 9, 3, 1} int[] arr1 = {1, 2, 4, 5, 6};
Output: {4, 6, 8, 9, 3} int[] arr2 = {2, 5, 8, 9, 3, 1};
java System.out.print("Distinct elements: ");
Copy code // Print distinct elements from arr1
for (int i = 0; i < arr1.length; i++) {
import java.util.HashSet; boolean isCommon = false;
for (int j = 0; j < arr2.length; j++) {
import java.util.Set; if (arr1[i] == arr2[j]) {
isCommon = true;
break;
}
public class DistinctElements { }
if (!isCommon) {
public static void main(String[] args) { System.out.print(arr1[i] + " ");
}
int[] arr1 = {1, 2, 4, 5, 6}; }

int[] arr2 = {2, 5, 8, 9, 3, 1}; // Print distinct elements from arr2


for (int i = 0; i < arr2.length; i++) {
boolean isCommon = false;
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
Set<Integer> set1 = new HashSet<>(); isCommon = true;
break;
Set<Integer> set2 = new HashSet<>(); }
}
if (!isCommon) {
System.out.print(arr2[i] + " ");
for (int num : arr1) set1.add(num); }
}
for (int num : arr2) set2.add(num); }
}

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};

System.out.print("Common elements: ");


import java.util.HashSet; for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
import java.util.Set; if (arr1[i] == arr2[j]) {
System.out.print(arr1[i] + " ");
break; // Avoid duplicate printing for the
same element
public class CommonElements { }
}
public static void main(String[] args) { }
}
int[] arr1 = {1, 2, 4, 5, 6}; }
int[] arr2 = {2, 5, 8, 9, 3, 1};

Set<Integer> set1 = new HashSet<>();


Set<Integer> set2 = new HashSet<>();

for (int num : arr1) set1.add(num);


for (int num : arr2) set2.add(num);

set1.retainAll(set2);
System.out.println(set1);
}
}

40. Shift Half of the Elements to the Right


Input: {2, 5, 8, 9, 3, 1}
Output: {9, 3, 1, 2, 5, 8}
java
Copy code
public class ShiftHalfRight {
public static void main(String[] args) {
int[] arr = {2, 5, 8, 9, 3, 1};
int n = arr.length;

int[] result = new int[n];


int mid = n / 2;

// Shift half of the elements to the right


for (int i = 0; i < mid; i++) {
result[i + mid] = arr[i];
}
for (int i = mid; i < n; i++) {
result[i - mid] = arr[i];
}

for (int num : result) {


System.out.print(num + " ");
}
}
}

41. Print Missing Elements public class MissingElements {


public static void main(String[] args) {
Input: {2, 4, 6, 8, 13} int[] arr = {2, 4, 6, 8, 13};
Output: {3, 5, 7, 9, 10, 11, 12} int max = 13;
java System.out.print("Missing elements: ");
for (int i = 1; i < max; i++) {
Copy code boolean found = false;
public class MissingElements { for (int num : arr) {
if (num == i) {
public static void main(String[] args) { found = true;
break;
int[] arr = {2, 4, 6, 8, 13}; }
}
int max = 13; if (!found) {
System.out.print(i + " ");
Set<Integer> present = new HashSet<>(); }
}
for (int num : arr) { }
}
present.add(num);
}
for (int i = 1; i < max; i++) {
if (!present.contains(i)) {
System.out.print(i + " ");
}
}
}
}

42. Convert Numbers into Words


Input: 2917
Output: two thousand nine hundred seventeen
java
Copy code
public class NumberToWords {
private static final String[] below20 = {"", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
private static final String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"};
private static final String[] thousands = {"", "thousand"};

public static void main(String[] args) {


int num = 2917;
System.out.println(convertToWords(num));
}

public static String convertToWords(int num) {


if (num == 0) return "zero";
StringBuilder sb = new StringBuilder();
int i = 0;
while (num > 0) {
if (num % 1000 != 0) {
sb.insert(0, helper(num % 1000) + thousands[i] + " ");
}
num /= 1000;
i++;
}
return sb.toString().trim();
}

private static String helper(int num) {


if (num == 0) return "";
else if (num < 20) return below20[num] + " ";
else if (num < 100) return tens[num / 10] + " " + helper(num % 10);
else return below20[num / 100] + " hundred " + helper(num % 100);
}
}

43. Shuffle Array Elements Using Math.random()


java
Copy code
import java.util.Random;

public class ShuffleArray {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
Random rand = new Random();

for (int i = 0; i < arr.length; i++) {


int j = rand.nextInt(arr.length);
// Swap arr[i] with arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}

44. Remove Elements Before a Given Index


Test Case 1:
Input: {1, 2, 4, 5, 1, 3, 2}
Index: 3
Output: {5, 1, 3, 2}
java
Copy code
public class RemoveBeforeIndex {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 1, 3, 2};
int index = 3;

int[] newArr = new int[arr.length - index];


for (int i = index; i < arr.length; i++) {
newArr[i - index] = arr[i];
}

for (int num : newArr) {


System.out.print(num + " ");
}
}
}

45. Copy an Array Using arraycopy()


java
Copy code
public class ArrayCopy {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int[] copy = new int[arr.length];
System.arraycopy(arr, 0, copy, 0, arr.length);

for (int num : copy) {


System.out.print(num + " ");
}
}
}

46. Linear Search


java
Copy code
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 30;
int index = linearSearch(arr, target);
System.out.println("Found at index: " + index);
}

public static int linearSearch(int[] arr, int target) {


for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1; // Not found
}
}
47. Binary Search
java
Copy code
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 30;
int index = binarySearch(arr, target);
System.out.println("Found at index: " + index);
}

public static int binarySearch(int[] arr, int target) {


int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Not found
}
}

48. Sort Array in Ascending Order


Input: {9, 5, 6, 1, 2, 7}
Output: {1, 2, 5, 6, 7, 9}
java
Copy code
import java.util.Arrays;

public class SortAscending {


public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}

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):

49. Sort Array in Descending Order


Input: {9, 5, 6, 1, 2, 7}
Output: {9, 7, 6, 5, 2, 1}
java
Copy code
import java.util.Arrays;
import java.util.Collections;

public class SortDescending {


public static void main(String[] args) {
Integer[] arr = {9, 5, 6, 1, 2, 7}; // Use Integer instead of int for descending sort
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(Arrays.toString(arr));
}
}
50. Bubble Sort
java
Copy code
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
bubbleSort(arr);

for (int num : arr) {


System.out.print(num + " ");
}
}

public static void bubbleSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
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;
}
}
}
}
}

51. Insertion Sort


java
Copy code
public class InsertionSort {
public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
insertionSort(arr);

for (int num : arr) {


System.out.print(num + " ");
}
}

public static void insertionSort(int[] arr) {


int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
}

52. Selection Sort


java
Copy code
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
selectionSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}

public static void selectionSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

53. Quick Sort


java
Copy code
public class QuickSort {
public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
quickSort(arr, 0, arr.length - 1);

for (int num : arr) {


System.out.print(num + " ");
}
}

public static void quickSort(int[] arr, int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

public static int partition(int[] arr, int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i + 1] and arr[high]
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}

54. Merge Sort


java
Copy code
public class MergeSort {
public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
mergeSort(arr, 0, arr.length - 1);

for (int num : arr) {


System.out.print(num + " ");
}
}

public static void mergeSort(int[] arr, int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

int[] L = new int[n1];


int[] R = new int[n2];

System.arraycopy(arr, left, L, 0, n1);


System.arraycopy(arr, mid + 1, R, 0, n2);

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++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}
}

55. Sort Array Using the sort() Method


java
Copy code
import java.util.Arrays;

public class SortUsingSortMethod {


public static void main(String[] args) {
int[] arr = {9, 5, 6, 1, 2, 7};
Arrays.sort(arr);

System.out.println(Arrays.toString(arr));
}
}

56. Arrange a String Array in Ascending Order


Input: {"Ram", "Amar", "Cel", "David", "Bunny"}
Output: {"Amar", "Bunny", "Cel", "David", "Ram"}
java
Copy code
import java.util.Arrays;

public class SortStringArray {


public static void main(String[] args) {
String[] arr = {"Ram", "Amar", "Cel", "David", "Bunny"};
Arrays.sort(arr);

System.out.println(Arrays.toString(arr));
}
}

57. Arrange a Character Array in Ascending Order


Input: {'R', 'A', 'C', 'D', 'B'}
Output: {'A', 'B', 'C', 'D', 'R'}
java
Copy code
import java.util.Arrays;

public class SortCharacterArray {


public static void main(String[] args) {
char[] arr = {'R', 'A', 'C', 'D', 'B'};
Arrays.sort(arr);

System.out.println(Arrays.toString(arr));
}
}

You might also like