25 Array Problems With Solutions
25 Array Problems With Solutions
500.
WTD: Examine an array expected to contain consecutive integers from 1 to 500. Identify any
integer that is missing from this sequence.
(e.g.: I/P: [1,2,4,5]; O/P: 3)
#include <stdio.h>
// The missing number is the difference between the expected sum and
the actual sum
int missingNumber = expectedSum - actualSum;
return missingNumber;
}
int main() {
int arr[] = {1, 2, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
2) Find the duplicate number on a given integer array.
WTD: Inspect the provided array. Determine if there's any integer that appears more frequently
than it should, signifying a duplicate.
(e.g.: I/P: [3,1,3,4,2]; O/P: 3 )
#include <stdio.h>
int main() {
int arr[] = {3, 1, 3, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
if (duplicate != -1) {
printf("Duplicate Number: %d\n", duplicate);
} else {
printf("No duplicate found.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {34, 15, 88, 2};
int n = sizeof(arr) / sizeof(arr[0]);
#include <stdio.h>
#include <stdlib.h>
free(pairs);
}
int main() {
int arr[] = {2, 4, 3, 5, 6, -2, 4, 7, 8, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int targetSum = 7;
findPairsWithSum(arr, n, targetSum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
free(seen);
return duplicates;
}
int main() {
int arr[] = {4, 3, 2, 7, 8, 2, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int duplicateCount;
free(duplicateNumbers);
return 0;
}
6) Sort an array using the quicksort algorithm.
WTD: Implement the quicksort sorting technique on the provided array to rearrange its
elements in ascending order.
(e.g.: I/P: [64, 34, 25, 12, 22, 11, 90]; O/P: [11, 12, 22, 25, 34, 64, 90] )
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quicksort(arr, 0, n - 1);
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {1, 1, 2, 2, 3, 4, 4};
int n = sizeof(arr) / sizeof(arr[0]);
removeDuplicates(arr, &n);
printf("Array with Duplicates Removed: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 4, 5, 6};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
rotateArray(arr, n, k);
return 0;
}
10) Count occurrences of a number in a sorted array.
WTD: For a given number and a sorted array, iterate through the array to count the number of
times that particular number appears.
(e.g.: I/P: [1, 2, 2, 2, 3], 2; O/P: 3 )
#include <stdio.h>
if (arr[mid] == target) {
result = mid;
high = mid - 1; // Search in the left half
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
if (arr[mid] == target) {
result = mid;
low = mid + 1; // Search in the right half
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
int main() {
int arr[] = {1, 2, 2, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 2;
return 0;
}
11) Find the "Kth" max and min element of an array.
WTD: Sort the array and retrieve the kth largest and kth smallest numbers.
(e.g.: I/P: [7, 10, 4, 3, 20, 15], K=3; O/P: 7 )
#include <stdio.h>
int pivotIndex = i + 1;
quicksort(arr, 0, n - 1);
return arr[n - k];
}
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
return 0;
}
12) Move all zeros to the left of an array while maintaining the
order of other numbers.
WTD: Reorder the array by moving all zero values to the leftmost positions while ensuring the
relative order of the non-zero numbers remains unchanged.
(e.g.: I/P: [1,2,0,4,3,0,5,0]; O/P: [0,0,0,1,2,4,3,5] )
#include <stdio.h>
int main() {
int arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
int n = sizeof(arr) / sizeof(arr[0]);
moveZerosToLeft(arr, n);
return 0;
}
13) Merge two sorted arrays to produce one sorted array.
WTD: Sequentially compare the elements of two sorted arrays, combining them into a single
array that remains sorted.
(e.g.: I/P: [1,3,5], [2,4,6]; O/P: [1,2,3,4,5,6] )
#include <stdio.h>
int main() {
int arr1[] = {1, 3, 5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
return 0;
}
#include <stdio.h>
if (count > n / 2) {
return candidate; // Candidate is the majority element
}
int main() {
int arr[] = {3, 3, 4, 2, 4, 4, 2, 4, 4};
int n = sizeof(arr) / sizeof(arr[0]);
if (majorityElement != -1) {
printf("Majority Element: %d\n", majorityElement);
} else {
printf("No Majority Element Found\n");
}
return 0;
}
15) Find the two repeating elements in a given array.
WTD: Investigate the array and find two numbers that each appear more than once.
(e.g.: I/P: [4, 2, 4, 5, 2, 3, 1]; O/P: [4,2] )
#include <stdio.h>
void printTwoRepeatNumber (int arr [], int size)
{
int i, j;
printf("Repeating elements are ");
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr [i] == arr [j])
{
printf("%d ", arr [i]);
break;
}
}
}
}
int main ()
{
int arr [] = { 4, 2, 4, 5, 2, 3, 1 };
int arr_size = sizeof(arr) / sizeof(arr [0]);
printTwoRepeatNumber (arr, arr_size);
return 0;
}
// Merge two subarrays: one containing positive numbers and the other
containing negative numbers
void merge(int arr[], int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;
int main() {
int arr[] = {-1, 2, -3, 4, 5, 6, -7, 8, 9};
int n = sizeof(arr) / sizeof(arr[0]);
rearrangePosNeg(arr, 0, n - 1);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
return false;
}
int main() {
int arr[] = {4, 2, -3, 1, 6};
int n = sizeof(arr) / sizeof(arr[0]);
if (hasZeroSumSubarray(arr, n)) {
printf("Subarray with zero sum exists\n");
} else {
printf("Subarray with zero sum does not exist\n");
}
return 0;
}
int main() {
int arr[] = {-7, 1, 5, 2, -4, 3, 0};
int n = sizeof(arr) / sizeof(arr[0]);
if (equilibriumIndex != -1) {
printf("Equilibrium Index: %d\n", equilibriumIndex);
} else {
printf("No Equilibrium Index Found\n");
}
return 0;
}
#include <stdio.h>
#include <stdbool.h>
currentLength = j - currentElement;
int main() {
int arr[] = {1, 9, 3, 10, 4, 20, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findLongestConsecutiveSubsequence(arr, n);
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {0, 1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
rearrangeArray(arr, n);
return 0;
}
int main() {
int arr[] = {1, 3, 20, 4, 1, 0};
int n = sizeof(arr) / sizeof(arr[0]);
if (peak != -1) {
printf("Peak Element: %d\n", peak);
} else {
printf("No Peak Element Found\n");
}
return 0;
}
22) Compute the product of an array except self.
WTD: For every index in the array, calculate the product of all numbers except for the number at
that index.
(e.g.: I/P: [1,2,3,4]; O/P: [24,12,8,6])
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
int result[n];
productExceptSelf(arr, n, result);
printf("Result: ");
for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdbool.h>
printf("\n");
}
int main() {
int arr[] = {16, 17, 4, 3, 5, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findLeaders(arr, n);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
return true;
}
int main() {
int arr[] = {9, 7, 5, -3};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 6;
if (canDivide) {
printf("Array can be divided into pairs with sum divisible by %d:
True\n", k);
} else {
printf("Array cannot be divided into pairs with sum divisible by
%d: False\n", k);
}
return 0;
}
#include <stdio.h>
void printSubarrayWithLeastSum (int arr [], int n) {
int min_sum = arr [0]; // initialize minimum sum as first element
int start = 0; // initialize starting index as 0
int end = 0; // initialize ending index as 0
for (int i = 0; i < n; i++) { // loop for each element of the array
int curr_sum = 0; // initialize current sum as 0
for (int j = i; j < n; j++) { // loop for each subarray starting
from i
curr_sum += arr [j]; // add current element to current sum
if (curr_sum < min_sum) { // if current sum is smaller than
minimum sum
min_sum = curr_sum; // update minimum sum
start = i; // update starting index
end = j; // update ending index
}
}
}
printf("The subarray with the least sum is: ");
for (int i = start; i <= end; i++) { // loop for printing the subarray
printf("%d ", arr [i]);
}
printf("\nThe least sum is: %d", min_sum);
}
int main () {
int arr [] = {3, 1, -4, 2, 0};
int n = sizeof(arr) / sizeof(arr [0]);
printSubarrayWithLeastSum (arr, n);
return 0;
}