Practice essential array programs in C including reversing arrays, rotating elements, finding duplicates, merging sorted arrays, and more coding problems with solutions.
The best way to master arrays in C is through hands-on practice. This section contains carefully selected array programs that are frequently asked in interviews and programming exams. Each solution includes a clear explanation of the approach, complete code, and expected output.
Program 1: Reverse an Array
Approach: Two-Pointer Technique
Use two pointers — one at the start and one at the end. Swap them and move inward until they meet.
#include <stdio.h>
void reverseArray(int arr[], int n) {
int left = 0, right = n - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original: ");
printArray(arr, n);
reverseArray(arr, n);
printf("Reversed: ");
printArray(arr, n);
return 0;
}
Original: 10 20 30 40 50 60
Reversed: 60 50 40 30 20 10
| [10][20][30][40][50][60] left=0, right=5 | swap |
| [60][20][30][40][50][10] left=1, right=4 | swap |
| [60][50][30][40][20][10] left=2, right=3 | swap |
| [60][50][40][30][20][10] left=3, right=2 | STOP (left >= right) |
Time: O(n/2) = O(n) | Space: O(1)
Program 2: Rotate Array by K Positions
Left Rotation
#include <stdio.h>
void leftRotate(int arr[], int n, int k) {
k = k % n; // Handle k > n
// Reversal algorithm
// Step 1: Reverse first k elements
for (int i = 0, j = k - 1; i < j; i++, j--) {
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
// Step 2: Reverse remaining elements
for (int i = k, j = n - 1; i < j; i++, j--) {
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
// Step 3: Reverse entire array
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int n = 7, k = 3;
printf("Original: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
leftRotate(arr, n, k);
printf("\nLeft rotate %d: ", k);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Original: 1 2 3 4 5 6 7
Left rotate 3: 4 5 6 7 1 2 3
| Original | [1, 2, 3, 4, 5, 6, 7] |
| Step 1 | [3, 2, 1, 4, 5, 6, 7] ← Reverse first 3 |
| Step 2 | [3, 2, 1, 7, 6, 5, 4] ← Reverse last 4 |
| Step 3 | [4, 5, 6, 7, 1, 2, 3] ← Reverse entire array ✓ |
Time: O(n) | Space: O(1)
Program 3: Find Duplicates in an Array
#include <stdio.h>
void findDuplicates(int arr[], int n) {
printf("Duplicate elements: ");
int found = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
printf("%d ", arr[i]);
found = 1;
break; // Avoid printing same duplicate multiple times
}
}
}
if (!found) printf("None");
printf("\n");
}
// Optimized approach for values in range [0, n-1]
void findDuplicatesOptimized(int arr[], int n) {
printf("Duplicates (optimized): ");
for (int i = 0; i < n; i++) {
int index = arr[i] % n;
arr[index] += n;
}
for (int i = 0; i < n; i++) {
if (arr[i] / n >= 2)
printf("%d ", i);
}
printf("\n");
}
int main() {
int arr[] = {4, 3, 2, 7, 8, 2, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
findDuplicates(arr, n);
return 0;
}
Program 4: Merge Two Sorted Arrays
#include <stdio.h>
void mergeSorted(int a[], int m, int b[], int n, int result[]) {
int i = 0, j = 0, k = 0;
while (i < m && j < n) {
if (a[i] <= b[j])
result[k++] = a[i++];
else
result[k++] = b[j++];
}
// Copy remaining elements
while (i < m) result[k++] = a[i++];
while (j < n) result[k++] = b[j++];
}
int main() {
int a[] = {1, 3, 5, 7, 9};
int b[] = {2, 4, 6, 8, 10, 12};
int m = 5, n = 6;
int result[11];
mergeSorted(a, m, b, n, result);
printf("Array A: ");
for (int i = 0; i < m; i++) printf("%d ", a[i]);
printf("\nArray B: ");
for (int i = 0; i < n; i++) printf("%d ", b[i]);
printf("\nMerged: ");
for (int i = 0; i < m + n; i++) printf("%d ", result[i]);
printf("\n");
return 0;
}
Array A: 1 3 5 7 9
Array B: 2 4 6 8 10 12
Merged: 1 2 3 4 5 6 7 8 9 10 12
Program 5: Find Second Largest Element
#include <stdio.h>
#include <limits.h>
int secondLargest(int arr[], int n) {
if (n < 2) return INT_MIN;
int first = INT_MIN, second = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
return second;
}
int main() {
int arr[] = {45, 78, 23, 90, 67, 90, 12};
int n = sizeof(arr) / sizeof(arr[0]);
int result = secondLargest(arr, n);
if (result == INT_MIN)
printf("No second largest element\n");
else
printf("Second largest: %d\n", result);
return 0;
}
Program 6: Move Zeros to End
#include <stdio.h>
void moveZerosToEnd(int arr[], int n) {
int writePos = 0;
// Move all non-zero elements forward
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
arr[writePos++] = arr[i];
}
}
// Fill remaining positions with zeros
while (writePos < n) {
arr[writePos++] = 0;
}
}
int main() {
int arr[] = {0, 5, 0, 3, 0, 12, 0, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Before: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
moveZerosToEnd(arr, n);
printf("\nAfter: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Before: 0 5 0 3 0 12 0 8
After: 5 3 12 8 0 0 0 0
Program 7: Find Missing Number (1 to N)
Given an array containing n-1 numbers from 1 to n, find the missing number.
#include <stdio.h>
int findMissing(int arr[], int n) {
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;
for (int i = 0; i < n - 1; i++) {
actualSum += arr[i];
}
return expectedSum - actualSum;
}
int main() {
int arr[] = {1, 2, 4, 5, 6, 7, 8}; // 3 is missing
int n = 8; // Numbers should be 1 to 8
printf("Missing number: %d\n", findMissing(arr, n));
return 0;
}
Program 8: Find Intersection of Two Arrays
#include <stdio.h>
void findIntersection(int a[], int m, int b[], int n) {
printf("Intersection: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i] == b[j]) {
printf("%d ", a[i]);
b[j] = -1; // Mark as used to handle duplicates
break;
}
}
}
printf("\n");
}
int main() {
int a[] = {1, 3, 5, 7, 9, 11};
int b[] = {2, 3, 5, 8, 9, 12};
findIntersection(a, 6, b, 6);
return 0;
}
Program 9: Check if Array is Sorted
#include <stdio.h>
int isSorted(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1])
return 0; // Not sorted
}
return 1; // Sorted
}
int main() {
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {1, 5, 3, 7, 9};
printf("arr1 sorted? %s\n", isSorted(arr1, 5) ? "Yes" : "No");
printf("arr2 sorted? %s\n", isSorted(arr2, 5) ? "Yes" : "No");
return 0;
}
arr1 sorted? Yes
arr2 sorted? No
Program 10: Leaders in an Array
An element is a leader if it's greater than all elements to its right.
#include <stdio.h>
void findLeaders(int arr[], int n) {
printf("Leaders: ");
int maxFromRight = arr[n - 1];
printf("%d ", maxFromRight); // Rightmost is always a leader
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
printf("%d ", arr[i]);
}
}
printf("\n");
}
int main() {
int arr[] = {16, 17, 4, 3, 5, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findLeaders(arr, n);
return 0;
}
Interview Questions
Q1: How do you rotate an array in O(n) time and O(1) space?
Use the reversal algorithm: reverse the first k elements, reverse the remaining n-k elements, then reverse the entire array. This achieves the rotation without extra space.
Q2: How would you find a duplicate in an array of n+1 integers where values are between 1 and n?
Floyd's cycle detection (tortoise and hare) algorithm can find the duplicate in O(n) time and O(1) space by treating array values as pointers to the next index.
Q3: What's the most efficient way to merge two sorted arrays?
The two-pointer technique gives O(m+n) time. Start with pointers at the beginning of both arrays, compare elements, and copy the smaller one to the result array.
Q4: How do you find the majority element (appears more than n/2 times)?
Boyer-Moore Voting Algorithm: maintain a candidate and count. Increment count for same element, decrement for different. The candidate surviving after one pass is the majority element (verify with a second pass).
Summary
- Reversing arrays uses the two-pointer swap technique — O(n) time, O(1) space
- Array rotation can be done efficiently using the reversal algorithm
- Finding duplicates has multiple approaches depending on value constraints
- Merging sorted arrays uses the two-pointer merge technique
- Most array problems benefit from sorting as a preprocessing step
- The key patterns are: two pointers, sliding window, frequency counting, and sorting