CODING BASIC PROBLEMS-----
ARRAY---
1. **Find the Maximum and Minimum Elements in an Array**
- Given an array, find the maximum and minimum elements.
solution----
class main{
// function to find minimum array
public int minElement(int [] arr){
int min=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]<min){
min=arr[i];
}
}
return min;
}
// function to find maximum element
public int maxElement(int [] arr){
int max=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
}
return max;
}
// main function
public static void main(String [] args){
int [] myArray={15,18,56,12,9,3};
main obj=new main();
int minElement= obj.minElement(myArray);
int maxElement=obj.maxElement(myArray);
System.out.println("minimum element is:" + minElement);
System.out.println("maximum element is:" + maxElement);
}
2. **Reverse an Array**
- Given an array, reverse the elements in the array.
solution:: class main{
// reversing an array
public void reverse(int [] arr){
int n=arr.length;
for(int i=0;i<n/2;i++){
int temp=arr[i];
arr[i]=arr[n-i-1];
arr[n-i-1]=temp;
}
for(int k:arr){
System.out.println(k + " ");
}
}
// main function
public static void main(String [] args){
int [] myArray={15,18,56,9,3};
main obj=new main();
obj.reverse(myArray);
}
3. **Sum of Array Elements**
- Calculate the sum of all elements in an array.
4. **Find the Second Largest Element in an Array**
- Given an array, find the second largest element.
solution:: class Main {
public static int findSecondMax(int[] arr) {
int max = arr[0];
int secondMax = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
} else if (arr[i] > secondMax && arr[i] != max) {
secondMax = arr[i];
}
}
return secondMax;
}
public static void main(String[] args) {
int[] arr = {13, 8, 17, 18, 34, 56, 68};
int secondMax = findSecondMax(arr);
System.out.println(secondMax);
}
}
5. **Count Even and Odd Elements in an Array**
- Count the number of even and odd elements in an array.
solution:: public class Main {
public static void calcEvenOdd(int[] arr) {
int evenCount = 0;
int oddCount = 0;
for (int num : arr) {
if (num % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
System.out.println("Even: " + evenCount);
System.out.println("Odd: " + oddCount);
}
public static void main(String[] args) {
int[] arr = {34, 56, 31, 45, 67, 88, 34, 50, 23, 24, 52, 2};
calcEvenOdd(arr);
}
}
6. **Merge Two Arrays**
- Given two arrays, merge them into a single array.
7. **Remove Duplicates from an Array**
- Remove all duplicate elements from an array.
8. **Rotate Array Elements**
- Rotate the elements of an array to the right by a given number of steps.
9. **Check if an Array is Sorted**
- Check if the elements of an array are in non-decreasing order.
10. **Find the Missing Number**
- Given an array containing `n-1` distinct numbers taken from `1` to `n`, find
the missing number.
solution:: public class Main {
// Method to find missing number
public static int findMissingNumber(int[] arr, int n) {
int sumOfNumbers = n * (n + 1) / 2;
int sumOfArrayElement = 0;
for (int i = 0; i < arr.length; i++) {
sumOfArrayElement = arr[i] + sumOfArrayElement;
}
int missingNumber = sumOfNumbers - sumOfArrayElement;
return missingNumber;
}
public static void main(String[] args) {
int n = 6;
int[] array = {1, 2, 5, 4, 6};
int result = findMissingNumber(array, n);
System.out.println("Missing number: " + result);
}
}