0% found this document useful (0 votes)
3K views10 pages

CC Solution Set-1

The document describes an algorithm to arrange flower sticks in a bouquet given by Emma to her father. It is given that Emma is given N flower sticks numbered from 1 to N. She must arrange the first K sticks in order of increasing length and the remaining sticks in order of decreasing length. The algorithm takes as input the number of sticks N, the value of K, and the length of each stick. It sorts the sticks in increasing order, prints the first K in increasing order of length, and prints the remaining sticks in decreasing order of length.

Uploaded by

Palanisamy R
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)
3K views10 pages

CC Solution Set-1

The document describes an algorithm to arrange flower sticks in a bouquet given by Emma to her father. It is given that Emma is given N flower sticks numbered from 1 to N. She must arrange the first K sticks in order of increasing length and the remaining sticks in order of decreasing length. The algorithm takes as input the number of sticks N, the value of K, and the length of each stick. It sorts the sticks in increasing order, prints the first K in increasing order of length, and prints the remaining sticks in decreasing order of length.

Uploaded by

Palanisamy R
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/ 10

Q1. In the city of Toyland, there are N houses.

Noddy is looking for a piece of land in the city


to build his house. He wants to buy the land where he can build the largest possible house.
All the houses in the city lie in a straight line and all of them are given a house number and
position of the house from the entry point in the city. Noddy wants to find the house numbers
between which he can build the largest house.

Input: The input to the function/method consists of two arguments

 numOf House, an integer representing the number of houses.


 houseList, a list where each element of the list is a list of integers representing the
house number and its position respectively.

Constraints
2 < numOfHouse < 106
1 <houseList[i][0] <numOfHouse
0 < houseList[i][1] < 106
0 < I < numOfHouse

Note: No two houses will have the same position. In case of multiple such answers, return the
one with the least distance from the reference point Zero.

Example:
Input:
numOfHouse = 5
houseList = [[3, 7],[1, 9],[2, 0],[5, 15],[4, 30]]
Output: [4, 5]

typedef struct BoundedArray


{
int size;
int *arr;
}boundedarray;
#include
struct barr
{
int hnum;
int hpos;
};
void find_land(int n, struct barr hse[])
{
int max,ind_s,ind_end,i;
if(hse[0].hpos>hse[1].hpos)
{ max=hse[0].hpos-hse[1].hpos;
ind_s=0;
ind_end=1;
}
else
{
max=hse[1].hpos-hse[0].hpos;
ind_s=0;
ind_end=1;
}
for(i=1;i<n-1;i++)
{
if(hse[i].hpos>hse[i+1].hpos)
{
if(max<=(hse[i].hpos-hse[i+1].hpos))
{
max=hse[i].hpos-hse[i+1].hpos;
ind_s=i;
ind_end=i+1;
}
}
else
{
if(max<=(hse[i+1].hpos-hse[i].hpos))
{
max=hse[i+1].hpos-hse[i].hpos;
ind_s=i;
ind_end=i+1;
}
}
}
if(hse[ind_s].hnum<hse[ind_end].hnum)
printf(“%d %d”,hse[ind_s].hnum,hse[ind_end].hnum);
else
printf(“%d %d”,hse[ind_end].hnum,hse[ind_s].hnum);
}
int main() {
int n,k,i;
scanf(“%d”,&n);
struct barr hse[n];
for(i=0;i<n;i++)
scanf(“%d %d”,&hse[i].hnum,&hse[i].hpos);
find_land(n,hse);
}

Q2. A water reservation system constructed in a city has several opening and closing gates. If
any opening gates are not closed with a corresponding closing gate then the water will leak
out of the system and there will be a threat to the life of people living in the city. Also, the
closing gate cannot exist without the opening gate, so the system head checks the design of
the system and he has to ensure that the people are safe in the city. Write an algorithm to find
whether people are safe or not.

Input:
The input to the function/method consists of one argument- str, a string representing the
sequence of gates of the water reservation system.
Output:
Return an integer representing the number of gates which have closing gates corresponding to
the opening gates else return an integer-1.

Constraints:
The opening gates are representing by “(“ and closing gates are representing “)”

Example 1
Input: Str =()()
Output: 3

Logic:
This problem is like the balanced parenthesis problem. Once a pair of parenthesis is found
(the pair should have an opening parenthesis and closing parenthesis), a counter is
incremented.

#include <stdio.h>
int top = -1,c=0;
char stack[100];

// function prototypes
void push(char);
void pop();
void find_top();
int main()
{
int i;
char a[100];
printf(“enter expression\n”);
scanf(“%s”, &a);
for (i = 0; a[i] != „\0‟;i++)
{
if (a[i] == „(„)
{
push(a[i]);
}
else if (a[i] == „)‟)
{
pop();
}
}
find_top();
return 0;
}

// to push elements in stack


void push(char a)
{
stack[top] = a;
top++;
}

// to pop elements from stack


void pop()
{
if (top == -1)
{
printf(“-1”);
exit(0);
}
else
{
top–;
c++;
}
}

// to find top element of stack


void find_top()
{
if (top == -1)
printf(“%d”,c);
else
printf(“-1”);
}

Q3. Given weights and values of n items, the task is to put these items in a knapsack of
capacity W to get the maximum total value in the knapsack. In this problem, 0-1 means that
we can either put the complete item in the knapsack or ignore it.

Consider the following example,

Input:
Number of items:3
value and weight of items:
100 20
50 10
150 30
Size of the knapsack:50

Output:
Maximum total value in the knapsack:250

Explanation:

Weight = 20, value = 100


Weight = 10, value = 50
Weight = 30, value = 150
Weight = (20 + 10), Value = (100 + 50)
Weight = (20 + 30), Value = (100 + 150)
Weight = (10 + 30), Value = (50 + 150)
Weight = (20 + 10 + 30) > 50

The maximum among these is


Weight = (20 + 30), Value = (100 + 150)
Weight = 50, Value = 250

Solution 1:

#include<stdio.h>

/* Function to find maximum of two integers */


int max(int a, int b) { return (a > b)? a : b; }

/* Function to find the maximum value that can be put in a knapsack of capacity W */
int knapSack(int W, int weight[], int value[], int n)
{
if (n == 0 || W == 0) /* Base Case */
return 0;

/* If weight of the nth item is more than Knapsack capacity W */


if (weight[n-1] > W)
return knapSack(W, weight, value, n-1); /* this item cannot be included in the optimal
solution */

else return max( value[n-1] + knapSack(W-weight[n-1], weight, value, n-1), knapSack(W,


weight, value, n-1));
}

int main()
{
int n;
printf(“\nEnter the number of items : “);
scanf(“%d”, &n);
int value[n];
int weight[n];
int i;
printf(“\nEnter the item‟s weight and its value \n”);
for(i = 0; i < n; i++)
{
scanf(“%d %d”, &weight[i], &value[i]);
}
int W;
printf(“\nEnter the capacity of the knapsack : “);
scanf(“%d”, &W);
printf(“\nMaximum value in a 0-1 knapsack : %d\n”, knapSack(W, weight, value, n));
return 0;
}
Solution 2:

/* C program – Dynamic Programming implementation of 0-1 Knapsack problem */


#include<stdio.h>

/* Function to find maximum of two integers */


int max(int a, int b) { return (a > b)? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W


int knapSack(int W, int weight[], int value[], int n)
{
int i, w;
int K[n+1][W+1];

/* Build table K[][] in bottom up manner */


for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (weight[i-1] <= w)
K[i][w] = max(value[i-1] + K[i-1][w-weight[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}

int main()
{
int n;
printf(“\nEnter the number of items : “);
scanf(“%d”, &n);
int value[n];
int weight[n];
int i;
printf(“\nEnter the item‟s weight and its value \n”);
for(i = 0; i < n; i++)
{
scanf(“%d %d”, &weight[i], &value[i]);
}
int W;
printf(“\nEnter the capacity of the knapsack : “);
scanf(“%d”, &W);
printf(“\nMaximum value in a 0-1 knapsack : %d\n”, knapSack(W, weight, value, n));
return 0;
}
Q4. Program to find the total number of islands using DFS is discussed here. Given an input
island matrix, where 0 represents water and 1 represents land. Find the total number of
islands that are formed by connected 1‟s.

For example, consider the input island matrix

10101
00100
00110
01010
11100
00001
01010
00110
00011
11000

Total number of islands = 5

/* C program to find the number of islands using DFS */


#include

/*checks whether the given index is out of the array. Out of array is considered as 0 */
int isSafe(int N,int M,int i,int j)
{
if(i < 0 || i >= N)
return 0;
if(j < 0 || j >= M)
return 0;
return 1;
}

/* this function does the DFS for every new one “1” found and assign every connected one to
zero */
void delIsland(int **A,int N,int M,int i,int j)
{
if(isSafe(N,M,i,j) && A[i][j] == 1)
{
A[i][j] = 0;
delIsland(A, N , M , i-1, j-1);
delIsland(A, N, M, i-1, j);
delIsland(A, N, M, i-1, j+1);
delIsland(A, N, M, i, j-1);
delIsland(A, N, M, i, j+1);
delIsland(A, N, M, i+1, j-1);
delIsland(A, N, M, i+1, j);
delIsland(A, N, M, i+1, j+1);
}
}

/* this function finds the total number of islands and return count – 1 as total number of
bridges */
int TotalBridge(int **A, int N, int M)
{
int count = 0 ;
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
if(A[i][j] == 1)
{
count++;
delIsland(A,N,M,i,j);
}
}

if(count == 0)
return 0;
return count;
}

/* get the input and calls the function */


int main()
{
int N,M;
int i,j;
printf(“\nEnter the order of the islands : “);
scanf(“%d %d”, &N,&M);
int **A = (int **)malloc(N * sizeof(int *));
for (i = 0; i < N; i++)
A[i] = (int *)malloc(M * sizeof(int));

printf(“\nEnter the island matrix : \n”);


for(i = 0; i < N ; i++)
for(j = 0; j < M; j++)
scanf(“%d”,&A[i][j]);
printf(“\nTotal Number of islands : %d\n”, TotalBridge(A,N,M));
return 0;
}

Q5. Emma wants to gift a bouquet to her father on his birthday and asked for help from
her mother Rosy. Rosy gives N flower sticks numbered 1 to N to Emma and tells her to
arrange it in the bouquet in a particular order. She asks her to arrange the first K flower sticks
in the order of their increasing length and the remaining sticks in an order of their decreasing
length.

Write an algorithm to find the final arrangement of the flower sticks in which Emma gifted
the bouquet to her father.

Input:
The input to the function/method consists of three arguments.

 num, an integer representing the number of flower sticks (N).


 random, an integer representing the number K given by Rosy to Emma sticks
 a list of integers representing the length of flower sticks.

Output: Return a list of integers representing the final pattern of the flower sticks in which
Emma gifted the bouquet to her father

Constraints:
Random < num
0 < num < 106

// Arranging of flower sticks in a boquet – C code

#include
void arrange(int n, int k, int arr[])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<k;i++)
printf(“%d “,arr[i]);
for(i=n-1;i>=k;i–)
printf(“%d “,arr[i]);
}
int main()
{
int n,k,i;
int arr[20];
printf(“\nEnter the values of n and k : “);
scanf(“%d%d”,&n,&k);
printf(“\nEnter all the elements:\n “);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
arrange(n,k,arr);
}

You might also like