Single Dimensional Array Programs
Single Dimensional Array Programs
int main() {
int n, i;
Page | 3
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------2. Progra
m to Find the Sum of All Elements in an Array
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------3. Progra
m to Find the Largest Element in an Array
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
Page | 4
return 0;
}
----------------------------------------------------------------------------------------4. Progra
m to Find the Smallest Element in an Array
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------5. Progra
m to Reverse an Array
#include <stdio.h>
Page | 5
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------6. Progra
m to Copy One Array to Another
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
----------------------------------------------------------------------------------------7. Progra
m to Count Even and Odd Numbers in an Array
Page | 6
#include <stdio.h>
int main() {
int n, i, even = 0, odd = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
8. Program to Search for an Element in an Array (Linear Search)
#include <stdio.h>
int main() {
int n, i, key, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
if (found) {
printf("Element found at index %d\n", i);
} else {
printf("Element not found in the array\n");
}
return 0;
}
----------------------------------------------------------------------------------------
9. Program to Sort an Array (Bubble Sort)
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
10. Program to Find the Frequency of Each Element in an Array
#include <stdio.h>
int main() {
Page | 8
int n, i, j, count;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
----------------------------------------------------------------------------------------
11. Program to Find the Second Largest Element in an Array
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
12. Program to Find the Second Smallest Element in an Array
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
P a g e | 10
----------------------------------------------------------------------------------------
13. Program to Insert an Element at a Specific Position in an Array
#include <stdio.h>
int main() {
int n, i, pos, value;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
----------------------------------------------------------------------------------------
14. Program to Delete an Element from a Specific Position in an Array
#include <stdio.h>
int main() {
int n, i, pos;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
15. Program to Merge Two Arrays
#include <stdio.h>
int main() {
int n1, n2, i;
int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
merged[i] = arr1[i];
}
printf("Merged array:\n");
for (i = 0; i < n1 + n2; i++) {
printf("%d ", merged[i]);
}
return 0;
}
----------------------------------------------------------------------------------------
16. Program to Find the Union of Two Arrays
#include <stdio.h>
int main() {
int n1, n2, i, j, k = 0;
int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
// Add elements of the second array that are not already in the union
P a g e | 13
return 0;
}
----------------------------------------------------------------------------------------
17. Program to Find the Intersection of Two Arrays
#include <stdio.h>
int main() {
int n1, n2, i, j, k = 0;
int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
return 0;
}
----------------------------------------------------------------------------------------
18. Program to Check if an Array is a Palindrome
#include <stdio.h>
int main() {
int n, i, isPalindrome = 1;
int arr[n];
if (isPalindrome) {
printf("The array is a palindrome.\n");
} else {
printf("The array is not a palindrome.\n");
}
return 0;
P a g e | 15
}
----------------------------------------------------------------------------------------
19. Program to Rotate an Array by a Given Number of Positions
#include <stdio.h>
int main() {
int n, i, d, temp;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
20. Program to Find the Mode (Most Frequent Element) in an Array
#include <stdio.h>
int main() {
int n, i, j, maxCount = 0, mode = 0;
return 0;
}
----------------------------------------------------------------------------------------
21. Program to Find the Median of an Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
22. Program to Remove Duplicates from an Array
#include <stdio.h>
int main() {
int n, i, j, k;
int arr[n];
// Remove duplicates
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
for (k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--; // Reduce the size of the array
j--; // Re-check the current index
}
}
}
return 0;
}
----------------------------------------------------------------------------------------
23. Program to Find the Kth Largest Element in an Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, k;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
24. Program to Find the Kth Smallest Element in an Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, k;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
25. Program to Check if an Array is Sorted
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int isSorted = 1;
if (isSorted) {
printf("The array is sorted in ascending order.\n");
} else {
printf("The array is not sorted in ascending order.\n");
}
return 0;
}
----------------------------------------------------------------------------------------
26. Program to Find the Missing Number in a Sequence of Numbers
#include <stdio.h>
int main() {
int n, i, sum = 0;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
27. Program to Find the First Non-Repeating Element in an Array
#include <stdio.h>
int main() {
int n, i, j;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
28. Program to Find the Equilibrium Index of an Array
#include <stdio.h>
P a g e | 21
int main() {
int n, i, totalSum = 0, leftSum = 0;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
29. Program to Find the Longest Consecutive Sequence in an Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, currentLength = 1, maxLength = 1;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
30. Program to Find the Subarray with the Maximum Sum (Kadane's
Algorithm)
#include <stdio.h>
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
P a g e | 23
int main() {
int n, i, candidate = -1, count = 0;
int arr[n];
if (count > n / 2) {
printf("Majority element is: %d\n", candidate);
} else {
printf("No majority element found.\n");
}
return 0;
}
----------------------------------------------------------------------------------------
32. Program to Find the Smallest Positive Missing Number in an Array
#include <stdio.h>
int main() {
int n, i;
P a g e | 24
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
33. Program to Rearrange an Array in Alternating Positive and Negative
Numbers
#include <stdio.h>
int main() {
int n, i;
int arr[n];
rearrange(arr, n);
return 0;
}
----------------------------------------------------------------------------------------
34. Program to Find the Maximum Product Subarray
#include <stdio.h>
return maxProduct;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
35. Program to Find the Longest Subarray with Sum K
#include <stdio.h>
return maxLength;
}
P a g e | 27
int main() {
int n, i, k;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
36. Program to Find the Minimum Swaps Required to Sort an Array
#include <stdio.h>
#include <stdlib.h>
int swaps = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != temp[i]) {
swaps++;
for (int j = i + 1; j < n; j++) {
if (arr[j] == temp[i]) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
break;
}
}
P a g e | 28
}
}
free(temp);
return swaps;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
37. Program to Find the Maximum Circular Subarray Sum
#include <stdio.h>
int totalSum = 0;
for (int i = 0; i < n; i++) {
totalSum += arr[i];
arr[i] = -arr[i];
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
38. Program to Find the Maximum Length Bitonic Subarray
#include <stdio.h>
return maxLength;
}
int main() {
int n, i;
P a g e | 30
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
39. Program to Find the Maximum Sum of i * arr[i] Among All Rotations
#include <stdio.h>
return maxVal;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
40. Program to Find the Minimum Number of Jumps to Reach the End of an
Array
#include <stdio.h>
if (steps == 0) {
jumps++;
if (i >= maxReach) return -1;
steps = maxReach - i;
}
}
return -1;
}
int main() {
int n, i;
int arr[n];
return 0;
P a g e | 32
}
----------------------------------------------------------------------------------------
41. Program to Find the Maximum Difference Between Two Elements in an
Array
#include <stdio.h>
return maxDiff;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
42. Program to Find the Smallest Subarray with Sum Greater than a Given
Value
#include <stdio.h>
sum += arr[end++];
}
while (sum > x && start < n) {
if (end - start < minLength) {
minLength = end - start;
}
sum -= arr[start++];
}
}
return minLength;
}
int main() {
int n, i, x;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
43. Program to Find the Maximum Sum of Two Non-Overlapping Subarrays
#include <stdio.h>
}
if (i >= k - 1) {
left[i] = (i == k - 1) ? sum : (sum > left[i - 1] ? sum : left[i - 1]);
}
}
return maxSum;
}
int main() {
int n, i, k;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
P a g e | 35
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
45. Program to Find the Longest Subarray with Equal Number of 0s and 1s
#include <stdio.h>
if (sum == 0) {
maxLength = i + 1;
}
P a g e | 36
if (hash[sum + n] != -1) {
if (i - hash[sum + n] > maxLength) {
maxLength = i - hash[sum + n];
}
} else {
hash[sum + n] = i;
}
}
return maxLength;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
46. Program to Find the Maximum Sum of a Subarray with At Most K
Elements
#include <stdio.h>
return maxSum;
}
P a g e | 37
int main() {
int n, i, k;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
47. Program to Find the Maximum Sum of a Subarray with Exactly K
Elements
#include <stdio.h>
return maxSum;
}
int main() {
int n, i, k;
int arr[n];
P a g e | 38
return 0;
}
----------------------------------------------------------------------------------------
48. Program to Find the Maximum Sum of a Subarray with All Unique
Elements
#include <stdio.h>
#include <stdbool.h>
int start = 0;
for (int end = 0; end < n; end++) {
while (visited[arr[end]]) {
visited[arr[start]] = false;
currentSum -= arr[start];
start++;
}
visited[arr[end]] = true;
currentSum += arr[end];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return maxSum;
}
int main() {
int n, i;
int arr[n];
scanf("%d", &arr[i]);
}
return 0;
}
----------------------------------------------------------------------------------------
49. Program to Find the Maximum Sum of a Subarray with All Elements
Divisible by K
#include <stdio.h>
return maxSum;
}
int main() {
int n, i, k;
int arr[n];
return 0;
}
P a g e | 40
----------------------------------------------------------------------------------------
50. Program to Find the Maximum Sum of a Subarray with All Elements
Prime
#include <stdio.h>
#include <stdbool.h>
return maxSum;
}
int main() {
int n, i;
int arr[n];
return 0;
}
----------------------------------------------------------------------------------------
P a g e | 41