0% found this document useful (0 votes)
3 views

Single Dimensional Array Programs

The document contains a list of 50 programs related to single-dimensional arrays, covering various operations such as input/output, searching, sorting, and manipulating elements. Each program is accompanied by C code examples demonstrating how to implement the respective functionality. The programs include tasks like finding the largest/smallest element, reversing an array, merging arrays, and calculating sums and frequencies.

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Single Dimensional Array Programs

The document contains a list of 50 programs related to single-dimensional arrays, covering various operations such as input/output, searching, sorting, and manipulating elements. Each program is accompanied by C code examples demonstrating how to implement the respective functionality. The programs include tasks like finding the largest/smallest element, reversing an array, merging arrays, and calculating sums and frequencies.

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Page | 1

Single Dimensional Array Programs


1 Program to Input and Display Elements of an Array
2 Program to Find the Sum of All Elements in an Array
3 Program to Find the Largest Element in an Array
4 Program to Find the Smallest Element in an Array
5 Program to Reverse an Array
6 Program to Copy One Array to Another
7 Program to Count Even and Odd Numbers in an Array
8 Program to Search for an Element in an Array (Linear Search)
9 Program to Sort an Array (Bubble Sort)
10 Program to Find the Frequency of Each Element in an Array
11 Program to Find the Second Largest Element in an Array
12 Program to Find the Second Smallest Element in an Array
13 Program to Insert an Element at a Specific Position in an Array
14 Program to Delete an Element from a Specific Position in an Array
15 Program to Merge Two Arrays
16 Program to Find the Union of Two Arrays
17 Program to Find the Intersection of Two Arrays
18 Program to Check if an Array is a Palindrome
19 Program to Rotate an Array by a Given Number of Positions
20 Program to Find the Mode (Most Frequent Element) in an Array
21 Program to Find the Median of an Array
22 Program to Remove Duplicates from an Array
23 Program to Find the Kth Largest Element in an Array
24 Program to Find the Kth Smallest Element in an Array
25 Program to Check if an Array is Sorted
26 Program to Find the Missing Number in a Sequence of Numbers
27 Program to Find the First Non-Repeating Element in an Array
28 Program to Find the Equilibrium Index of an Array
29 Program to Find the Longest Consecutive Sequence in an Array
30 Program to Find the Subarray with the Maximum Sum (Kadane's
Algorithm)
31 Program to Find the Majority Element in an Array (Boyer-Moore Voting
Algorithm)
32 Program to Find the Smallest Positive Missing Number in an Array
33 Program to Rearrange an Array in Alternating Positive and Negative
Numbers
34 Program to Find the Maximum Product Subarray
35 Program to Find the Longest Subarray with Sum K
36 Program to Find the Minimum Swaps Required to Sort an Array
37 Program to Find the Maximum Circular Subarray Sum
38 Program to Find the Maximum Length Bitonic Subarray
39 Program to Find the Maximum Sum of i * arr[i] Among All Rotations
40 Program to Find the Minimum Number of Jumps to Reach the End of an
Array
41 Program to Find the Maximum Difference Between Two Elements in an
Array
42 Program to Find the Smallest Subarray with Sum Greater than a Given
Value
Page | 2

43 Program to Find the Maximum Sum of Two Non-Overlapping Subarrays


44 Program to Find the Maximum Sum of a Subarray with No Adjacent
Elements
45 Program to Find the Longest Subarray with Equal Number of 0s and 1s
46 Program to Find the Maximum Sum of a Subarray with At Most K
Elements
47 Program to Find the Maximum Sum of a Subarray with Exactly K
Elements
48 Program to Find the Maximum Sum of a Subarray with All Unique
Elements
49 Program to Find the Maximum Sum of a Subarray with All Elements
Divisible by K
50 Program to Find the Maximum Sum of a Subarray with All Elements Prime

1. Program to Input and Display Elements of an Array


#include <stdio.h>

int main() {
int n, i;
Page | 3

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Elements in the array are:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}

printf("Sum of all elements in the array is: %d\n", sum);

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

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int max = arr[0];


for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

printf("Largest element in the array is: %d\n", max);

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int min = arr[0];


for (i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}

printf("Smallest element in the array is: %d\n", min);

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Array in reverse order:\n");


for (i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}

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);

int arr1[n], arr2[n];

printf("Enter %d elements for the first array:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}

for (i = 0; i < n; i++) {


arr2[i] = arr1[i];
}

printf("Elements of the second array are:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr2[i]);
}

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0) {
even++;
} else {
odd++;
}
}

printf("Number of even elements: %d\n", even);


printf("Number of odd elements: %d\n", odd);

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");


scanf("%d", &key);

for (i = 0; i < n; i++) {


if (arr[i] == key) {
found = 1;
break;
}
Page | 7

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (i = 0; i < n - 1; i++) {


for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("Sorted array in ascending order:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

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);

int arr[n], freq[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
freq[i] = -1; // Initialize frequency array with -1
}

for (i = 0; i < n; i++) {


count = 1;
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0; // Mark as counted
}
}
if (freq[i] != 0) {
freq[i] = count;
}
}

printf("Frequency of each element:\n");


for (i = 0; i < n; i++) {
if (freq[i] != 0) {
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
Page | 9

int first = arr[0], second = arr[0];

for (i = 1; i < n; i++) {


if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}

printf("Second largest element is: %d\n", second);

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int first = arr[0], second = arr[0];

for (i = 1; i < n; i++) {


if (arr[i] < first) {
second = first;
first = arr[i];
} else if (arr[i] < second && arr[i] != first) {
second = arr[i];
}
}

printf("Second smallest element is: %d\n", second);

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);

int arr[n + 1]; // Extra space for the new element

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the position to insert (0 to %d): ", n);


scanf("%d", &pos);

printf("Enter the value to insert: ");


scanf("%d", &value);

// Shift elements to the right


for (i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}

arr[pos] = value; // Insert the value

printf("Array after insertion:\n");


for (i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}

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];

printf("Enter %d elements:\n", n);


P a g e | 11

for (i = 0; i < n; i++) {


scanf("%d", &arr[i]);
}

printf("Enter the position to delete (0 to %d): ", n - 1);


scanf("%d", &pos);

// Shift elements to the left


for (i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}

printf("Array after deletion:\n");


for (i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}

return 0;
}
----------------------------------------------------------------------------------------
15. Program to Merge Two Arrays
#include <stdio.h>

int main() {
int n1, n2, i;

printf("Enter the size of the first array: ");


scanf("%d", &n1);

int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

printf("Enter the size of the second array: ");


scanf("%d", &n2);

int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

int merged[n1 + n2];

// Copy elements of the first array


for (i = 0; i < n1; i++) {
P a g e | 12

merged[i] = arr1[i];
}

// Copy elements of the second array


for (i = 0; i < n2; i++) {
merged[n1 + i] = arr2[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;

printf("Enter the size of the first array: ");


scanf("%d", &n1);

int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

printf("Enter the size of the second array: ");


scanf("%d", &n2);

int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

int unionArr[n1 + n2];

// Copy elements of the first array


for (i = 0; i < n1; i++) {
unionArr[k++] = arr1[i];
}

// Add elements of the second array that are not already in the union
P a g e | 13

for (i = 0; i < n2; i++) {


int found = 0;
for (j = 0; j < n1; j++) {
if (arr2[i] == arr1[j]) {
found = 1;
break;
}
}
if (!found) {
unionArr[k++] = arr2[i];
}
}

printf("Union of the two arrays:\n");


for (i = 0; i < k; i++) {
printf("%d ", unionArr[i]);
}

return 0;
}
----------------------------------------------------------------------------------------
17. Program to Find the Intersection of Two Arrays
#include <stdio.h>

int main() {
int n1, n2, i, j, k = 0;

printf("Enter the size of the first array: ");


scanf("%d", &n1);

int arr1[n1];
printf("Enter %d elements for the first array:\n", n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

printf("Enter the size of the second array: ");


scanf("%d", &n2);

int arr2[n2];
printf("Enter %d elements for the second array:\n", n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

int intersection[n1 < n2 ? n1 : n2];

// Find common elements


P a g e | 14

for (i = 0; i < n1; i++) {


for (j = 0; j < n2; j++) {
if (arr1[i] == arr2[j]) {
intersection[k++] = arr1[i];
break;
}
}
}

printf("Intersection of the two arrays:\n");


for (i = 0; i < k; i++) {
printf("%d ", intersection[i]);
}

return 0;
}
----------------------------------------------------------------------------------------
18. Program to Check if an Array is a Palindrome
#include <stdio.h>

int main() {
int n, i, isPalindrome = 1;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Check if the array is a palindrome


for (i = 0; i < n / 2; i++) {
if (arr[i] != arr[n - i - 1]) {
isPalindrome = 0;
break;
}
}

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the number of positions to rotate: ");


scanf("%d", &d);

// Rotate the array


for (i = 0; i < d; i++) {
temp = arr[0];
for (int j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
arr[n - 1] = temp;
}

printf("Array after rotation:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n], count[n];


P a g e | 16

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
count[i] = 0;
}

// Count frequency of each element


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count[i]++;
}
}
if (count[i] > maxCount) {
maxCount = count[i];
mode = arr[i];
}
}

printf("Mode of the array is: %d\n", mode);

return 0;
}
----------------------------------------------------------------------------------------
21. Program to Find the Median of an Array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers (used for sorting)


int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sort the array


qsort(arr, n, sizeof(int), compare);

// Find the median


if (n % 2 == 0) {
P a g e | 17

float median = (arr[(n / 2) - 1] + arr[n / 2]) / 2.0;


printf("Median of the array is: %.2f\n", median);
} else {
printf("Median of the array is: %d\n", arr[n / 2]);
}

return 0;
}
----------------------------------------------------------------------------------------
22. Program to Remove Duplicates from an Array
#include <stdio.h>

int main() {
int n, i, j, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// 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
}
}
}

printf("Array after removing duplicates:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}
----------------------------------------------------------------------------------------
23. Program to Find the Kth Largest Element in an Array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers (used for sorting)


int compare(const void *a, const void *b) {
P a g e | 18

return (*(int *)b - *(int *)a);


}

int main() {
int n, i, k;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

// Sort the array in descending order


qsort(arr, n, sizeof(int), compare);

printf("The %dth largest element is: %d\n", k, arr[k - 1]);

return 0;
}
----------------------------------------------------------------------------------------
24. Program to Find the Kth Smallest Element in an Array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers (used for sorting)


int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int main() {
int n, i, k;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

// Sort the array in ascending order


qsort(arr, n, sizeof(int), compare);
P a g e | 19

printf("The %dth smallest element is: %d\n", k, arr[k - 1]);

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];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int isSorted = 1;

// Check if the array is sorted in ascending order


for (i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
isSorted = 0;
break;
}
}

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;

printf("Enter the number of elements (excluding the missing number): ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements (sequence with one missing number):\n", n);


P a g e | 20

for (i = 0; i < n; i++) {


scanf("%d", &arr[i]);
sum += arr[i];
}

// Calculate the expected sum of the sequence


int expectedSum = (n + 1) * (n + 2) / 2;

printf("The missing number is: %d\n", expectedSum - sum);

return 0;
}
----------------------------------------------------------------------------------------
27. Program to Find the First Non-Repeating Element in an Array
#include <stdio.h>

int main() {
int n, i, j;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Find the first non-repeating element


for (i = 0; i < n; i++) {
int isUnique = 1;
for (j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("The first non-repeating element is: %d\n", arr[i]);
return 0;
}
}

printf("All elements are repeating.\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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
totalSum += arr[i];
}

// Find the equilibrium index


for (i = 0; i < n; i++) {
totalSum -= arr[i]; // Subtract the current element from the total sum
if (leftSum == totalSum) {
printf("Equilibrium index is: %d\n", i);
return 0;
}
leftSum += arr[i]; // Add the current element to the left sum
}

printf("No equilibrium index found.\n");

return 0;
}
----------------------------------------------------------------------------------------
29. Program to Find the Longest Consecutive Sequence in an Array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers (used for sorting)


int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int main() {
int n, i, currentLength = 1, maxLength = 1;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sort the array


P a g e | 22

qsort(arr, n, sizeof(int), compare);

// Find the longest consecutive sequence


for (i = 1; i < n; i++) {
if (arr[i] == arr[i - 1] + 1) {
currentLength++;
} else if (arr[i] != arr[i - 1]) {
currentLength = 1;
}
if (currentLength > maxLength) {
maxLength = currentLength;
}
}

printf("The length of the longest consecutive sequence is: %d\n", maxLength);

return 0;
}
----------------------------------------------------------------------------------------
30. Program to Find the Subarray with the Maximum Sum (Kadane's
Algorithm)
#include <stdio.h>

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int maxSum = arr[0], currentSum = arr[0];

// Find the subarray with the maximum sum


for (i = 1; i < n; i++) {
currentSum = (arr[i] > currentSum + arr[i]) ? arr[i] : currentSum + arr[i];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}

printf("The maximum sum of a subarray is: %d\n", maxSum);

return 0;
}
----------------------------------------------------------------------------------------
P a g e | 23

31. Program to Find the Majority Element in an Array (Boyer-Moore Voting


Algorithm)
#include <stdio.h>

int main() {
int n, i, candidate = -1, count = 0;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Find the candidate for the majority element


for (i = 0; i < n; i++) {
if (count == 0) {
candidate = arr[i];
count = 1;
} else if (arr[i] == candidate) {
count++;
} else {
count--;
}
}

// Verify if the candidate is the majority element


count = 0;
for (i = 0; i < n; i++) {
if (arr[i] == candidate) {
count++;
}
}

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

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Create a hash array to mark the presence of positive numbers


const int MAX = 1e6 + 2;
int hash[MAX];
for (i = 0; i < MAX; i++) {
hash[i] = 0;
}

// Mark the presence of positive numbers


for (i = 0; i < n; i++) {
if (arr[i] > 0) {
hash[arr[i]] = 1;
}
}

// Find the smallest missing positive number


for (i = 1; i < MAX; i++) {
if (hash[i] == 0) {
printf("The smallest positive missing number is: %d\n", i);
break;
}
}

return 0;
}
----------------------------------------------------------------------------------------
33. Program to Rearrange an Array in Alternating Positive and Negative
Numbers
#include <stdio.h>

void rearrange(int arr[], int n) {


int i = 0, j = n - 1;

// Separate positive and negative numbers


while (i < j) {
while (arr[i] < 0 && i < j) i++;
while (arr[j] >= 0 && i < j) j--;
if (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
P a g e | 25

// Rearrange the array in alternating order


int pos = i, neg = 0;
while (pos < n && neg < pos && arr[neg] < 0) {
int temp = arr[neg];
arr[neg] = arr[pos];
arr[pos] = temp;
pos++;
neg += 2;
}
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

rearrange(arr, n);

printf("Array after rearrangement:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}
----------------------------------------------------------------------------------------
34. Program to Find the Maximum Product Subarray
#include <stdio.h>

int maxProduct(int arr[], int n) {


int maxEndingHere = arr[0];
int minEndingHere = arr[0];
int maxProduct = arr[0];

for (int i = 1; i < n; i++) {


int temp = maxEndingHere;
maxEndingHere = (arr[i] > maxEndingHere * arr[i]) ? arr[i] : maxEndingHere *
arr[i];
maxEndingHere = (maxEndingHere > minEndingHere * arr[i]) ?
maxEndingHere : minEndingHere * arr[i];
minEndingHere = (arr[i] < minEndingHere * arr[i]) ? arr[i] : minEndingHere *
arr[i];
P a g e | 26

minEndingHere = (minEndingHere < temp * arr[i]) ? minEndingHere : temp *


arr[i];

if (maxEndingHere > maxProduct) {


maxProduct = maxEndingHere;
}
}

return maxProduct;
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxProduct(arr, n);


printf("Maximum product of a subarray is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
35. Program to Find the Longest Subarray with Sum K
#include <stdio.h>

int longestSubarrayWithSumK(int arr[], int n, int k) {


int maxLength = 0, sum = 0;
int start = 0, end = 0;

while (end < n) {


sum += arr[end];
while (sum > k && start <= end) {
sum -= arr[start];
start++;
}
if (sum == k) {
maxLength = (maxLength > (end - start + 1)) ? maxLength : (end - start +
1);
}
end++;
}

return maxLength;
}
P a g e | 27

int main() {
int n, i, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of K: ");


scanf("%d", &k);

int result = longestSubarrayWithSumK(arr, n, k);


printf("Length of the longest subarray with sum %d is: %d\n", k, result);

return 0;
}
----------------------------------------------------------------------------------------
36. Program to Find the Minimum Swaps Required to Sort an Array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers (used for sorting)


int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int minSwaps(int arr[], int n) {


int *temp = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
temp[i] = arr[i];
}

qsort(temp, n, sizeof(int), compare);

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = minSwaps(arr, n);


printf("Minimum swaps required to sort the array: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
37. Program to Find the Maximum Circular Subarray Sum
#include <stdio.h>

int kadane(int arr[], int n) {


int maxSum = arr[0], currentSum = arr[0];
for (int i = 1; i < n; i++) {
currentSum = (arr[i] > currentSum + arr[i]) ? arr[i] : currentSum + arr[i];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return maxSum;
}

int maxCircularSum(int arr[], int n) {


int maxKadane = kadane(arr, n);

int totalSum = 0;
for (int i = 0; i < n; i++) {
totalSum += arr[i];
arr[i] = -arr[i];
}

int maxWrap = totalSum + kadane(arr, n);

return (maxWrap > maxKadane) ? maxWrap : maxKadane;


P a g e | 29

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxCircularSum(arr, n);


printf("Maximum circular subarray sum is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
38. Program to Find the Maximum Length Bitonic Subarray
#include <stdio.h>

int maxBitonicLength(int arr[], int n) {


int inc[n], dec[n];
inc[0] = 1;
dec[n - 1] = 1;

// Fill increasing array


for (int i = 1; i < n; i++) {
inc[i] = (arr[i] > arr[i - 1]) ? inc[i - 1] + 1 : 1;
}

// Fill decreasing array


for (int i = n - 2; i >= 0; i--) {
dec[i] = (arr[i] > arr[i + 1]) ? dec[i + 1] + 1 : 1;
}

// Find the maximum length


int maxLength = 0;
for (int i = 0; i < n; i++) {
if (inc[i] + dec[i] - 1 > maxLength) {
maxLength = inc[i] + dec[i] - 1;
}
}

return maxLength;
}

int main() {
int n, i;
P a g e | 30

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxBitonicLength(arr, n);


printf("Maximum length of bitonic subarray is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
39. Program to Find the Maximum Sum of i * arr[i] Among All Rotations
#include <stdio.h>

int maxSumRotation(int arr[], int n) {


int sum = 0, currVal = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
currVal += i * arr[i];
}

int maxVal = currVal;


for (int i = 1; i < n; i++) {
currVal = currVal + sum - n * arr[n - i];
if (currVal > maxVal) {
maxVal = currVal;
}
}

return maxVal;
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxSumRotation(arr, n);


P a g e | 31

printf("Maximum sum of i * arr[i] among all rotations is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
40. Program to Find the Minimum Number of Jumps to Reach the End of an
Array
#include <stdio.h>

int minJumps(int arr[], int n) {


if (n <= 1) return 0;
if (arr[0] == 0) return -1;

int maxReach = arr[0], steps = arr[0], jumps = 1;

for (int i = 1; i < n; i++) {


if (i == n - 1) return jumps;

maxReach = (maxReach > i + arr[i]) ? maxReach : i + arr[i];


steps--;

if (steps == 0) {
jumps++;
if (i >= maxReach) return -1;
steps = maxReach - i;
}
}

return -1;
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = minJumps(arr, n);


if (result == -1) {
printf("It is not possible to reach the end of the array.\n");
} else {
printf("Minimum number of jumps to reach the end is: %d\n", result);
}

return 0;
P a g e | 32

}
----------------------------------------------------------------------------------------
41. Program to Find the Maximum Difference Between Two Elements in an
Array
#include <stdio.h>

int maxDifference(int arr[], int n) {


int minElement = arr[0];
int maxDiff = arr[1] - arr[0];

for (int i = 1; i < n; i++) {


if (arr[i] - minElement > maxDiff) {
maxDiff = arr[i] - minElement;
}
if (arr[i] < minElement) {
minElement = arr[i];
}
}

return maxDiff;
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxDifference(arr, n);


printf("Maximum difference between two elements is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
42. Program to Find the Smallest Subarray with Sum Greater than a Given
Value
#include <stdio.h>

int smallestSubarrayWithSum(int arr[], int n, int x) {


int minLength = n + 1;
int start = 0, end = 0;
int sum = 0;

while (end < n) {


while (sum <= x && end < n) {
P a g e | 33

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of x: ");


scanf("%d", &x);

int result = smallestSubarrayWithSum(arr, n, x);


if (result == n + 1) {
printf("No such subarray exists.\n");
} else {
printf("Length of the smallest subarray with sum greater than %d is: %d\n", x,
result);
}

return 0;
}
----------------------------------------------------------------------------------------
43. Program to Find the Maximum Sum of Two Non-Overlapping Subarrays
#include <stdio.h>

int maxSumTwoSubarrays(int arr[], int n, int k) {


int left[n], right[n];

// Compute the maximum sum subarray from the left


int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (i >= k) {
sum -= arr[i - k];
P a g e | 34

}
if (i >= k - 1) {
left[i] = (i == k - 1) ? sum : (sum > left[i - 1] ? sum : left[i - 1]);
}
}

// Compute the maximum sum subarray from the right


sum = 0;
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (i <= n - k - 1) {
sum -= arr[i + k];
}
if (i <= n - k) {
right[i] = (i == n - k) ? sum : (sum > right[i + 1] ? sum : right[i + 1]);
}
}

// Find the maximum sum of two non-overlapping subarrays


int maxSum = 0;
for (int i = k - 1; i < n - k; i++) {
if (left[i] + right[i + 1] > maxSum) {
maxSum = left[i] + right[i + 1];
}
}

return maxSum;
}

int main() {
int n, i, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

int result = maxSumTwoSubarrays(arr, n, k);


printf("Maximum sum of two non-overlapping subarrays of size %d is: %d\n", k,
result);

return 0;
}
----------------------------------------------------------------------------------------
P a g e | 35

44. Program to Find the Maximum Sum of a Subarray with No Adjacent


Elements
#include <stdio.h>

int maxSumNoAdjacent(int arr[], int n) {


int incl = arr[0];
int excl = 0;

for (int i = 1; i < n; i++) {


int temp = (incl > excl) ? incl : excl;
incl = excl + arr[i];
excl = temp;
}

return (incl > excl) ? incl : excl;


}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxSumNoAdjacent(arr, n);


printf("Maximum sum of a subarray with no adjacent elements is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
45. Program to Find the Longest Subarray with Equal Number of 0s and 1s
#include <stdio.h>

int longestSubarrayWithEqual0s1s(int arr[], int n) {


int maxLength = 0, sum = 0;
int hash[2 * n + 1];
for (int i = 0; i < 2 * n + 1; i++) {
hash[i] = -1;
}

for (int i = 0; i < n; i++) {


sum += (arr[i] == 0) ? -1 : 1;

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements (0s and 1s):\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = longestSubarrayWithEqual0s1s(arr, n);


printf("Length of the longest subarray with equal 0s and 1s is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
46. Program to Find the Maximum Sum of a Subarray with At Most K
Elements
#include <stdio.h>

int maxSumSubarrayWithAtMostKElements(int arr[], int n, int k) {


int maxSum = 0, currentSum = 0;

for (int i = 0; i < n; i++) {


currentSum += arr[i];
if (i >= k) {
currentSum -= arr[i - k];
}
if (currentSum > maxSum) {
maxSum = currentSum;
}
}

return maxSum;
}
P a g e | 37

int main() {
int n, i, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

int result = maxSumSubarrayWithAtMostKElements(arr, n, k);


printf("Maximum sum of a subarray with at most %d elements is: %d\n", k,
result);

return 0;
}
----------------------------------------------------------------------------------------
47. Program to Find the Maximum Sum of a Subarray with Exactly K
Elements
#include <stdio.h>

int maxSumSubarrayWithExactlyKElements(int arr[], int n, int k) {


int maxSum = 0, currentSum = 0;

for (int i = 0; i < k; i++) {


currentSum += arr[i];
}
maxSum = currentSum;

for (int i = k; i < n; i++) {


currentSum += arr[i] - arr[i - k];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}

return maxSum;
}

int main() {
int n, i, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];
P a g e | 38

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

int result = maxSumSubarrayWithExactlyKElements(arr, n, k);


printf("Maximum sum of a subarray with exactly %d elements is: %d\n", k,
result);

return 0;
}
----------------------------------------------------------------------------------------
48. Program to Find the Maximum Sum of a Subarray with All Unique
Elements
#include <stdio.h>
#include <stdbool.h>

int maxSumSubarrayWithUniqueElements(int arr[], int n) {


int maxSum = 0, currentSum = 0;
bool visited[100000] = {false};

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;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
P a g e | 39

scanf("%d", &arr[i]);
}

int result = maxSumSubarrayWithUniqueElements(arr, n);


printf("Maximum sum of a subarray with all unique elements is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
49. Program to Find the Maximum Sum of a Subarray with All Elements
Divisible by K
#include <stdio.h>

int maxSumSubarrayWithDivisibleByK(int arr[], int n, int k) {


int maxSum = 0, currentSum = 0;

for (int i = 0; i < n; i++) {


if (arr[i] % k == 0) {
currentSum += arr[i];
if (currentSum > maxSum) {
maxSum = currentSum;
}
} else {
currentSum = 0;
}
}

return maxSum;
}

int main() {
int n, i, k;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of k: ");


scanf("%d", &k);

int result = maxSumSubarrayWithDivisibleByK(arr, n, k);


printf("Maximum sum of a subarray with all elements divisible by %d is: %d\n", k,
result);

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>

bool isPrime(int num) {


if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int maxSumSubarrayWithAllPrimes(int arr[], int n) {


int maxSum = 0, currentSum = 0;

for (int i = 0; i < n; i++) {


if (isPrime(arr[i])) {
currentSum += arr[i];
if (currentSum > maxSum) {
maxSum = currentSum;
}
} else {
currentSum = 0;
}
}

return maxSum;
}

int main() {
int n, i;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int result = maxSumSubarrayWithAllPrimes(arr, n);


printf("Maximum sum of a subarray with all prime elements is: %d\n", result);

return 0;
}
----------------------------------------------------------------------------------------
P a g e | 41

You might also like