Arrays: Practice Problems II
What's up, buddies? Feeling confident with the knowledge of arrays you have gained!
So, it's time for some warm-up.
Program1: Write a java program to print all negative elements in an array.
Input
10
-1 -10 100 5 61 -2 -23 8 -90 51
Output
-1
-10
-2
-23
-90
Approach
Hint
You already know the way of printing elements of an array. Isn't it? Just iterate similarly. But before printing, check if the element is negative.
Solution
import java.util.Scanner;
public class NegativeElementsInArray {
public static void main(String[] args) {
//Input size of the array
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
//Declare the array
int[] arr = new int[size];
//Input elements in the array
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < size; i++) {
// If current array element is negative
if (arr[i] < 0) {
System.out.println(arr[i]);
}
}
scanner.close();
}
}
Program 2: Write a java program to find maximum and minimum element in array.
Input
5
10 50 12 16 2
Output
Maximum: 50
Minimun: 2
Approach
Hint
Okay, so you already know how to check elements of an array while iterating. Use the same concept to find the maximum and minimum elements of the array.
Explanation
How are we going to do that? Assume we have several boxes, each containing a different size toy car, and we want to find the box with the largest toy car. So, what do we do now? Considering first box has the largest toy car, we proceed to the next box. We check to see if this car is larger than the previous one, and if it is, we consider it to be the largest. When we check the third box, we discover that this car is smaller than our largest, so we skip it. Again, the fourth box has larger car than the car we considered as the largest. As a result, we replace our largest car with the car from the fourth box. Similarly, we compare the other cars and update the largest car. Finally, we have the box with the largest car.
I hope now you can think of finding the box with smallest car. Easy, right?
This is what we need to do in case of arrays too to find the maximum and minimum element.
Solution Code:
import java.util.Scanner;
public class MaxMinInArray {
public static void main(String[] args) {
// Input size of the array
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
//Declare the array
int[] arr = new int[size];
// Input array elements
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
// Assume first element as maximum and minimum
int maxElement = arr[0];
int minElement = arr[0];
for (int i = 1; i < size; i++) {
// If current element is greater than max
if (arr[i] > maxElement) {
maxElement = arr[i];
}
// If current element is smaller than min
if (arr[i] < minElement) {
minElement = arr[i];
}
}
// Print maximum and minimum element
System.out.println("Maximum: " + maxElement);
System.out.println("Minimum: " + minElement);
scanner.close();
}
}
Problem 3: Write a java program to to count total number of even and odd elements in an array.
Input
9
1 2 3 4 5 6 7 8 9
Output
Total even elements: 4
Total odd elements: 5
Approach
Hint
Let's simplify!
An even number is one that can be divided into two equal halves. If not, it is an odd number.
Let's say you have four candies and want to share them with your best friend. You keep two candies and give the remaining two to your best friend. Since, 4 was could be easily divided into 2 parts, 4 is an even number.
Okay! Now, assume you have 5 candies. Again you want to share it equally with your best friend. You keep two candies for yourself and give two to your friend. There is now only one candy left. You don't want to give your friend an extra candy, and you can't even keep this candy for yourself too(your friend will be unhappy). Strange situation, right? Hence, the name. The number 5 is an odd number. This left candy is known as a remainder. When we divide odd numbers by 2, we always get a 1 as a remainder. In the case of an even number, the remainder is 0.
Can you think of an operator who can assist you in obtaining the remainder?
Yes, you got it right! It is the modulo operator. (Check out the fundamental section if you haven't already!)
So, simply iterate through the array's elements, making sure to check and maintain counts for both even and odd elements. That's all!
Solution Code:
import java.util.Scanner;
public class EvenOddCount {
public static void main(String[] args) {
// Input size of the array
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
//Declare the array
int[] arr = new int[size];
// Input array elements
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
// Assuming that there are 0 even and odd elements
int evenCount = 0;
int oddCount = 0;
for (int i = 0; i < size; i++) {
// If the current element of the array is even then increment even count
if (arr[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
System.out.println("Total even elements: " + evenCount);
System.out.println("Total odd elements: " + oddCount);
scanner.close();
}
}
Problem 4: Write a java program to print the reverse of array.
Input
5
10, 5, 16, 35, 500
Output
500, 35, 16, 5, 10
Approach
Hint
You already know how to print the elements of the array. Can you think of how we can print the elements in the reverse order? Just traverse the elements in the reverse order and print the elements simultaneously.
Explanation/Steps
- Iterate through the elements in reverse order by running a loop from size - 1 to 0.
- Print the element as you iterate.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input size of array
Scanner scanner = new Scanner(System.in);
System.out.println("Enter size of the array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
// Input array elements
System.out.println("Enter elements in array: ");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
// Print array in reversed order
System.out.println("Array in reverse order: ");
for (int i = size - 1; i >= 0; i--) {
System.out.print(arr[i] + "\t");
}
System.out.println();
scanner.close();
}
}
Great Going!