DAA Basic C Programming n[5]
DAA Basic C Programming n[5]
LAB Record
2116231801153
EXP.NO:1(a)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int a, b, t;
t=a;
a=b;
b=t;
return 0;
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(b)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
Write a C program to find the eligibility of admission for a professional course based on the
following criteria:
Or
Test Case 1
Input
70 60 80
Output
#include<stdio.h>
int main()
{
int m,p,c;
scanf("%d %d %d",&m,&p,&c);
int t=m+p+c;
2116231801153
printf("The candidate is not eligible");
}
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(c)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
Malini goes to BestSave hyper market to buy grocery items. BestSave hyper market provides 10%
discount on the bill amount B when ever the bill amount B is more than Rs.2000.
The bill amount B is passed as the input to the program. The program must print the final amount A
payable by Malini.
Input Format:
The first line denotes the value of B.
Output Format:
The first line contains the value of the final payable amount A.
Example Input/Output 1:
Input:
1900
Output:
1900
Example Input/Output 2:
Input:
3000
Output:
2700
PROGRAM:
#include<stdio.h>
int main(){
int c, t;
scanf("%d",&c);
if(c>2000){
t=c-(c*0.1);
}
else{
t=c;
}
printf("%d",t);
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(d)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
Baba is very kind to beggars and every day Baba donates half of the amount he has when
ever a beggar requests him. The money M left in Baba's hand is passed as the input and the
number of beggars B who received the alms are passed as the input. The program must print
the money Baba had in the beginning of the day.
Input Format:
Output Format:
The first line denotes the value of money with Baba in the beginning of the day.
Example Input/Output:
Input:
100
2
Output:
400
PROGRAM:
#include<stdio.h>
int main(){
int m, b;
scanf("%d", &m);
scanf("%d", &b);
int t=m*b;
printf("%d", t*2);
}
2116231801153
OUTPUT:
2116231801153
BASIC C PROGRAMMING-PRACTICE
EXP.NO:1(e)
DATE:
QUESTION:
The CEO of company ABC Inc wanted to encourage the employees coming on time to the
office. So he announced that for every consecutive day an employee comes on time in a
week (starting from Monday to Saturday), he will be awarded Rs.200 more than the previous
day as "Punctuality Incentive". The incentive I for the starting day (ie on Monday) is passed
as the input to the program. The number of days N an employee came on time consecutively
starting from Monday is also passed as the input. The program must calculate and print the
"Punctuality Incentive" P of the employee.
Input Format:
Output Format:
Example Input/Output:
Input:
500
3
Output:
2100
PROGRAM:
#include<stdio.h>
int main(){
int a,d;
scanf("%d",&a);
scanf("%d",&d);
int t=0;
for(int i=0;i<d;i++)
{
a=a+200;
2116231801153
t=t+a;
}
printf("%d",t-600);
}
2116231801153
OUTPUT:
21162318011853
EXP.NO:1(f)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
Two numbers M and N are passed as the input. A number X is also passed as the input. The
program must print the numbers divisible by X from N to M (inclusive of M and N).
Input Format:
Output Format:
Boundary Conditions:
Example Input/Output 1:
Input:
2
40
7
Output:
35 28 21 14 7
PROGRAM:
#include<stdio.h>
int main()
{
int m,n,x;
scanf("%d\n%d\n%d",&m,&n,&x);
for(int i=n;i>=m;i--){
if(i%x==0){
printf("%d ",i);
}
}
2116231801153
}
OUTPUT:
2116231801153
EXP.NO:1(g)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main(){
int a, b;
return 0;
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(h)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int a,b,c,g;
scanf("%d %d %d",&a,&b,&c);
g=a;
g=b;
else
g=c;
printf("%d",g);
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(i)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%2==0)
printf("Even");
else
printf("Odd");
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(j)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i,f=1;
for(i=1;i<=n;i++)
{
f*=i;
}
printf("%d",f);
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(k)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int s=0;
for(int i=1;i<=n;i++)
{
s+=i;
}
printf("%d",s);
return 0;
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(l)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main(){
int n,a,b,c;
scanf("%d",&n);
a=0;
b=1;
for(int i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
printf("%d",a);
return 0;
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(m)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
input:
ab
output:
a^b value
For example:
PROGRAM:
#include<math.h>
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int p=pow(a,b);
printf("%d",p);
}
2116231801153
OUTPUT:
2116231801153
EXP.NO:1(n)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
For example:
PROGRAM:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
{
printf("Prime");
}
else
{
printf("No Prime");
}
}
2116231801153
OUTPUT:
EXP.NO:1(o)
BASIC C PROGRAMMING-PRACTICE
DATE:
QUESTION:
PROGRAM:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int r=0;
while(n!=0){
r=(r*10)+(n%10);
n=n/10;
}
printf("%d",r);
}
OUTPUT:
EXP.NO:2(a)
FINDING TIME COMPLEXITY USING COUNTER METHOD
DATE:
QUESTION:
Convert the following algorithm into a program and find its time complexity using the
counter method.
void function (int n)
{
int i= 1;
int s =1;
while(s <= n)
{
i++;
s += i;
}
}
Note: No need of counter increment for declarations and scanf() and count variable printf()
statements.
Input:
A positive Integer n
Output:
Print the value of the counter variable
PROGRAM:
#include<stdio.h>
int main()
int n;
int count=0;
scanf("%d",&n)
; int i=1;
count++;
int s=1;
count++;
while(s<=n)
{
count++;
i++;
count++;
s=s+i;
count++;
count++;
printf("%d",count);
return 0;
}
OUTPUT:
EXP.NO:2(b)
FINDING TIME COMPLEXITY USING COUNTER METHOD
DATE:
QUESTION:
Convert the following algorithm into a program and find its time complexity using the
counter method.
void func(int n)
{
if(n==1)
{
printf("*");
}
else
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
printf("*");
printf("*");
break;
}
}
}
}
Note: No need of counter increment for declarations and scanf() and count variable printf()
statements.
Input:
A positive Integer n
Output:
Print the value of the counter variable
PROGRAM:
#include<stdio.h>
int main()
{
int n; scanf("%d",&n); int c = 0;
int i;
c++;
int j;
c++;
if (n == 1)
{ c++;
c++;
} else {
c++;
c++;
c++;
c++;
break;
c++;
printf("%d", c);
}
OUTPUT:
EXP.NO:2(c)
FINDING TIME COMPLEXITY USING COUNTER METHOD
DATE:
QUESTION:
Convert the following algorithm into a program and find its time complexity using counter method.
Factor(num) {
{
for (i = 1; i <= num;++i)
{
if (num % i== 0)
{
printf("%d ", i);
}
}
}
Note: No need of counter increment for declarations and scanf() and counter variable printf()
statement.
Input:
A positive Integer n
Output:
Print the value of the counter variable
PROGRAM:
#include<stdio.h>
int main()
int n,i;
int c=0;
scanf("%d",&n);
c++;
if (n % i== 0)
c++;
c++;
c++;
printf("%d",c);
return 0;
}
OUTPUT:
EXP.NO:2(d)
FINDING TIME COMPLEXITY USING COUNTER METHOD
DATE:
QUESTION:
Convert the following algorithm into a program and find its time
void function(int n)
{
int c= 0;
for(int i=n/2; i<n; i++)
for(int j=1; j<n; j = 2 * j)
for(int k=1; k<n; k = k * 2)
c++;
}
Note: No need of counter increment for declarations and scanf() and count variable printf()
statements.
Input:
A positive Integer n
Output:
Print the value of the counter variable
PROGRAM:
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
int c=0;
c++;
for(int i=n/2;i<n;i++)
{
c++;
for(int j =1;j<n;j=2*j)
c++;
for(int k =1;k<n;k=k*2)
c++;
c++;
// c++;
c++;
c++;
c++;
printf("%d",c);
}
OUTPUT:
EXP.NO:2(e)
FINDING TIME COMPLEXITY USING COUNTER METHOD
DATE:
QUESTION:
Convert the following algorithm into a program and find its time complexity using counter
method.
void reverse(int n)
{
int rev = 0, remainder;
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n/= 10;
}
print(rev);
}
Note: No need of counter increment for declarations and scanf() and count variable printf()
statements.
Input:
A positive Integer n
Output:
Print the value of the counter variable
PROGRAM:
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
int c =0;
c++;
while(n!=0)
{c++;
remainder = n % 10;
c++;
c++;
n/= 10;
c++;
c++;
//print(rev);
c++;
printf("%d",c);
}
OUTPUT:
EXP.NO:3(a)
DIVIDE AND CONQUER
DATE:
PROBLEM STATEMENT:
Given an array of 1s and 0s this has all 1s first followed by all 0s. Aim is to find the number of 0s. Write
a program using Divide and Conquer to Count the number of zeroes in the given array.
Input Format
First Line Contains Integer m – Size of array
Next m lines Contains m numbers – Elements of an array
Output Format
First Line Contains Integer – Number of zeroes present in the given array.
PROGRAM:
#include <stdio.h>
int main() {
int m, i; scanf("%d",
scanf("%d", &arr[i]);
<= high) {
= mid;
break;
if (arr[mid] == 1) {
low = mid + 1;
}
}
if (firstZeroIndex == -1) {
printf("0\n");
} else {
printf("%d\n", m - firstZeroIndex);
return 0;
}
OUTPUT:
EXP.NO:3(b)
DIVIDE AND CONQUER
DATE:
PROBLEM STATEMENT:
Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints:
n == nums.length 1
<= n <= 5 * 104
-231 <= nums[i] <= 231 – 1
PROGRAM:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int nums[n];
scanf("%d", &nums[i]);
int count = 0;
int candidate = 0;
if (count == 0) {
candidate = nums[i];
}
if (nums[i] == candidate) {
count++;
} else {
count--;
printf("%d\n", candidate);
return 0;
}
OUTPUT:
EXP.NO:3(c)
DIVIDE AND CONQUER
DATE:
PROBLEM STATEMENT:
Given a sorted array and a value x, the floor of x is the largest element in array smaller than
or equal to x. Write divide and conquer algorithm to find floor of x.
Input Format
First Line Contains Integer n – Size of array
Next n lines Contains n numbers – Elements of an array
Last Line Contains Integer x – Value for x
Output Format
First Line Contains Integer – Floor value for x
PROGRAM:
#include <stdio.h>
int main() {
int n, x;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &x);
if (arr[mid] == x) {
floor = arr[mid];
break;
if (arr[mid] < x) {
floor = arr[mid];
left = mid + 1;
else {
right = mid - 1;
if (floor != -1) {
printf("%d\n", floor);
} else {
return 0;
}
OUTPUT:
EXP.NO:3(d)
DIVIDE AND CONQUER
DATE:
PROBLEM STATEMENT:
Given a sorted array of integers say arr[] and a number x. Write a recursive program using
divide and conquer strategy to check if there exist two elements in the array whose sum = x.
If there exist such two elements then return the numbers, otherwise print as “No”.
Note: Write a Divide and Conquer Solution
Input Format
First Line Contains Integer n – Size of array
Next n lines Contains n numbers – Elements of an array
Last Line Contains Integer x – Sum Value
Output Format
First Line Contains Integer – Element1
Second Line Contains Integer – Element2 (Element 1 and Elements 2 together sums to
value “x”)
PROGRAM:
#include <stdio.h>
int main() {
int n, x;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &x);
int found = 0;
printf("%d\n", arr[left]);
printf("%d\n", arr[right]);
f ound = 1;
break;
if (sum < x) {
left++;
} else {
right--;
if (!found) {
printf("No\n");
return 0;
}
OUTPUT:
EXP.NO:3(e)
DIVIDE AND CONQUER
DATE:
PROBLEM STATEMENT:
Input Format:
The first line contains the no of elements in the list-n
The next n lines contain the elements.
Output:
Sorted list of elements
PROGRAM:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n];
scanf("%d", &a[i]);
a[i] = a[j];
a[j] = temp;
}
return 0;
}
OUTPUT:
EXP.NO:4(a)
1-G-COIN PROBLEM
DATE:
QUESTION:
Write a program to take value V and we want to make change for V Rs, and we have infinite
supply of each of the denominations in Indian currency, i.e., we have infinite supply of { 1, 2, 5,
10, 20, 50, 100, 500, 1000} valued coins/notes, what is the minimum number of coins and/or
notes needed to make the change.
Input Format:
Output Format:
Example Input :
64
Output:
Explanaton:
#include <stdio.h>
int min_coins_and_notes(int V) {
int count = 0;
if (V == 0) {
break;
count += V / denominations[i];
V %= denominations[i];
return count;
int main() {
int V;
scanf("%d", &V);
printf("%d\n", min_coins_and_notes(V));
return 0;
}
OUTPUT:
EXP.NO:4(b)
2-G-COOKIES PROBLEM
DATE:
QUESTION:
Assume you are an awesome parent and want to give your children some cookies. But, you should
give each child at most one cookie.
Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be
content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the
child i, and the child i will be content. Your goal is to maximize the number of your content
children and output the maximum number.
Example 1:
Input:
123
11
Output:
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size is both 1, you could only make the child
whose greed factor is 1 content.
Constraints:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
int childIndex = 0;
int cookieIndex = 0;
childIndex++;
cookieIndex++;
return childIndex;
int main() {
scanf("%d", &gSize);
if (g == NULL) {
return 1;
scanf("%d", &g[i]);
}
scanf("%d", &sSize);
if (s == NULL) {
fprintf(stderr, "Memory allocation failed\n");
free(g);
return 1;
scanf("%d", &s[i]);
free(g);
free(s);
return 0;
}
OUTPUT:
EXP.NO:4(c)
3-G-BURGER PROBLEM
DATE:
QUESTION:
A person needs to eat burgers. Each burger contains a count of calorie. After eating the burger,
the person needs to run a distance to burn out his calories.
If he has eaten i burgers with c calories each, then he has to run at least 3i * c kilometers to burn
out the calories. For example, if he ate 3
burgers with the count of calorie in the order: [1, 3, 2], the kilometers he needs to run are (30 * 1)
+ (31 * 3) + (32 * 2) = 1 + 9 + 18 = 28.
But this is not the minimum, so need to try out other orders of consumption and choose the
minimum value. Determine the minimum distance
he needs to run. Note: He can eat burger in any order and use an efficient sorting
algorithm.Apply greedy approach to solve the problem.
Input Format
First Line contains the number of burgers
Second line contains calories of each burger which is n space-separate integers
Output Format
Print: Minimum number of kilometers needed to run to burn out the calories
Sample Input
3
5 10 7
Sample Output
76
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
}
long long minDistance(int calories[], int n) {
exit(1);
exit(1);
powerOf3 *= 3;
return totalDistance;
int main() {
int n;
return 1;
if (n == 0) {
printf("0\n");
return 0;
}
int *calories = (int *)malloc(n * sizeof(int));
if (calories == NULL) {
return 1;
free(calories);
return 1;
free(calories);
return 0;
}
OUTPUT:
EXP.NO:4(d)
4-G-ARRAY SUM MAX PROBLEM
DATE:
QUESTION:
Given an array of N integer, we have to maximize the sum of arr[i] * i, where i is the index of the
element (i = 0, 1, 2, ..., N).Write an algorithm based on Greedy technique with a Complexity
O(nlogn).
Input Format:
Output Format:
Sample Input:
25340
Sample output:
40
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
scanf("%d", &arr[i]);
}
qsort(arrint max_sum = 0;
max_sum += arr[i] * i;
printf("%d\n", max_sum);
free(arr);
return 0;
, n, sizeof(int), compare);
OUTPUT:
EXP.NO:4(e)
5-G-PRODUCT OF ARRAY ELEMENTS-MINIMUM
DATE:
QUESTION:
Given two arrays array_One[] and array_Two[] of same size N. We need to first rearrange the
arrays such that the sum of the product of pairs( 1 element from each) is minimum. That is SUM
(A[i] * B[i]) for all i is minimum.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
scanf("%d", &array_One[i]);
scanf("%d", &array_Two[i]);
}
qsort(array_One, n, sizeof(int), compare_asc); qsort(array_T)
int min_sum = 0;
printf("%d\n", min_sum);
free(array_One);
free(array_Two);
return 0;
QUESTION:
Ram and Sita are playing with numbers by giving puzzles to each other. Now it was Ram term, so
he gave Sita a positive integer ‘n’ and two numbers 1 and 3. He asked her to find the possible
ways by which the number n can be represented using 1 and 3.Write any efficient algorithm to
find the possible ways.
Example 1:
Input: 6
Output:6
Explanation: There are 6 ways to 6 represent number with 1 and 3
1+1+1+1+1+1
3+3
1+1+1+3
1+1+3+1
1+3+1+1
3+1+1+1
Input Format
First Line contains the number n
Output Format
Print: The number of possible ways ‘n’ can be represented using 1 and 3
Sample Input
6
Sample Output
PROGRAM:
#include <stdio.h>
dp[0] = 1;
if (n >= 1) {
dp[1] = 1;
}
if (n >= 2) {
dp[2] = 1;
if (n >= 3) {
dp[3] = 2;
return dp[n];
int main() {
int n;
scanf("%d", &n);
printf("%lld\n", count_ways(n));
return 0;
}
OUTPUT:
EXP.NO:5(b)
PLAYING WITH CHESSBOARD
DATE:
QUESTION:
Ram is given with an n*n chessboard with each cell with a monetary value. Ram stands at
the (0,0), that the position of the top left white rook. He is been given a task to reach the
bottom right black rook position (n-1, n-1) constrained that he needs to reach the position by
traveling the maximum monetary path under the condition that he can only travel one step
right or one step down the board. Help ram to achieve it by providing an efficient DP
algorithm.
Example:
Input
3
124
234
871
Output:
19
Explanation:
Totally there will be 6 paths among that the optimal is
Optimal path value:1+2+8+7+1=19
Input Format
First Line contains the integer n
The next n lines contain the n*n chessboard values
Output Format
PROGRAM:
#include <stdio.h>
return (a > b) ? a : b;
dp[0][0] = chessboard[0][0];
int main() {
int n;
int chessboard[MAX][MAX];
scanf("%d", &n);
scanf("%d", &chessboard[i][j]);
return 0;
}
OUTPUT:
EXP.NO:5(c)
LONGEST COMMON SUBSEQUENCE
DATE:
QUESTION:
Given two strings find the length of the common longest subsequence(need not be
contiguous) between the two.
Example:
s1: ggtabe
s2: tgatasb
s1 a g g t a b
s2 g x t x a y b
The length is 4
PROGRAM:
#include <stdio.h>
#include <string.h>
int m = strlen(s1);
int n = strlen(s2);
if (i == 0 || j == 0) {
dp[i][j] = 0;
dp[i][j] = (dp[i - 1][j] > dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
return dp[m][n];
int main() {
scanf("%s", s1);
scanf("%s", s2);
printf("%d",result);
return 0;
}
OUTPUT
EXP.NO:5(d)
LONGEST NON-DECREASING SUBSEQUENCE
DATE:
QUESTION:
Problem statement:
Eg:
Input:9
Sequence:[-1,3,4,5,2,2,2,2,3]
Output:6
PROGRAM:
#include <stdio.h>
int dp[n];
int max_length = 1;
dp[i] = 1;
max_length = dp[i];
}
return max_length;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]); }
return 0;
}
OUTPUT:
EXP.NO:6(a)
FINDING DUPLICATES-O(N^2) TIME COMPLEXITY,O(1) SPACE
COMPLEXITY
DATE:
QUESTION:
Given a read only array of n integers between 1 and n, find one number that repeats.
Input Format:
n Lines - n Elements
Output Format:
For example:
Input Result
5 1
11234
PROGRAM:
#include <stdio.h>
slow = arr[slow];
fast = arr[arr[fast]];
}
fast = 0;
slow = arr[slow];
fast = arr[fast];
return slow;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
printf("%d", duplicate);
return 0;
}
OUTPUT:
EXP.NO:6(b)
FINDING DUPLICATES-O(N) TIME COMPLEXITY,O(1) SPACE
COMPLEXITY
DATE:
QUESTION:
Given a read only array of n integers between 1 and n, find one number that repeats.
Input Format:
n Lines - n Elements
Output Format:
For example:
Input Result
5 1
11234
PROGRAM:
#include <stdio.h>
fast = arr[arr[fast]];
fast = 0;
slow = arr[slow];
fast = arr[fast];
return slow;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
printf("%d", duplicate);
return 0;
}
OUTPUT:
EXP.NO:6(c) PRINT INTERSECTION OF 2 SORTED ARRAYS-O(M+N)TIME
COMPLEXITY,O(1) SPACE COMPLEXITY
DATE:
QUESTION:
OR in other words,
Given 2 sorted arrays, find all the elements which occur in both the arrays.
Input Format
· The first line contains T, the number of test cases. Following T lines contain:
Output Format
Example
Input:
3 10 17 57
6 2 7 10 15 57 246
Output:
10 57
Input:
6123456
216
Output:
16
For example:
Input Result
1 10 57
3 10 17 57
Input Result
2 7 10 15 57 246
PROGRAM:
#include <stdio.h>
int i = 0, j = 0;
i++;
j++;
} else {
i++;
j++;
printf("\n");
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n1);
int arr1[n1];
scanf("%d", &arr1[i]);
scanf("%d", &n2);
int arr2[n2];
scanf("%d", &arr2[i]);
return 0;
}
OUTPUT:
EXP.NO:6(d)
PRINT INTERSECTION OF 2 SORTED ARRAYS-O(M+N)TIME
COMPLEXITY,O(1) SPACE COMPLEXITY
DATE:
QUESTION:
OR in other words,
Given 2 sorted arrays, find all the elements which occur in both the arrays.
Input Format
· The first line contains T, the number of test cases. Following T lines contain:
Output Format
Example
Input:
3 10 17 57
6 2 7 10 15 57 246
Output:
10 57
Input:
6123456
216
Output:
16
For example:
Input Result
1 10 57
Input Result
3 10 17 57
2 7 10 15 57 246
PROGRAM:
#include <stdio.h>
int i = 0, j = 0;
int found = 0;
i++;
j++;
} else {
i++;
j++;
if (!found) {
printf("No Intersection");
}printf("\n");
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n1);
int arr1[n1];
scanf("%d", &arr1[i]);
scanf("%d", &n2);
int arr2[n2];
scanf("%d", &arr2[i]);
return 0;
}
OUTPUT:
EXP.NO:6(e)
PAIR WITH DIFFERENCE-O(N^2)TIME COMPLEXITY,O(1) SPACE
COMPLEXITY
DATE:
QUESTION:
Given an array A of sorted integers and another non negative integer k, find if there exists 2
indices i and j such that A[j] - A[i] = k, i != j.
Input Format:
Output Format:
1 - If pair exists
0 - If no pair exists
YES as 5 - 1 = 4
So Return 1.
For example:
Input Result
3 1
135
PROGRAM:
#include <stdio.h>
int i = 0, j = 1;
!= j && diff == k) {
return 1;
j++;
else {
i++;
return 0;
int main() {
int n, k;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &k);
return 0;
}
OUTPUT:
EXP.NO:6(f)
LONGEST NON-DECREASING SUBSEQUENCE
DATE:
QUESTION:
Given an array A of sorted integers and another non negative integer k, find if there exists 2
indices i and j such that A[j] - A[i] = k, i != j.
Input Format:
Output Format:
1 - If pair exists
0 - If no pair exists
YES as 5 - 1 = 4
So Return 1.
For example:
Input Result
3 1
135
PROGRAM:
#include <stdio.h>
while (j < n) {
int diff = arr[j] - arr[i];
if (i != j && diff == k) {
return 1;
j++;
else {
i++;
if (i == j) {
j++;
return 0;
int main() {
int n, k;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
scanf("%d", &k);
return 0;
}
OUTPUT: