0% found this document useful (0 votes)
189 views114 pages

Arrays Reference Material

Given an unsorted array of non-negative integers, the problem is to find a continuous sub-array which adds to a given sum. The input contains the array size, array elements, and the sum. The output prints the starting and ending indexes of the sub-array if a sub-array is found, or indicates no sub-array exists. The solution uses two pointers to iterate through all possible sub-arrays, calculating the current sum and checking if it equals the given sum.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views114 pages

Arrays Reference Material

Given an unsorted array of non-negative integers, the problem is to find a continuous sub-array which adds to a given sum. The input contains the array size, array elements, and the sum. The output prints the starting and ending indexes of the sub-array if a sub-array is found, or indicates no sub-array exists. The solution uses two pointers to iterate through all possible sub-arrays, calculating the current sum and checking if it equals the given sum.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 114

Problem :1

Language:C
Problem Statement:
Given an unsorted array a of size N of non-negative integers,
find a continuous sub-array which adds to a given number sum.

Input Format:
The first line contains an integer, denoting the size of the array.
The second line contains integers denoting the elements
of the array.
The last line contains an integer, denoting the sum.

Constraints
1<= n<=100
1<=arr<= 1000, where arr is the ith element of the array.
1<= n<=100000

Output Format:
The output line contains integers denoting the indexes.
TESTCASE 1:
Input:
7
[1, 4, 0, 0, 3, 10, 5]
sum = 7
Output:
Sum found between indexes 1 and 4

TESTCASE 2:
Input:
2
[1, 4]
sum = 0
Output:
No subarray found
Solution if (curr > sum || j == n)
#include<stdio.h> break;
int subarr(int arr[], int n, int sum) curr_sum = curr + arr[j];
}
{
}
int curr, i, j;
for (i = 0; i < n; i++) printf("No subarray found");
return 0;
{
}
curr = arr[i];
for (j = i+1; j <= n; j++) int main()
{
{
int size;
if (curr == sum) scanf("%d",&size);
{ int a[size],sum,i;
printf ("Sum found for(i=0;i<size;i++)
between indexes %d and %d", i, j- scanf("%d",&a[i]);
1); scanf("%d",&sum);
subarr(a, size, sum);
return 1;
return 0;
} }
Constraints
Problem:2 1<= n<=100
1<=arr<= 1000, where arr is the ith
Language:C element of the array.
Module : Base Module
Output Format:
Problem Statement:
The output line contains integer denoting
Given an array C of size N-1 and the missing element in the array
given that there are numbers
from
TESTCASE 1:
1 to N with one element Input:
missing, the missing number is to 6
be found. [1,2,3,4,6,7]
Output:
Input Format: 5
The first line contains an integer, TESTCASE 2:
denoting the size of the array.
Input:
The second line contains integers 3
denoting the elements [1,2,4]
of the array. Output:
3
Solution

#include<stdio.h>
int getMissingNo (int a[], int int size;
n) scanf("%d",&size);
{ int a[size],i;
for(i=0;i<size;i++)
int i, total;
scanf("%d",&a[i]);
total = (n+1)*(n+2)/2; int miss =
for ( i = 0; i< n; i++) getMissingNo(a,size);
total -= a[i]; printf("%d", miss);
return 0;
return total;
}
}
int main()
{
Problem 3:
Language:C
Module : Base Module
Output Format:
Problem Statement: The output line contains integers denoting
Given an array A of the elements
size N containing 0s, 1s, and 2s of the array in ascending order.
you need to sort the array in
ascending order. TESTCASE 1:
Input:
6
Input Format: [1 0 2 0 1 2]
The first line contains an integer, Output:
denoting the size of the array. [0 0 1 1 2 2 n]
The second line contains integers
denoting the elements TESTCASE 2:
Input:
of the array. 3
Constraints [2 1 0]
1<= n<=100 Output:
[0 1 2 n]
1<=arr<= 1000, where arr is the ith
element of the array.
Solution
#include<stdio.h>
break;
void swap(int *a, int *b);
case 2:
void sort(int a[], int arr_size) swap(&a[mid], &a[hi--]);
{ break;
int lo = 0; }
}
int hi = arr_size - 1; }
int mid = 0;
while (mid <= hi) void swap(int *a, int *b)
{
{ int temp = *a;
switch (a[mid]) *a = *b;
{ *b = temp;
}
case 0:
swap(&a[lo++], &a[mid++]); void printArray(int arr[], int arr_size)
break; {
case 1:
int i;
mid++;
for (i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int size;
scanf("%d",&size);
int a[size],i;
for(i=0;i<size;i++)
scanf("%d",&a[i]);
sort(a,size);
printArray(a,size);
return 0;
}
Problem 3:
Language:C
Module : Base Module
1<=arr<= 1000, where arr is the ith
Problem Statement:
element of the array.
Given an array A of N positive numbers.
The task is to find the position Output Format:
where equilibrium first occurs in the The output line contains integer
array. Equilibrium position in an array denoting an equilibrium
is a position such that the sum of
elements before it is equal to the sum index (if any) or -1 (if no equilibrium
of elements after it. indexes exist).

TESTCASE 1:
Input Format:
Input:
The first line contains an integer, 7
denoting the size of the array.
[-7, 1, 5, 2, -4, 3, 0]
The second line contains integers Output:
denoting the elements 3
of the array.

Constraints
1<= n<=100
Solution
#include <stdio.h>
int equilibrium(int arr[], int n)
{ }
int i, j;
int main()
int leftsum, rightsum; {
for (i = 0; i < n; ++i) { // int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
// int arr_size = sizeof(arr) /
leftsum = 0;
sizeof(arr[0]);
for (j = 0; j < i; j++) int size;
leftsum += arr[j]; scanf("%d",&size);
int arr[size],sum,i;
rightsum = 0;
for(i=0;i<size;i++)
for (j = i + 1; j < n; j++) scanf("%d",&arr[i]);
rightsum += arr[j];
if (leftsum == rightsum) printf("%d", equilibrium(arr, size));

return i; return 0;
} }
return -1;
Problem:5
Language:C
1<=arr<= 1000, where arr is the ith
Module : Base Module
element of the array.
Problem Statement:
Given an array A of N positive Output Format:
integers. Find The output line contains integer denoting
the sum of maximum sum sum of maximum sum.
increasing subsequence of the
given array. TESTCASE 1:
Input:
7
Input Format: {1, 101, 2, 3, 100, 4, 5}
The first line contains an integer, Output:
denoting the size of the array. 106
The second line contains integers
denoting the elements TESTCASE 2:
Input:
of the array.
4
{10, 5, 4, 3}
Constraints output:
10
1<= n<=100
Solution
#include<stdio.h>
return max;
int maxSumIS(int arr[], int n) }
{
int i, j, max = 0; int main()
{
int msis[n];
int size;
for ( i = 0; i < n; i++ ) scanf("%d",&size);
msis[i] = arr[i]; int a[size],sum,i;
for(i=0;i<size;i++)
for ( i = 1; i < n; i++ )
scanf("%d",&a[i]);
for ( j = 0; j < i; j++ ) printf("Sum of maximum sum
if (arr[i] > arr[j] && increasing subsequence is %d\n",
maxSumIS( a, size ) );
msis[i] < msis[j] + arr[i])
return 0;
msis[i] = msis[j] + arr[i]; }
for ( i = 0; i < n; i++ )
if ( max < msis[i] )
max = msis[i];
Problem:6 Constraints
Language:C 1<= n<=100
-1000<=arr[i]<= 1000, where arr is the ith
Module : Base Module
element of the array.
Problem Statement:
Given an array of positive integers. Output Format:
Your task is to find the leaders The output line prints all the leaders in
in the array.An element is leader if it the array.
is greater than all the elements to
its TESTCASE 1:
Input:
right side.
6
And the rightmost element is always 674352
a leader. Output:
257
Input Format:
TESTCASE 2:
The first line contains an integer, Input:
denoting the size of the array.
5
The second line contains integers 26384
denoting the elements Output:
of the array. 48
Solution
#include<stdio.h>
void printleaders(int arr[], int n)
{ int main()
int max = arr[n-1]; {
int n;
int i;
scanf("%d",&n);
printf("%d ",max); int a[n],i;
for(i= n-2; i>=0; i--) for(i=0;i<n;i++)
{
{
scanf("%d",&a[i]);
if(max<arr[i]) }
{ printleaders(a,n);
return 0;
printf("%d
",arr[i]); }
max = arr[i];
}
}
}
Problem:7
Language:C
Module : Base Module 1<=arr<= 1000, where arr is the ith element of
the array.
Problem Statement: 1<= n <= 100
Given an array A and an integer K.
Find the maximum for each and Output Format:
The output line contains integers denoting
every contiguous subarray of size K. elements in an array contiguous
subarray of size K
TESTCASE 1:
Input Format: Input :
The first line contains an integer, 10
denoting the size of the array. {8, 5, 10, 7, 9, 4, 15, 12, 90, 13}
4
The second line contains integers
Output :
denoting the elements of the
10 10 10 15 15 90 90
array.
TESTCASE 2:
The last line contains an integer, Input :
denoting the contiguous subarray 6
of size K {1, 2, 3, 1, 4, 5, 2, 3, 6}
3
Constraints
Output :
1<= n<=100 3345556
Solution
#include <stdio.h>

void printKMax(int arr[], int n, int k)


{
int j, max; int main()
{
int size;
for (int i = 0; i <= n - k; i++) { scanf("%d",&size);
max = arr[i]; int a[size],i,k;
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for (j = 1; j < k; j++) { scanf("%d", &k);
if (arr[i + j] > max) printKMax(a, size, k);
return 0;
max = arr[i + j];
}
}
printf("%d ", max);
}
}
Prorgram : 8
Language :C
Module :Base Module
Problem Statement: Given a string S consisting of lowercase Latin Letters. Find
the first non repeating character in S.

Input Format : Take input string as S

Output Format : Print the first non repeating character in S if there, otherwise
print message
"Either all characters are repeating or string is empty"

Sample Input1:
hihellohi
Sample Output1:
First non-repeating character is : e

Sample Input2:
hhhiii
Sample Output2:
Either all characters are repeating or string is empty
Solution
#include<stdlib.h>
/* The function returns index of first non-
#include<stdio.h>
repeating
#define NO_OF_CHARS 256 character in a string. If all characters are
/* Returns an array of size 256 repeating
containg count then returns -1 */
of characters in the passed char int firstNonRepeating(char *str)
array */ {
int *count = getCharCountArray(str);
int *getCharCountArray(char *str)
int index = -1, i;
{
int *count = (int for (i = 0; *(str+i); i++)
*)calloc(sizeof(int), {
NO_OF_CHARS); if (count[*(str+i)] == 1)
int i; {
index = i;
for (i = 0; *(str+i); i++) break;
count[*(str+i)]++; }
return count; }
}
free(count); // To avoid memory leak
return index;
}
int main()
{
char str[100];
scanf("%s",str);
int index = firstNonRepeating(str);
if (index == -1)
printf("Either all characters are repeating or string is empty");
else
printf("First non-repeating character is : %c", str[index]);
getchar();
return 0;
}
Prorgram :9
Language :C
Module :Base Module
Problem Statement: Given two
strings s1 and s2. Modify string s1 Sample Input1:
such that all the common characters
aacdb
of s1 and s2 is to be removed and the
uncommon characters of s1 and s2 is gafd
to be concatenated Sample Output1:
cbgf

Input Format : Sample Input2:


First line contain s1 string abc
abc
Second line contain s2 string
Sample Output2:
-1
Output Format :
Print string of all uncommon characters
otherwise print -1.
}
Solution if(q==0)
#include<stdio.h> {
#include<string.h> printf("%c",s[i]);
o+=1;
int print(char s[10000],char v[10000])
}
{ } return o;
int o=0; }
int i,j,r,q;
int main() {
r=strlen(v); char str1[100],str2[100];
for(i=0;i<strlen(s);i++) int x,y;
scanf("%s",str1);
{q=0;
scanf("%s",str2);
for(j=0;j<r;j++) x=print(str1,str2);
{ y=print(str2,str1);
if(s[i]==v[j]) if(x+y==0){
printf("-1");
{ }
q=1; printf("\n");
break;
return 0;
} }
Prorgram :10
Language :C
Module :Base Module
Problem Statement: You are given
two arrays, A and B, of equal Output Format : Print integer.
size N.
The task is to find the minimum Constratins:
value of A[0] * B[0] + A[1] * B[1] 1<=N<=50
+…+ A[N-1] * B[N-1], where
shuffling of elements of arrays A Sample Input:
and B is allowed. 3
322
Input Format : 765
Sample Output:
First line contains N as input of type 41
integer.
Second line contain N i.e. is no of
elements for array A.
Third line also contain N i.e. is no of
elements for array B.
Solution return result;
#include<stdio.h>
}
#include<stdlib.h>
void sort(const void *a, const void void main() {
*b) { int N,*arr1,result;
return *((int*)a) > *((int*)b); scanf("%d",&N);
if(N>=1 && N<=50) {
}
int arr1[N],arr2[N];
// Returns minimum sum of for(int i=0;i<N;i++){
product of two arrays scanf("%d",&arr1[i]);
// with permutations allowed }
for(int i=0;i<N;i++){
int minValue(int A[], int B[], int n)
scanf("%d",&arr2[i]);
{ }
// Multiplying minimum value of qsort(arr1, N, sizeof(int), sort);
A qsort(arr2,N,sizeof(int),sort);
// and maximum value of B result =minValue(arr1,arr2,N);
printf("%d",result);
int result = 0;
}
for (int i = 0; i < n; i++) }
result += (A[i] * B[n - i - 1]);
Prorgram :11

Language :C
Module :Base Module Constraints:
1<=N<=20
Problem Statement:
Given a Integer array A[] of n Sample Input1:
elements. Your task is to 3
complete the function Palin Array. 121 131 444
Which will return 1 if all the Sample Output1:
elements of the Array are Array is a PalinArray
palindrome otherwise it will
return 0. Sample Input2:
4
121 333 456 787
Input Format:
Sample Output2:
First line contains N of type integer. Array is not a PalinArray
Second line contains N no of
elements.
Solution
#include<stdio.h>
// Function to check if palindrome or
// Function to check not
// if an array is PalinArray or not int isPalindrome(int N) {
int res=0,rem=0,sum=0;
int isPalinArray(int arr[], int n) {
int temp =N;
// Traversing each element of while(N!=0){
the array rem = N%10;
// and check if it is palindrome sum = sum*10+rem;
or not N= N/10;
for (int i = 0; i < n; i++) { }
if(sum==temp){
int ans = isPalindrome(arr[i]);
res = 1;
if (ans == 0) { }
return 0; return res;
}
}
} void main() {
return 1; int N,*arr1;
} scanf("%d",&N);
if(N>=2 && N<=100) {
int arr[N];
for(int i=0;i<N;i++){
scanf("%d",&arr[i]);
}
int res = isPalinArray(arr, N);
if (res == 1)
printf("Array is a PalinArray");
else
printf("Array is not a PalinArray");
}
}
Prorgram :12
Language :C
Module :Base Module
Problem Statement:Given an array A of positive integers. Count number of smaller
elements on right side of each array.

Input Format:
First line contains N as integer.
Second line contains N no of elements of type integer.

Output Format:

Constratins:
2<=N<=100

Sample Input:
5
54321
Sample Output:
43210
}
Solution return arr1;
}
#include<stdio.h>
void main() {
#include<stdlib.h> int N,*arr1;
int i=0; scanf("%d",&N);
if(N>=2 && N<=100) {
int* createLowerArray (int *arr[],int
n) { int arr[N];
for(int i=0;i<N;i++){
int *arr1 = scanf("%d",&arr[i]);
(int*)malloc(sizeof(int)*n); }
for(int i=0;i<n;i++){ arr1=createLowerArray(arr,N);
arr1[i]=0; for(int j=0;j<=i-1;j++){
printf("%d ",arr1[j]);
}
}
for(;i<n;i++){ }
for(int j=i+1;j<n;j++){
}
if(arr[j]<arr[i]){
arr1[i]++; or
}
}
#include <stdio.h>
#include <stdlib.h>
int* f(int *a, int n) {
int *p = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
int count = 0; for (int i = 0; i < n; i++)
for (int j = i + 1; a[j] < a[i]; j++) scanf("%d", &a[i]);
count++; int *p = f(a, n);
for (int i = 0; i < n; i++)
p[i] = count; printf("%d ", p[i]);
} printf("\n");
return p; }

}
int main() {
int n;
scanf("%d", &n);
int a[n];
Prorgram :13
Language :C
Module :Base Module
Problem Statement:
Given an array Arr[] of N integers. Write
a program to find out number of pairs
in array whose XOR is odd. Constratins:
2<=N<=100

Input Format: Sample Input:


First line contains N as integer. 5
Second line contains N no of elements of 12345
type integer. Sample Output:
6

OutputFormat:
Print the integer i.e is number of pairs in
array whose XOR is odd .
Solution
#include<stdio.h>
int countXorPairElement(int arr[],
int n) { return count;
}
// To store count of XOR pair void main() {
int count = 0; int N;
scanf("%d",&N);
if(N>=2 && N<=100) {
for (int i = 0; i < n; i++) { int arr[N];
for (int j = i + 1; j < n; j++) for(int i=0;i<N;i++){
scanf("%d",&arr[i]);
}
// If XOR is odd increase printf("%d",countXorPairElement(ar
count
r,N));
if ((arr[i] ^ arr[j]) % 2==1) }
count++;
}
}
Prorgram:14
Language:C
Module : Base Module
Problem Statement:
Given a sorted array A, size N, of integers; every element appears twice except for one. Find
that element in linear time complexity and without using extra memory.

Input Format:
First line contains N as integer.
Second line contains N no of elements of type integer.

OutputFormat:
Print the integer(which element has not appear twice).
Constratins:
2<=N<=100

Sample Input:
7
2354534
Sample Output:
2
Solution
#include <stdlib.h>
#include<stdio.h> void main() {
int N;
void f(const void *a, const void *b) { scanf("%d",&N);
if(N>=2 && N<=100) {
return *((int*)a) > *((int*)b); int arr[N];
}
int findSingleAppersElement(int for(int i=0;i<N;i++){
ar[], int size) { scanf("%d",&arr[i]);
}
// Do XOR of all elements and
qsort(arr, N, sizeof(int), f);
return
printf("%d",findSingleAppersElement(
int res = ar[0]; arr,N));
for (int i = 1; i < size; i++) }
res = res ^ ar[i];
}
return res;
}
Problem : 15

Language : C
Module : Base Module
Problem Statement:
Given an array arr[] of positive integers of size N.
Reverse every sub-array of K consecutive group of elements

Input:
6
123456
3

Output:
321654
Solution
#include <stdio.h>
void swap(int *p, int *q) {
int temp = *p; int a[n];
*p = *q; for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
*q = temp;
int k;
} scanf("%d", &k);
void f(int a[], int n) { for (int i = 0; i < n - 1; i += k)
f(a + i, k);
int i, j;
for (int i = 0; i < n; i++)
for (i = 0, j = n - 1; i < j; i++, j--) printf("%d ", a[i]);
swap(&a[i], &a[j]); printf("\n");
} }

int main() {
int n;
scanf("%d", &n);
Problem : 16

Language : C
Module : Base Module
Problem Statement:
Given an unsorted array of size N. Find the first element in
array
such that all of its left elements are smaller and all right
elements to it are greater than it.

Input:
5 1 4 3 6 8 10 7 9
Ouput:
6
Solution
#include <stdio.h>
#include <values.h> for (int i=n-1; i>=0; i--) {
if (leftMax[i] < arr[i] &&
int max(int x, int y) {
rightMin > arr[i])
return x > y? x: y; return i;
}
rightMin =
int min(int x, int y) {
min(rightMin, arr[i]);
return x < y? x: y; }
} return -1;
}
int f(int arr[], int n) {
int main() {
int leftMax[n]; int n;
leftMax[0] = INT_MIN; scanf("%d", &n);
for (int i = 1; i < n; i++) int a[n];
for (int i = 0; i < n; i++)
leftMax[i] = max(leftMax[i- scanf("%d", &a[i]);
1], arr[i-1]); printf("%d\n", a[f(a, n)]);
int rightMin = INT_MAX; }
Problem : 17

Language : C
Module : Base Module
Problem Statement:
Given a sorted array A, size N, of integers; every element
appears twice except for one. Find that element in linear
time complexity and without using extra memory

Input:
7354534
Ouput:
7
Solution

#include <stdio.h>
#include <stdlib.h>
int f(const void *a, const void *b) { scanf("%d", &a[i]);
g(a, n);
return *((int*)a) > *((int*)b); for (int i = 0; i < n; i++)
} printf("%d ", a[i]);
int g(int a[], int n) { printf("\n");
for (int i = 0; i < n; i += 2)
qsort(a, n, sizeof(int), f); if (a[i] != a[i+1]) {
} printf("%d\n", a[i]);
int main() { break;
}
int n;
}
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
Problem : 18
Language : C
Module : Base Module
Problem Statement:
Given a matrix mat[][] of size M*N. Traverse and print the
matrix in spiral form.

Input:
22
12
34

Output:
1243
Solution for (i = l; i < n; ++i)
#include <stdio.h> {
printf("%d ",
a[k][i]);
void spiralPrint(int m, int n, int }
a[][50]) k++;
{
/* Print the last column
int i, k = 0, l = 0;
from the remaining columns */
/* k - starting row index for (i = k; i < m; ++i)
m - ending row index {
printf("%d ",
l - starting column index
a[i][n-1]);
n - ending column index }
i - iterator n--;
*/
/* Print the last row
while (k < m && l < n) from the remaining rows */
{ if ( k < m)
{
/* Print the first row from
the remaining rows */
for (i = n-1; i >= l; --i)
{
}
}
printf("%d ", a[m-1][i]);
}
}
m--; int main() {
} int a[50][50];
int R,C;
/* Print the first column from the scanf("%d %d", &R, &C);
remaining columns */ for (int i = 0; i < R; i++)
if (l < n) for (int j = 0; j < C; j++)
{ scanf("%d", &a[i][j]);
spiralPrint(R, C, a);
for (i = m-1; i >= k;
printf("\n");
--i)
}
{

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


}
l++;
Problem : 19
Language : C
Module : Base Module
Problem Statement:
Given three increasingly sorted arrays A, B, C of sizes N1, N2, and
N3
respectively, you need to print all common elements in these
arrays.
Input:
10 10 10
3 4 4 7 7 8 9 11 16 19
2 3 4 4 4 4 4 15 15 20
1 2 7 9 10 18 19 20 20 20
Output:
10 15 20
Solution
#include <stdio.h>
#include <stdlib.h>
int main()
{
//code
long long int i=0, j=0, k=0, l=0, n=0, len1=0, len2=0, len3=0, flag=0;
scanf("%lld\n",&n);
for(i=0;i<n;i++) {
scanf("%lld %lld %lld\n", &len1, &len2, &len3);
// printf("%lld %lld %lld\n", len1, len2, len3);
long long int *arr1=(long long int*)malloc(sizeof(long long int)*len1);
long long int *arr2=(long long int*)malloc(sizeof(long long int)*len2);
long long int *arr3=(long long int*)malloc(sizeof(long long int)*len3);
long long int prev=0;
for(j=0;j<len1;j++) {
scanf("%lld\n",&arr1[j]);
}
prev=arr1[j];
for(k=0;k<len2;k++) { }
scanf("%lld\n",&arr2[k]); else {
} if(prev!=arr1[j]) {
printf("%lld
for(l=0;l<len3;l++) { ",arr1[j]);
scanf("%lld\n",&arr3[l]); prev=arr1[j];
} }
}
j=0, k=0, l=0, flag=0; j++;
for(;;) { k++;
if(j>=len1 || k>=len2 || l++;
l>=len3) { flag=1;
}
break;
else {
} if(arr1[j]<arr2[k]) {
else { ++j;
}
if(arr1[j]==arr2[k] &&
arr2[k]==arr3[l]) { if(arr2[k]<arr3[l]) {
++k;
if(prev==0) { }
printf("%lld ",arr1[j]);
if(arr3[l]<arr1[j]) {
++l;
}
if(arr1[j]<arr3[l]) { if(!flag) {
printf("-1");
++j;
}
} printf("\n");
if(arr2[k]<arr1[j]) { free(arr1);
free(arr2);
++k;
free(arr3);
} }
if(arr3[l]<arr2[k]) { return 0;
++l; }

}
}
}
}
Problem : 20

Language : C
Module : Base Module
Problem Statement:
You are given an array arr[] of N integers including 0. The task
is to find the smallest positive number missing from the
array.

Input :
8
2 3 7 6 8 -1 -10 15
Output:
1
}
Solution return j;
#include <stdio.h> }
int findMissingPositive(int arr[], int size) {
#include <stdlib.h>
int i;
void swap(int *a, int *b) { for(i = 0; i < size; i++) {
int temp; if(abs(arr[i]) - 1 < size && arr[
abs(arr[i]) - 1] > 0)
temp = *a;
arr[ abs(arr[i]) - 1] = -arr[ abs(arr[i]) -
*a = *b; 1];
*b = temp; }
} for(i = 0; i < size; i++)
if (arr[i] > 0)
int segregate (int arr[], int size) { return i+1;
int j = 0, i; return size+1;
for(i = 0; i < size; i++) { }
int findMissing(int arr[], int size) {
if (arr[i] <= 0) { int shift = segregate (arr, size);
swap(&arr[i], &arr[j]); return findMissingPositive(arr+shift,
j++; size-shift);
}
}
int main() {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
int missing = findMissing(a, n);
printf("%d\n", missing);
}
Problem : 21
Module: Base program
Language: C
Problem statement
Given a String of length S, reverse the whole string without
reversing
the individual words in it. Words are separated by dots
Input:
all.the.best.
Output:
lla.eht.tseb.
Solution
#include <stdio.h>
#include <string.h>
char* f(char s[]) {
int i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
char t = s[i]; s[i] = s[j]; s[j] = t;
}
return s;
}
int main() {
char s[] = "all.the.best.";
char *p = strtok(s, ".");
printf("%s.", f(p));
while((p = strtok(NULL, "."))) {
printf("%s.", f(p));
}
printf("\n");
}
Problem : 22

Module: Base program


Language: C
Problem statement
Given a string s, remove adjacent duplicate characters from
the string s. The output string should not have any adjacent
duplicates.
Input:
abba
Output:
aba
Solution
#include <stdio.h>
char* f(char s[]) {
for (int i = 0; s[i];) {
char e = s[i];
printf("%c", e);
while (s[i] && s[i] == e)
i++;
}
}
int main() {
char s[1000];
scanf("%s", s);
f(s);
printf("\n");
}
Problem : 23
Module: Base program
Language: C
Problem statement
Given two strings a and b consisting of lowercase characters.
The task is to check whether two given strings are anagram of
each other or not.

Input-1:
post stop
Output-1:
YES

Input-2:
post fast
Output-1:
NO
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
int myCompare(const void *a, const qsort(b, strlen(b), sizeof(char),
void *b) { myCompare);
return *((char *)a) - *((char * )b); return strcmp(a,b) == 0;
}
}
bool areAnagrams(char *s1, char int main() {
*s2) { char x[100], y[100];
if (strlen(s1) != strlen(s2)) { scanf("%s %s", x, y);
areAnagrams(x,
return false;
y)?printf("YES\n"):printf("NO\n");
} }
char a[100], b[100];
strcpy(a, s1); strcpy(b, s2);
qsort(a, strlen(a), sizeof(char),
myCompare);
Problem : 24
Module: Base program
Language: C
Problem statement
Given a string, the task is to remove duplicates from it.
Input:
abba
Output:
ab
Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int myCompare(const void *a, const
void *b) {
}
return *((char *)a) - *((char * )b); int main() {
} char s[1000];
scanf("%s", s);
char* f(char s[]) {
f(s);
qsort(s, strlen(s), sizeof(char), printf("\n");
myCompare); }
for (int i = 0; s[i];) {
char e = s[i];
printf("%c", e);
while (s[i] && s[i] == e)
i++;
}
Problem : 25

Module: Base program


Language: C
Problem statement
You are given a string S containing alphanumeric characters.
Find out whether the string is a palindrome or not.
Input-1:
liril
Output-1:
yes
Input-2:
soap
Output-2:
no
Solution
#include <stdio.h>
#include <string.h>
int f(char s[]) {
int i, j;
for (int i = 0, j = strlen(s) - 1; i < j; i++, j--)
if (s[i] != s[j])
return 0;
return 1;
}

int main() {
char s[1000];
scanf("%s", s);
f(s)?printf("yes\n"):printf("no\n");
}
Problem : 26

Module: Base program


Language: C
Problem statement
Given a string - "str", find the repeated character
present first in the string.

Input-1:
mississippi
Output-1:
i
Solution
#include <stdio.h>
#include <string.h>
int search(char s[], char k) {
for (int i = 0; s[i]; i++)
if (s[i] == k)
return i;
return -1;
}

int main() {
char s[1000];
scanf("%s", s);
for (int i = 0; s[i]; i++)
if (search(s + i + 1, s[i]) != -1) {
printf("%c\n", s[i]);
break;
}
}
Problem : 27
Language :C
Module :Base Module
Problem Statement:Given an array A
of size N, find all combination of Sample Input1:
four elements in the array whose 6
sum is equal to a given value K 10 20 30 40 1 2
91
Sample Output2:
Input Format: 20 1 30 40
First line contain N of type integer.
Second line contains N i.e. is no of N Sample Input2:
arrays elements. 5
10 20 30 40 50
Third line contains K of type integer 100
Sample Output2:
Output Format: 10 20 30 40
Print the 4 elements of type integer
seperated by space.
Solution
#include <stdio.h>
#include <stdlib.h>
// The following structure is needed to store pair sums in aux[]
struct pairSum
{
int first; // index (int A[]) of first element in pair
int sec; // index of second element in pair
int sum; // sum of the pair
};
// Following function is needed for library function qsort()
int compare (const void *a, const void * b)
{
struct pairSum *p = a;
struct pairSum *q = b;
return p->sum > q->sum;
//return ( (*(pairSum *)a).sum - (*(pairSum*)b).sum);
}
// Function to check if two given pairs
have any common element or not // Create an auxiliary array to store all
int noCommon(struct pairSum a, pair sums
struct pairSum b) int size = (n*(n-1))/2;
{ struct pairSum aux[size];
if (a.first == b.first || a.first == b.sec /* Generate all possible pairs from A[]
||
and store sums
a.sec == b.first || a.sec == of all possible pairs in aux[] */
b.sec) int k = 0;
return 0; for (i = 0; i < n-1; i++)
return 1; {
for (j = i+1; j < n; j++)
} {
// The function finds four elements aux[k].sum = arr[i] + arr[j];
with given sum X aux[k].first = i;
void findFourElements (int arr[], int n, aux[k].sec = j;
int X) k++;
}
{
}
int i, j;
// Sort the aux[] array using library arr[aux[j].first], arr[aux[j].sec]);
function for sorting return;
qsort (aux, size, sizeof(aux[0]), }
compare); else if (aux[i].sum + aux[j].sum < X)
i++;
else
// Now start two index variables j--;
from two corners of array }
// and move them toward each }
other. int main()
i = 0; {
int N,i,X;
j = size-1; scanf("%d",&N);
while (i < size && j >=0 ) int arr[N];
{ for(i=0;i<N;i++){
scanf("%d",&arr[i]);
if ((aux[i].sum + aux[j].sum == }
X) && noCommon(aux[i], aux[j]))
scanf("%d",&X);
{ findFourElements(arr, N, X);
printf ("%d %d %d %d\n", return 0;
arr[aux[i].first], arr[aux[i].sec], }
Problem : 28
Language :C
Module :Base Module Output Format:
Problem Statement:Given three Print integer i.e is all common
increasingly sorted arrays A, B, elements in these arrays.
C of sizes N1, N2, and Sample Input1:
N3 respectively, you need to print 445
all common elements in these 1234
arrays. 1 2 30 40
1 2 60 70 80
Sample Output1:
Input Format:
12
First line contains 3 integer of size of
an array i.e N1,N2,N3. Sample Input2:
Second line contain N1 i.e is no of 444
integer array elements. 1 20 30 80
Third line contain N2 i.e is no of 11 20 40 80
integer array elements. 12 10 20 80
Fourth line contain N3 i.e is no of Sample Output2:
integer array elements. 20 80
Solution
printf("%d ",ar1[i]);
#include<stdio.h> i++; j++; k++;
void findCommonElements(int ar1[], }
int ar2[], int ar3[], int n1, int n2, int // x < y
n3) else if (ar1[i] < ar2[j])
{ i++;
// Initialize starting indexes for // y < z
ar1[], ar2[] and ar3[] else if (ar2[j] < ar3[k])
j++;
int i = 0, j = 0, k = 0; // We reach here when x > y and z <
// Iterate through three arrays y, i.e., z is smallest
while all arrays have elements else
while (i < n1 && j < n2 && k < n3) k++;
}
{
}
// If x = y and y = z, print any of
them and move ahead int main() {
// in all arrays int N1,N2,N3,i,j,k;
if (ar1[i] == ar2[j] && ar2[j] == scanf("%d %d %d",&N1,&N2,&N3);
ar3[k]) int arr1[N1],arr2[N2],arr3[N3];
for(i=0;i<N1;i++){
{
scanf("%d",&arr1[i]);
}
for(j=0;j<N2;j++){
scanf("%d",&arr2[j]);
}for(k=0;k<N3;k++){
scanf("%d",&arr3[k]);
}
findCommonElements(arr1,arr2,arr3,N1,N2,N3);
return 0;
}
Problem : 29
Language :C
Module :Base Module
Problem Statement: Given an array of distinct integers. The task is
to count all the triplets such that sum of two elements equals
the third element.

Input Format:
First line contain N of type integer.
Second line N no of distinct elements.

Output Format:
Print 3 integer separated by single space.
Sample Input:
5
1 4 5 100 200
Sample Output:
514
Solution
#include<stdio.h> while (j < k) {
void sort(const void *a, const void if (arr[i] == arr[j] + arr[k]) {
*b) {
// pair found
return *((int*)a) > *((int*)b);
printf("%d %d
} %d\n",arr[i],arr[j],arr[k]);
// utility function for finding return;
// triplet in array } else if (arr[i] > arr[j] + arr[k])
j += 1;
void findTriplet(int arr[], int n) { else
// sort the array k -= 1;
qsort(arr, n, sizeof(int), sort); }
}
// for every element in arr
// check if a pair exist(in array) // no such triplet is found in array
whose printf("No such triplet exists");
// sum is equal to arr element }
for (int i = n - 1; i >= 0; i--) {
int main() {
int j = 0; int N,i;
int k = i - 1; scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d",&arr[i]);
}
findTriplet(arr, N);
return 0;
}
Problem : 30
Language :C Output Format:
Print the integer number Of uneaten
Module :Base Module
leaves.
Problem Statement:K caterpillars are
eating their way through N leaves, Constraints:
each caterpillar falls from leaf to 1<=K<=15
leaf in a unique sequence, all
caterpillars start at a twig at 1<=N<=1000
position 0 and falls onto the leaves Sample Input:
at position between 1 and N. Each 10
caterpillar j has as associated jump 3
number Aj. A caterpillar with jump 2
number j eats leaves at positions 4
that are multiple of j. It will 5
proceed in the order j, 2j, 3j…. till it
reaches the end of the leaves and Sample Output:
it stops and build its cocoon.
4
Input Format: Explanation:
N= No of uneaten leaves. [2, 4, 5] is a j member jump numbers, all
leaves which are multiple of 2, 4, and 5
K= No. of caterpillars
are eaten, leaves 1,3,7,9 are left, and
A = Array of integer jump numbers thus the no. 4
Solution
#include<stdio.h>
int findUneatenLeaves(int leaves,
int k, int jump[]) {
int n = jump[0]; }
int main() {
int uneaten = n-1; int leaves,k,i=0;
int j = 0; scanf("%d",&leaves);
while(n <= leaves) { scanf("%d",&k);
int jump[k];
j = 0; printf("length of an array
while(j < k) { is:%d\n",k);
if((n % jump[j]) == 0 ) while(i < k) {
break; scanf("%d",&jump[i]);
i++;
j++;
}
} printf("%d",findUneatenLeaves(lea
if(j == k) uneaten++; ves, k, jump));
return 0;
n++;
}
}
return uneaten;
Problem : 31
Language :C
Module :Base Module
Problem Statement: Given two
arrays and the operation to be
performed is that the every
element of a[] should be divided
by all the element of b[] and their Sample Input:
floor value has to be calculated. 32
5 100 8
23
Input Format: Sample Output:
First line contains N1,N2 of arrays 0 16 1
sizes.
Second line enter N1 no of elements.
Third line enter N2 no of elements.

Constraints:
1<=N1<=100
1<=N2<=100
Solution
#include<stdio.h>
x = floor(a[j] / mul);
// Function to calculate the quotient printf("%d ",x);
// of every element of the array }
void calculate(int a[], int b[], int n, }
int m) { int main() {
int N1,N2;
int mul = 1;
scanf("%d %d",&N1,&N2);
int i,j,x; //create arr1 and arr2
// Calculate the product of all int arr1[N1];
elements int arr2[N2];
int i,j;
for (i = 0 ; i < m ; i++)
for(i=0;i<N1;i++){
if (b[i] != 0) scanf("%d",&arr1[i]);
mul = mul * b[i]; }
// To calculate the quotient of for(j=0;j<N2;j++){
every scanf("%d",&arr2[j]);
}
// array element
calculate(arr1,arr2, N1, N2);
for (j = 0 ; j < n ; j++) return 0;
{ }
Problem : 32
Language :C
Module :Base Module
Problem Statement: Given a string of both uppercase and lowercase alphabets,
the task is to print the string with alternate occurrences of any character
dropped(including space and consider upper and lowercase as same)

Input Format:
First line contain string str1

Output Format:
Print string.

Sample Input:
Talentio private limited
Sample Output:
talenio prvtimed
Solution
void displayStringAlternate(char
str[]) {
int occ[122];
printf("%c",str[i]);
// Convert uppercase to }
lowercase
printf("\n");
char *s = strlwr(str); }
int len = strlen(str);
// Start traversing the string
int main()
for (int i = 0; i < len; i++) { {
char str[100];
char temp = s[i]; gets(str);

// Increment occurrence
count displayStringAlternate(st
occ[temp]++; r);
// If count is odd then print return 0;
the character }
if (occ[temp]%2 != 0)
Problem : 33
Language :C
Module :Base Module
Problem Statement:
Given a string S and text T. Output the
smallest window in the Here len1 is of String and len2
string S having all characters of the is pattern i.e. T
text T. Both the string S and text T
contains lowercase english alphabets.
Sample Input:
this is a test string
Input Format: tist
Sample Output:
First line contain string S
Smallest window is :t stri
Second line contain pattern text T

Output Format:
Print string S
Note: if len1<len2 then print message
"No such window exists"
Solution return str;
#include<stdio.h> }
#include<limits.h> // Function to find smallest window
containing
#include<string.h> // all characters of 'pat'
#include<stdlib.h> char * findSubString(char str[], char
#define NO_OF_CHARS 256 pat[])
{
char* ss(char s[],int start_index,int int len1 = strlen(str);
min_len)
int len2 = strlen(pat);
{
char *str; // check if string's length is less than
pattern's
int index=0;
// length. If yes then no such window
str =malloc(strlen(s)+1); can exist
for(int if (len1 < len2)
i=start_index;i<min_len;i++){ {
str[index] =s[i]; printf("No such window exists");
return "";
index++; }
}
str[index]='\0'; int hash_pat[NO_OF_CHARS ]={0};
int hash_str[NO_OF_CHARS ]={0};
// store occurrence of's characters if (hash_pat[str[j]] != 0 &&
of pattern hash_str[str[j]] <= hash_pat[str[j]]
)
for (int i = 0; i < len2; i++)
count++;
hash_pat[pat[i]]++;
int start = 0, start_index = -1, // if all the characters are matched
min_len = INT_MAX; if (count == len2)
{
// start traversing the string
// Try to minimize the window i.e.,
int count = 0; // count of characters check if
for (int j = 0; j < len1 ; j++) // any character is occurring more
{ no. of times
// than its occurrence in pattern,
// count occurrence of characters if yes
of string // then remove it from starting
hash_str[str[j]]++; and also remove
// the useless characters.
while ( hash_str[str[start]] >
// If string's char matches with
hash_pat[str[start]]
pattern's char
|| hash_pat[str[start]] == 0)
// then increment count {
if (hash_str[str[start]] > // If no window found
hash_pat[str[start]]) if (start_index == -1)
hash_str[str[start]]--; {
start++;
printf("No such window exists");
} return "";
}
// update window size
// Return substring starting from
int len_window = j - start + start_index
1;
if (min_len > len_window) // and length min_len
{ //return str.substr(start_index,
min_len);
min_len = len_window; return
start_index = start; ss(str,start_index,start_index+min_len)
} ;
} }
}
// Driver code
int main()
{
char str[100];
char pat[100];
gets(str);
gets(pat);
printf("Smallest window is :%s\n",findSubString(str, pat));
return 0;
}
Problem : 34
Languge : C
Module : Base Module
Problem Statement:
Given a string, the task is to remove duplicates from it.
Input format:
First line of input contains string taken from user.
Output format:
Output will be an string that doesn’t contain duplicates in it.
Constraints:
1<=s<=100
Input:
Strings
Output:
String
Solution

#include <stdio.h>
#include <string.h>
int main(){
char str[100];
str[k] = str[k + 1];
int i, j, k; }
gets(str); }
}
for(i = 0; i < strlen(str); i++){ }
for(j = i + 1; str[j] != '\0'; printf("\n Final String= %s ",
j++) str);
return 0;
{ }
if(str[j] == str[i])
{
for(k = j;
str[k] != '\0'; k++)
{
Problem : 35

Languge : C
Module : Base Module
Problem Statement:
Given a string S, find length of the longest substring with all
distinct characters.
Input Format:
The first line contains string Str.
Output Format:
The output contains length of longest substring in string Str.
Constraints:
1<=S<=1000
Input : ABDEFGABEF
Output : 6
Solution
#include <stdio.h>
#include<string.h> for (i = 0; i < NO_OF_CHARS; i++)
visited[i] = -1;
#include<stdlib.h>
visited[str[0]] = 0;
#include<stdio.h> for (i = 1; i < n; i++)
#include<string.h> {
prev_index = visited[str[i]];
#define NO_OF_CHARS 256
if (prev_index == -1 || i - cur_len
int min(int a, int b); > prev_index)
int longestUniqueSubsttr(char *str) cur_len++;
else
{
{
int n = strlen(str); if (cur_len > max_len)
int cur_len = 1; max_len = cur_len;
int max_len = 1; cur_len = i - prev_index;
}
int prev_index; visited[str[i]] = i;
int i; }
int *visited = (int if (cur_len > max_len)
*)malloc(sizeof(int)*NO_OF_CHAR max_len = cur_len;
S); free(visited); // free memory
allocated for visited
return max_len;
} int min(int a, int b)
{
return (a>b)?b:a;
}
int main()
{
gets(str);
printf("%s\ n", str);
int len = longestUniqueSubsttr(str);
printf(“ %d", len);
return 0;
}
Problem : 36
Languge : C
Module : Base Module
Problem Statement:
Your task is to implement the function atoi. The function takes a
string(str) as argument and converts it to an integer and returns
it.
Input format:
The sample input contains integer in string format.
Output Format:
Output contains the string converted in integer format.
Constraints:
1<=s<=1000
Input:
“89789”
Output:
89789
Solution
#include <stdio.h>
int myAtoi(char *str)
{
int res = 0;
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
return res;
}
int main()
{
char str[] = "89789";
int val = myAtoi(str);
printf ("%d ", val);
return 0;
}
Problem : 37
Languge : C
Module : Base Module
Problem Statement:
The function takes two strings as index number. Return -1 if no
arguments(s,x) and locates the match found.
occurrence of the string x in the
string s. The function returns and Constraints:
integer denoting the first 1 <= T <= 200
occurrence of the string x in s. 1<= |s| ,|x| <= 1000
Example:
Input Format:
Input
The first line of input contains an 2
integer T denoting the no of test TalentioSolutions sl
cases . Then T test cases follow. TalentioSolutions Sol
The first line of each test case
Output
contains two strings s and x.
-1
Output Format: 8
For each test case, in a new line,
output will be an integer
denoting the first occurrence of
the x in the string s which returns
Solution
int strstr(string first, string second)
{
}
int i=0,k=0,index=0,flag=1,m=0; i++;
while(first[i]!='\0'){ }
if(m==0)
if(first[i]==second[k]){
return -1;
if(flag) else
index = i; return index;
}
flag = 0;
int strstr(string[],string[]);
k++; int main()
}else{ {
k=0; Int n;
scanf(“%d\n”,&n);
flag = 1; gets(first);
} gets(second);
if(second[k]=='\0'){ printf(“%d”,strstr(first,second));
}
m=1;
break;
Problem : 38
Languge : C
Module : Base Module
Problem Statement:
Given a array of N strings, find the longest common prefix among
all strings present in the array.
Input format:
The input given is array of N strings.
Output Format:
The output gives the string which is longest among all strings.
Constraints:
1<=S<=1000
Input:
{"apple", "ape", "april"}
Ouptut:
"ap"
Solution return (true);
}
#include<stdio. h>
string commonPrefix(string arr[], int n)
int findMinLength(string arr[], int n) {
{ int index = findMinLength(arr, n);
string prefix;
int min = INT_MAX;
int low = 0, high = index;
for (int i=0; i<=n-1; i++) while (low <= high)
if (arr[i].length() < min) {
int mid = low + (high - low) / 2;
min = arr[i].length();
if (allContainsPrefix (arr, n, arr[0],
return(min); low, mid))
} {
bool allContainsPrefix(string arr[], prefix = prefix +
int n, string str, int start, int end) arr[0].substr(low, mid-low+1);
low = mid + 1;
{ }
for (int i=0; i<=n-1; i++) else
for (int j=start; j<=end; j++) high = mid - 1;
}
if (arr[i][j] != str[j])
return (prefix);
return (false); }
int main()
{
int n,i;
for(i=0;i<n;i++)
{
Scanf(“%s”,arr[i]);
}
int n = sizeof (arr) / sizeof (arr[0]);
string ans = commonPrefix(arr, n);
if (ans.length())
printf(" longest common prefix is %s ",ans);
else
printf(" no common prefix");
return 0;
}
Problem : 39
Languge : C Output:
Module : Base Module For each test case print the space
separated values of the new matrix.
Problem Statement:
Given a 2D screen, location of a Constraints:
pixel in the screen ie(x,y) and a 1 <= T <= 100
color(K), your task is to replace 1 <= M[][] <= 100
color of the given pixel and all
adjacent(excluding diagonally
Example:
adjacent) same colored pixels
with the given color K.
Input:
Input:
The first line of input contains an 3
integer T denoting the no of test 34
cases. Then T test cases follow. 011011110123
The first line of each test case 015
contains Two integers N and M
22
denoting the size of the matrix.
Then in the next line are N*M 1111
space separated values of the 018
matrix. Then in the next line are 44
three values x, y and K. 1234123412341324
029

Output:
055055550523
8888
1294129412941324
color= plane[x][y];
Solution
void map_colour(int x,int y,int n,int map_colour(x,y,n,m,plane,k,color);
m,int plane[][100],int k,int color);
for(i=0;i<n;i++)
int plane[100][100]; { for(j=0;j<m;j++)
int main() { printf("%d ",plane[i][j]);
}
{
}
int t,n,m,i,x,y,k,j,color; printf("\n");
scanf("%d",&t); }
return 0;
while(t--)
}
{ void map_colour(int x,int y,int n,int m,int
scanf("%d %d",&n,&m); plane[][100],int k, int color)
for(i=0;i<n;i++) { plane[x][y]=k;

{ for(j=0;j<m;j++) if(plane[x][y-1]==color && y-1>=0)


{ scanf("%d",&plane[i][j]); {
} map_colour(x,y-1,n,m,plane,k,color);}

} if(plane[x][y+1]==color && y+1<m)


scanf("%d %d %d",&x,&y,&k); {
map_colour(x,y+1,n,m,plane,k,color);}

if(plane[x-1][y]==color && x-1>=0)


{
map_colour(x-1,y,n,m,plane,k,color);}

if(plane[x+1][y]==color && x+1<n)


{
map_colour(x+1,y,n,m,plane,k,color);}
}
Problem : 40
Languge : C
Module : Base Module Output:
Print maximum number of A's. Print -1, if
Problem Statement:
N is greater than 75.
Prints 'A' on screen, (Ctrl-A): Select
screen, (Ctrl-C): Copy selection to Constraints:
buffer, (Ctrl-V): Print buffer on 1 <= T <= 50
screen appending it after what
1 <= N <= 75
has already been printed. : If you
can only press the keyboard for N
times (with the above four keys), Example:
write a program to produce Input:
maximum numbers of A's. 2
3
7
Input:
The first line of input contains an Output:
integer T denoting the number of 3
test cases. The first line of each 9
test case is N, N is the number of
key.
Explanation:
Testcase 1: We can at most get 3 A's on screen by pressing
following key sequence.
A, A, A.
Testcase 2: We can at most get 9 A's on screen by pressing
following key sequence.
A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl
Solution
#include <stdlib.h>
int max(int a, int b){
if(a>b){ for(int i=6;i<=75;i++){
a[i] = max(4*a[i-5], 3*a[i-4]);
return a;
}
}else{ int t;
return b; scanf("%d", &t);
for(int i=0;i<t;i++){
}
int n;
} scanf("%d", &n);
int main() { if(n > 75){
printf("%d\n", -1);
int *a = malloc(sizeof(int) * 76);
}else{
a[0] = 0; printf("%d\n", a[n]);
a[1] = 1; }
a[2] = 2; }
return 0;
a[3] = 3; }
a[4] = 4;
a[5] = 5;
Problem : 41

Languge : C
Note: Assume that there are enough
Module : Base Module glasses in the triangle till no glass
Problem Statement: overflows.

Input:First line of the input contains an


There is a stack of water glasses in a
integer T denoting the number of test
form of pascal triangle and a
person wants to pour the water at cases and each test case consists of
the topmost glass, but the three lines. First line contain an integer
capacity of each glass is 1 unit. K, second line contains an integer i and
Overflow takes place in such a third line contains an integer j.
way that after 1 unit, 1/2 of
remaining unit gets into bottom Output:
left glass and other half in bottom Corresponding to each test case output
right glass.Now the pours K units the remaining amount of water in jth
of water in the topmost glass and
cup of the ith row correct to 6 decimal
wants to know how much water is
there in the jth glass of the ith places.
row.
Constraints:
1 <= T<=20
1 <= K <= 1000
1 <= i <= K
1 <= j<= K

Example:
Input:
1
3
2
1

Output:
1
dp[q][q]=temp>0?temp:0;
Solution }
int max(int a, int b) for(q=2; q<i; q++)
{ {
for(r=1; r<q; r++)
if(a>b)
{
return a; dp[q][r]=0;
return b; temp=(dp[q-1][r-1]-1)/2;
temp2=(dp[q-1][r]-1)/2;
}
if(temp>0)
float pascal_water(int k, int i, int j) dp[q][r]+=temp;
{ if(temp2>0)
dp[q][r]+=temp2;
int m, q, r, s;
m=max(i,j); }
float dp[m][m], temp, temp2; }
dp[0][0]=k; return dp[i-1][j-1];
}
for(q=1; q<m; q++)
{
temp=(dp[q-1][0]-1)/2; void main()
{
dp[q][0]=temp>0?temp:0;
int i, j, t, k;
float res;
scanf("%d", &t);
while(t--)
{
scanf("%d", &k);
scanf("%d", &i); Test cases:
scanf("%d", &j); 1
5
res=pascal_water(k, i, j);
31
if(res>=1) O/P:0.500000
printf("1\n");
else
printf("%0.6f\n", res);
}
}
Problem : 42
Languge : C
Module : Base Module
Problem Statement: Input : row= 2,col = 2;
The problem is to count all the Output : 2
possible paths from top left to Explanation:
bottom right of a MxN matrix There are two paths
with the constraints that from (0, 0) -> (0, 1) -> (1, 1)
each cell you can either move to (0, 0) -> (1, 0) -> (1, 1)
right or down.
Input : row = 2, col = 3;
Input: Output : 3
Explanation:
First line of the input contains an
There are three paths
integers row and col. .
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2)
(0, 0) -> (0, 1) -> (1, 1) -> (1, 2)
Output: (0, 0) -> (1, 0) -> (1, 1) -> (1, 2)
Number of ways possible from top
left to reach bottom right
Solution
#include<stdio.h>
int find_way(int x, int y)
{
if (x == 1 || y == 1)
return 1;
return find_way(x-1, y) + find_way(x, y-1);
}

int main()
{
int row,col;
scanf("%d%d",&row,&col);
printf("\n%d",find_way(row,col));
}
Test Case:
23

O/p:3

88

O/P: 3432
Test case:

22
12
34
O/p:1 2 4 3
Problem : 43

Languge : C
Output:
Module : Base Module Print the total number of string
Problem Statement:Given 3 that can be formed.
characters a, b, c. Find the
number of strings of length n that Constraints:
can be formed from these 3 1<=T<=20
characters. Given that : we can 1<=N<=20
use ‘a’ as many times as we want,
‘b’ maximum once, and ‘c’
maximum twice. Example:
Input:
2
Input: 3
The first line of input contains an 5
integer T denoting the number of
test cases. The first and last line Output:
of each test case consists of an 19
integer n. 71
Solution

int main() {
int t, N, ans;
scanf("%d", &t);
while(t--) {
scanf("%d", &N);
ans = (1 + N * (N + 1));
ans += ((N * (N - 1) * (N - 1)) / 2);
printf("%d\n", ans);
}
return 0;
}
Test case:
1
2
O/P:8

3
1
O/P:3
2
O/P:8
3
O/P:19
Problem : 44

Languge : C
Module : Base Module mat[][] = { {10, 20, 30, 40},
Problem Statement:Print matrix in {15, 25, 35, 45},
snake pattern. {27, 29, 37, 48},
{32, 33, 39, 50}};

Input: First line containing row size Output : 10 20 30 40 45 35 25 15 27


and coloum size, r,c; 29
Second line consisting matrix 37 48 50 39 33 32
elements r*c.
Output: Input :
R=3 c=3
Matrix in snake pattern.
mat[][] = { {1, 2, 3},
{4, 5, 6},
Input : {7, 8, 9}};
Output : 1 2 3 6 5 4 7 8 9
R=4,c=4
Solution int main()
{
#include<stdio.h> int i,j, r,c;
void print(int M,int N, int **m) scanf("%d%d",&r,&c);
{ int **m = (int **)malloc(r * sizeof(int
*));
int i,j; for (i=0; i<r; i++)
for (int i = 0; i < M; i++) { m[i] = (int *)malloc(c * sizeof(int));

for(i=0;i<r;i++)
if (i % 2 == 0) {
{
for (int j = 0; j < N; j++) for(j=0;j<c;j++)
printf("%d ",m[i][j]); {
scanf("%d",&m[i][j]);
}
}
else { }
for (int j = N - 1; j >= 0; j--)
printf("%d ",m[i][j]);
print(r,c,m);
} return 0;
} }

You might also like