Day 2 of 100 Days of DSA Challenge: Array Basics
Table of contents
Welcome to Day 2 of the 100 Days DSA (Data Structures and Algorithms) Challenge! Today, we are diving into the fascinating world of arrays. Arrays are fundamental structures that hold elements in a connected block of memory, and understanding them is crucial for mastering data structures and algorithms. Here's what we have planned for today's challenge:
1. Finding the Largest and Smallest Elements in an Array
The task of identifying the largest and smallest elements in an array helps you get accustomed to iterating through arrays and using conditional statements effectively.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int n, i;
int largest, smallest;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
smallest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}
printf("Largest element: %d\n", largest);
printf("Smallest element: %d\n", smallest);
return 0;
}
2. Finding the Second Largest Element in an Array
Finding the second largest element is a bit trickier than finding the largest. This exercise will test your ability to maintain multiple variables and apply logical conditions.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int n, i;
int largest, secondLargest;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = secondLargest = -2147483648;
for (i = 0; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
if (secondLargest == -2147483648) {
printf("There is no second largest element in the array.\n");
} else {
printf("The second largest element is: %d\n", secondLargest);
}
return 0;
}
3. Rotating an Array to the Left by k Positions
Array rotation is a common operation in many algorithms. Rotating an array to the left by k positions involves moving each element k steps towards the beginning and wrapping around elements to the end of the array.
Placeholder for Code Example:
#include <stdio.h>
void rotateLeft(int arr[], int n, int k) {
int temp[k];
for (int i = 0; i < k; i++) {
temp[i] = arr[i];
}
for (int i = 0; i < n - k; i++) {
arr[i] = arr[i + k];
}
for (int i = 0; i < k; i++) {
arr[n - k + i] = temp[i];
}
}
int main() {
int n, k;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number of positions to rotate: ");
scanf("%d", &k);
rotateLeft(arr, n, k);
printf("Array after rotation:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
4. Counting the Number of Occurrences of a Given Element in an Array
Counting the occurrences of a specific element within an array is an essential skill for many counting and frequency-related algorithms. This task will help you practice loops and conditional checks.
Placeholder for Code Example:
#include <stdio.h>
int main() {
int n, i, element, count = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to count: ");
scanf("%d", &element);
for (i = 0; i < n; i++) {
if (arr[i] == element) {
count++;
}
}
printf("The element %d occurs %d times in the array.\n", element, count);
return 0;
}
5. Finding All Elements that Appear More Than Once in an Array
This is a common problem that requires the use of additional data structures such as hash tables or sorting algorithms to efficiently find duplicates.
Placeholder for Code Example:
#include <stdio.h>
void findDuplicates(int arr[], int n) {
int hash[1000] = {0};
int i, found = 0;
for (i = 0; i < n; i++) {
hash[arr[i]]++;
}
printf("Duplicate elements in the array:\n");
for (i = 0; i < 1000; i++) {
if (hash[i] > 1) {
printf("%d appears %d times\n", i, hash[i]);
found = 1;
}
}
if (!found) {
printf("No duplicates found.\n");
}
}
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findDuplicates(arr, n);
return 0;
}
Conclusion
By completing these exercises, you will solidify your understanding of basic array operations. Arrays are the building blocks for more advanced data structures and algorithms, and mastering these basics is essential. Keep pushing through, and you'll be well-prepared for the challenges to come.
Subscribe to my newsletter
Read articles from Rishi Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by