Array Solution
Array Solution
#include<stdio.h>
int main()
int arr[10], i;
scanf("%d", &arr[i]);
if(i==0)
printf("%d", arr[i]);
else
return 0;
}
2.maximum and minimum element of an array.
#include <stdio.h>
int main()
int a[1000],i,n,min,max;
scanf("%d",&n);
scanf("%d",&a[i]);
min=max=a[0];
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
return 0;
}
3.Kth smallest element of an array.
#include <stdio.h>
#include <stdlib.h>
int main()
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>
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 n = sizeof(arr)/sizeof(arr[0]);
sort_012(arr, n);
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>
int i = -1;
if (arr[j] < 0) {
i++;
arr[i] = arr[j];
arr[j] = temp;
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
rearrange(arr, n);
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 i = 0, j = 0;
int cnt = 0;
i++;
cnt++;
j++;
cnt++;
} else {
i++;
j++;
cnt++;
while (i < n) {
i++;
cnt++;
while (j < m) {
j++;
cnt++;
return cnt;
int main() {
return 0;
}
7. Given an array, rotate the array by one position in clock-wise direction.
#include <stdio.h>
int i;
for(i=n-1;i>0;i--){
arr[i]=arr[i-1];
arr[0] = temp;
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
rotate(arr, n);
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>
current_sum += arr[i];
if (current_sum < 0) {
current_sum = 0;
max_sum = current_sum;
return max_sum;
int main() {
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
8. Given an array arr[] denoting heights of N towers and a positive integer 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 main() {
int n = sizeof(arr)/sizeof(arr[0]);
int k = 6;
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.
#include <limits.h>
int jumps[n];
int i, j;
// initialize jumps[]
jumps[i] = INT_MAX;
// fill jumps[]
jumps[0] = 0;
break;
return jumps[n-1];
int main() {
int arr[] = {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};
return 0;
}
9. Given an array of integers nums containing n + 1 integers where each integer is in the range [1,
n] inclusive.
You must solve the problem without modifying the array nums and uses only constant extra
space.
#include <stdio.h>
do {
slow = nums[slow];
fast = nums[nums[fast]];
slow = nums[0];
slow = nums[slow];
fast = nums[fast];
return slow;
int main() {
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>
return max_so_far;
int main() {
int n = sizeof(a)/sizeof(a[0]);
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* a1 = (int*)a;
int* b1 = (int*)b;
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int**
returnColumnSizes) {
*returnSize = 1;
res[0][0] = intervals[0][0];
res[0][1] = intervals[0][1];
*returnColumnSizes = (int*)malloc(sizeof(int));
(*returnColumnSizes)[0] = 2;
} else {
(*returnSize)++;
res = (int**)realloc(res, sizeof(int*) * (*returnSize));
(*returnColumnSizes)[*returnSize - 1] = 2;
return res;
}
13. Given an integer N, find its factorial.
#include<stdio.h>
result *= i;
return result;
int main() {
int n = 10;
return 0;
int i, j;
if (j == n) return 0;
return 1;
}
int main() {
int n = sizeof(a1)/sizeof(a1[0]);
int m = sizeof(a2)/sizeof(a2[0]);
else
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>
return ans;
int main() {
int n = sizeof(a)/sizeof(a[0]);
int m = 3;
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) {
while(x > 0) {
x /= 10;
if (!isPalindrome(A[i])) {
return 0;
return 1;