Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

1.Write a program to reverse an array.

#include<stdio.h>

int main()

int arr[10], i;

printf("Enter any 10 elements: ");

for(i=0; i<10; i++)

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

printf("\nThe array elements in reverse order:\n");

for(i=9; i>=0; i--)

if(i==0)

printf("%d", arr[i]);

else

printf("%d, ", arr[i]);

return 0;

}
2.maximum and minimum element of an array.

#include <stdio.h>

int main()

int a[1000],i,n,min,max;

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

scanf("%d",&n);

printf("Enter elements in array : ");

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

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

min=max=a[0];

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

if(min>a[i])

min=a[i];

if(max<a[i])

max=a[i];

printf("minimum of array is : %d",min);

printf("\nmaximum of array is : %d",max);

return 0;

}
3.Kth smallest element of an array.

#include <stdio.h>

#include <stdlib.h>

int cmpfunc(const void* a, const void* b)

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

int kthSmallest(int arr[], int N, int K)

qsort(arr, N, sizeof(int), cmpfunc);

return arr[K - 1];

int main()

int arr[] = { 12, 3, 5, 7, 19 };

int N = sizeof(arr) / sizeof(arr[0]), K = 2;

printf("K'th smallest element is %d",

kthSmallest(arr, N, K));

return 0;

}
4. Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending
order.
#include <stdio.h>

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

int low = 0, mid = 0, high = n - 1;

while (mid <= high) {

switch (arr[mid]) {

case 0:

arr[low++] = 0;

arr[mid++] = arr[low];

break;

case 1:

mid++;

break;

case 2:

arr[mid] = arr[high];

arr[high--] = 2;

break;

int main() {

int arr[] = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};

int n = sizeof(arr)/sizeof(arr[0]);

sort_012(arr, n);

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

printf("%d ", arr[i]);

return 0;

}
5. An array contains both positive and negative numbers in random order. Rearrange
the array elements so that all negative numbers appear before all positive numbers.
#include <stdio.h>

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

int i = -1;

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

if (arr[j] < 0) {

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int main() {

int arr[] = {-12, 11, -13, -5, 6, -7, 5, -3, -6};

int n = sizeof(arr)/sizeof(arr[0]);

rearrange(arr, n);

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

printf("%d ", arr[i]);

return 0;

}
6. Given two arrays a[] and b[] of size n and m respectively. The task is to find the
number of elements in the union between these two arrays.

Union of the two arrays can be defined as the set containing distinct elements from
both the arrays. If there are repetitions, then only one occurrence of element should
be printed in the union.
#include <stdio.h>

#include <stdlib.h>

int findUnion(int a[], int b[], int n, int m) {

int i = 0, j = 0;

int cnt = 0;

while (i < n && j < m) {

if (a[i] < b[j]) {

printf("%d ", a[i]);

i++;

cnt++;

} else if (b[j] < a[i]) {

printf("%d ", b[j]);

j++;

cnt++;

} else {

printf("%d ", a[i]);

i++;

j++;

cnt++;

while (i < n) {

printf("%d ", a[i]);

i++;
cnt++;

while (j < m) {

printf("%d ", b[j]);

j++;

cnt++;

return cnt;

int main() {

int a[] = {1, 2, 3, 4, 5};

int b[] = {3, 4, 5, 6, 7};

int n = sizeof(a) / sizeof(a[0]);

int m = sizeof(b) / sizeof(b[0]);

int cnt = findUnion(a, b, n, m);

printf("\nUnion contains %d elements", cnt);

return 0;

}
7. Given an array, rotate the array by one position in clock-wise direction.

#include <stdio.h>

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

int temp = arr[n-1];

int i;

for(i=n-1;i>0;i--){

arr[i]=arr[i-1];

arr[0] = temp;

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr)/sizeof(arr[0]);

rotate(arr, n);

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

printf("%d ", arr[i]);

return 0;

}
8. Given an array Arr[] of N integers. Find the contiguous sub-array(containing at
least one number) which has the maximum sum and return its sum.
#include <stdio.h>

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

int max_sum = 0, current_sum = 0;

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

current_sum += arr[i];

if (current_sum < 0) {

current_sum = 0;

if (current_sum > max_sum) {

max_sum = current_sum;

return max_sum;

int main() {

int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};

int n = sizeof(arr)/sizeof(arr[0]);

int max_sum = max_sum_subarray(arr, n);

printf("Maximum sum of subarray is %d", max_sum);

return 0;

}
8. Given an array arr[] denoting heights of N towers and a positive integer K.

For each tower, you must perform exactly one of the following operations exactly


once.

 Increase the height of the tower by K


 Decrease the height of the tower by K

Find out the minimum possible difference between the height of the shortest and
tallest towers after you have modified each tower.
#include <stdio.h>

#include <limits.h>

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

int min_height = INT_MAX, max_height = INT_MIN;

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

min_height = (arr[i] < min_height) ? arr[i] : min_height;

max_height = (arr[i] > max_height) ? arr[i] : max_height;

int difference = max_height - min_height;

int mod = difference % (2 * k);

return mod ? difference - mod + 2 * k : difference;

int main() {

int arr[] = {1, 15, 10};

int n = sizeof(arr)/sizeof(arr[0]);

int k = 6;

int min_diff = min_difference(arr, n, k);

printf("Minimum possible difference is %d", min_diff);

return 0;

}
9.Given an array of N integers arr[] where each element represents the max length
of the jump that can be made forward from that element. Find the minimum
number of jumps to reach the end of the array (starting from the first element). If
an element is 0, then you cannot move through that element.

Note: Return -1 if you can't reach the end of the array.


#include <stdio.h>

#include <limits.h>

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

int jumps[n];

int i, j;

// initialize jumps[]

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

jumps[i] = INT_MAX;

// fill jumps[]

jumps[0] = 0;

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

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

if (i <= j + arr[j] && jumps[j] != INT_MAX) {

jumps[i] = (jumps[i] < jumps[j] + 1)? jumps[i]: jumps[j] + 1;

break;

return jumps[n-1];

int main() {
int arr[] = {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Minimum number of jumps: %d\n", minJumps(arr, n));

return 0;

}
9. Given an array of integers nums containing n + 1 integers where each integer is in the range [1,
n] inclusive.

There is only one repeated number in nums, return this  repeated  number.

You must solve the problem without modifying the array nums and uses only constant extra
space.

#include <stdio.h>

int findDuplicate(int* nums, int numsSize) {

int slow = nums[0];

int fast = nums[0];

do {

slow = nums[slow];

fast = nums[nums[fast]];

} while (slow != fast);

slow = nums[0];

while (slow != fast) {

slow = nums[slow];

fast = nums[fast];

return slow;

int main() {

int nums[] = {1, 3, 4, 2, 2};

int numsSize = sizeof(nums) / sizeof(nums[0]);

int duplicate = findDuplicate(nums, numsSize);

printf("The duplicate element is: %d\n", duplicate);

return 0;

}
11. Given an array Arr[] of N integers. Find the contiguous sub-array(containing at
least one number) which has the maximum sum and return its sum.
#include <stdio.h>

int max(int a, int b) {

return (a > b)? a : b;

int maxSubArraySum(int a[], int size) {

int max_so_far = a[0], max_ending_here = a[0];

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

max_ending_here = max(a[i], max_ending_here + a[i]);

max_so_far = max(max_so_far, max_ending_here);

return max_so_far;

int main() {

int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};

int n = sizeof(a)/sizeof(a[0]);

int max_sum = maxSubArraySum(a, n);

printf("Maximum sum of contiguous sub-array: %d\n", max_sum);

return 0;

}
12. Given an array of intervals where intervals[i] = [start , end ], merge all overlapping intervals, and
i i

return an array of the non-overlapping intervals that cover all the intervals in the input.

#include <stdio.h>

#include <stdlib.h>

int cmp(const void* a, const void* b) {

int* a1 = (int*)a;

int* b1 = (int*)b;

return a1[0] - b1[0];

int max(int a, int b) {

return (a > b)? a : b;

int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int**
returnColumnSizes) {

if (intervalsSize <= 0) return NULL;

qsort(intervals, intervalsSize, sizeof(int*), cmp);

*returnSize = 1;

int** res = (int**)malloc(sizeof(int*));

res[0] = (int*)malloc(sizeof(int) * 2);

res[0][0] = intervals[0][0];

res[0][1] = intervals[0][1];

*returnColumnSizes = (int*)malloc(sizeof(int));

(*returnColumnSizes)[0] = 2;

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

if (intervals[i][0] <= res[*returnSize - 1][1]) {

res[*returnSize - 1][1] = max(intervals[i][1], res[*returnSize - 1][1]);

} else {

(*returnSize)++;
res = (int**)realloc(res, sizeof(int*) * (*returnSize));

res[*returnSize - 1] = (int*)malloc(sizeof(int) * 2);

res[*returnSize - 1][0] = intervals[i][0];

res[*returnSize - 1][1] = intervals[i][1];

*returnColumnSizes = (int*)realloc(*returnColumnSizes, sizeof(int) * (*returnSize));

(*returnColumnSizes)[*returnSize - 1] = 2;

return res;

}
13. Given an integer N, find its factorial.
#include<stdio.h>

unsigned long long factorial(int n) {

unsigned long long result = 1;

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

result *= i;

return result;

int main() {

int n = 10;

printf("The factorial of %d is %llu\n", n, factorial(n));

return 0;

14. Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m. Task is to check


whether a2[] is a subset of a1[] or not. Both the arrays can be sorted or unsorted. 
#include <stdio.h>

int isSubset(int a1[], int a2[], int n, int m) {

int i, j;

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

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

if (a2[i] == a1[j]) break;

if (j == n) return 0;

return 1;

}
int main() {

int a1[] = {1, 2, 3, 4, 5};

int a2[] = {2, 4};

int n = sizeof(a1)/sizeof(a1[0]);

int m = sizeof(a2)/sizeof(a2[0]);

if (isSubset(a1, a2, n, m))

printf("a2[] is a subset of a1[]\n");

else

printf("a2[] is not a subset of a1[]\n");

return 0;

15.Given an array A[ ] of positive integers of size N, where each value represents
the number of chocolates in a packet. Each packet can have a variable number of
chocolates. There are M students, the task is to distribute chocolate packets
among M students such that :
1. Each student gets exactly one packet.
2. The difference between maximum number of chocolates given to a student and
minimum number of chocolates given to a student is minimum.

#include <stdio.h>

#include <stdlib.h>

#include<limits.h>

int compare (const void * a, const void * b) {

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

int minDifference(int a[], int n, int m) {

qsort(a, n, sizeof(int), compare); //sort the array

int ans = INT_MAX;

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

int diff = a[i + m - 1] - a[i];


ans = (ans, diff);

return ans;

int main() {

int a[] = {7, 3, 2, 4, 9, 12, 56};

int n = sizeof(a)/sizeof(a[0]);

int m = 3;

printf("Minimum difference: %d\n",minDifference(a, n, m));

return 0;

}
16.Given a Integer array A[] of n elements. Your task is to complete the
function PalinArray. Which will return 1 if all the elements of the Array are
palindrome otherwise it will return 0.

#include<stdio.h>

#include <stdbool.h>

bool isPalindrome(int x) {

int tmp = x, rev = 0;

while(x > 0) {

rev = rev * 10 + x % 10;

x /= 10;

return tmp == rev;

int PalinArray(int A[], int n) {

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

if (!isPalindrome(A[i])) {

return 0;

return 1;

You might also like