CC Solution Set-1
CC Solution Set-1
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]
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;
}
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.
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:
Solution 1:
#include<stdio.h>
/* 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;
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:
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.
10101
00100
00110
01010
11100
00001
01010
00110
00011
11000
/*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;
}
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.
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
#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);
}