In today's journey of the 100 Days of DSA challenge, I explored fundamental array operations. Arrays are a versatile data structure, and mastering their operations is crucial for solving problems effectively. Here are the problems I tackled, along with their Java implementations.
1. Find the Largest and Smallest Elements in an Array
The task is to traverse the array once, maintaining variables to track the largest and smallest values.
import java.util.*;
public class LargestandSmallest{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int count = sc.nextInt();
int[] arr = new int[count];
int min = 0;
int max = 0;
System.out.println("Enter elements of the array:");
for(int i=0; i<count; i++){
arr[i] = sc.nextInt();
if(i==0){
min = arr[i];
max = arr[i];
}
if(arr[i]>max)
max = arr[i];
else if(arr[i]<min)
min = arr[i];
}
System.out.println("Largest element is: ".concat(Integer.toString(max)));
System.out.println("Smallest element is: ".concat(Integer.toString(min)));
}
}
2. Find the Second Largest Element in an Array
This involves maintaining two variables: one for the largest element and another for the second largest.
import java.util.*;
public class SecoundLargestElement{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int count = sc.nextInt();
int[] arr = new int[count];
int ans = 0;
int max = 0;
System.out.println("Enter elements of the array:");
for(int i=0; i<count; i++){
arr[i] = sc.nextInt();
if(i==0){
ans = arr[i];
max = arr[i];
}
if(arr[i]>max){
ans = max;
max = arr[i];
}
else if(arr[i]>ans)
ans = arr[i];
}
sc.close();
System.out.println("Secound Largest element is: ".concat(Integer.toString(ans)));
}
}
3. Rotate an Array to the Left by k Positions
To rotate an array to the left, shift elements k positions to the left, and wrap around the first k elements.
import java.util.*;
public class RotateArrayLeft {
public static void printArray(int[] arr){
for(int i=0; i<arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void rotateArray(int[] arr){
int num = arr[0];
for(int i=0; i<arr.length-1; i++)
arr[i] = arr[i+1];
arr[arr.length-1] = num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements in the array: ");
int count = sc.nextInt();
int[] arr = new int[count];
System.out.println("Enter elements of the array: ");
for(int i=0; i<count; i++)
arr[i] = sc.nextInt();
System.out.println("Enter the value of k(no. of positions): ");
int k = sc.nextInt();
if(count==1)
printArray(arr);
else{
while(k>0){
rotateArray(arr);
k--;
}
printArray(arr);
}
}
}
4. Count the Occurrences of a Given Element in an Array
By iterating through the array, count how many times the target element appears.
import java.util.*;
public class ElementOccurrences {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int count = sc.nextInt();
int[] arr = new int[count];
System.out.println("Enter elements of the array: ");
for(int i=0; i<count; i++)
arr[i] = sc.nextInt();
System.out.println("Which element's occurrence do you want to find out?");
int num = sc.nextInt();
int ans = 0;
for(int i=0; i<count; i++)
ans = (arr[i]==num)?ans+1:ans;
System.out.println("Element "+num+" has "+ans+" occurrences.");
}
}
5. Find All Elements That Appear More Than Once in an Array
This problem can be solved by a brute force approach in the following way.
import java.util.*;
public class DulicatesInArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int count = sc.nextInt();
int[] arr = new int[count];
ArrayList<Integer> ans = new ArrayList<>();
System.out.println("Enter elements of the array: ");
for(int i=0; i<count; i++)
arr[i] = sc.nextInt();
for(int i=0; i<count; i++){
int num = 0;
for(int j=0; j<count; j++){
if(i==j)
continue;
else if(arr[i] == arr[j])
num++;
}
if(num>0 && !ans.contains(arr[i]))
ans.add(arr[i]);
}
System.out.println(ans);
}
}
Reflection
Day 2 was a rewarding experience, as I got to practice various operations on arrays. These operations are essential in solving many real-world programming challenges and interview problems.
Stay tuned for Day 3, where I'll explore more advanced array operations! ๐