0% found this document useful (0 votes)
68 views

Set W - Vvce

SET W_VVCE

Uploaded by

Purushotham
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)
68 views

Set W - Vvce

SET W_VVCE

Uploaded by

Purushotham
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/ 20

W SET 1

Program 1_MEDIUM LEVEL


1. 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 “)”
Input: Str =()()
Output: 3

Solution in C:
#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”);
}
Solution in C++:
#include<iostream>
#include<string.h> //to use strlen(), we need to include this header
using namespace std;
char st[20];
int top=-1;

void push(char a)
{
st[++top]=a;
}
char pop()
{
return st[top–];
}
int main()
{
char a[20],t;
int i,f=1;
int count=0;
cout<<“Enter the String: “;
cin>>a;
for(i=0;i<strlen(a);i++)
{
if(a[i]=='(‘)
push(a[i]);
if(a[i]==’)’)
{
if(top==-1)
f=0;
else
{
t=pop();
if(t=='(‘)
count++;
}
}
}

if(top>=0)
f=0;
if(f==0)
cout<<“-1”;
else
cout<<“Count: “<<count;
return 0;
}

Solution in JAVA:
import java.util.Scanner;
import java.util.*;

public class Main


{
public static int isParenthesisMatch(String str)
{
Stack<Character> stack = new Stack<Character>();
int count = 0;
char c;
for(int i=0; i < str.length(); i++)
{
c = str.charAt(i);
if(c == ‘(‘)
stack.push(c);

if(c == ‘)’)
{
if(stack.empty())
{
count = -1;
return count;
}
else if(stack.peek() == ‘(‘)
{
count++;
stack.pop();
}
else
return count;
}
}
return count;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the string: “);
String str = sc.nextLine();
System.out.println(isParenthesisMatch(str));
}
}

Solution in Python:
s = input(“Enter the string: “)
flag = 1
count = 0
stack = []
for x in s:
if x == ‘(‘:
stack.append(x)
if x == ‘)’:
if stack == []:
flag = 0
break
t = stack.pop()
if t != ‘(‘:
flag = 0
break
else:
count = count+1
if flag == 1:
print(“Count: %d” %(count))
else:
print(“-1”)

Output:
Program 2_MEDIUM LEVEL

Bob has to send a secret code S to his boss. He designs a method to encrypt the code using two key values
N and M. The formula that he uses to develop the encrypted code is shown below:
(((S^N%10)^M)%1000000007)

Write an algorithm to help Bob to encrypt the code.


Input: The input to the function/method consists of three arguments secret code, an integer
representing the secret code (S) value N, an integer representing the key value N and value M, an integer
representing the key value M.
Output: Return an integer representing the code encrypted by BOB.

Constraints:
1<=secret code<=10^9
0<=value N<=10^10
0<=value M<=10^10

Solution in C:
#include<stdio.h>
int main()
{
long int s,n,m,ans;
scanf(“%ld %ld %ld”,&s,&n, &m);
ans=pow(s,n);
ans=ans%10;
ans=pow(ans,m);
ans=ans%1000000007;
printf(“%ld”,ans);
return 0;
}

Solution in C++:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
unsigned long long int S, N, M;
unsigned long long int ans;
cout<<“Enter the values of S, N, M:”<<endl;
cin>>S>>N>>M;
ans = pow(S,N);
ans = ans%10;
ans = pow(ans,M);
ans = ans%1000000007;
cout<<“Answer: “<<ans;
return 0;
}

Solution in JAVA:
import java.util.Scanner;
import java.math.BigInteger;
public class Main
{
public static void main(String[] args)
{
BigInteger S, N, M, ans;
System.out.println(“Enter the values of S, N, M: “);
Scanner sc = new Scanner(System.in);
S = sc.nextBigInteger();
N = sc.nextBigInteger();
M = sc.nextBigInteger();
BigInteger b1, b2;
b1 = new BigInteger(“10”);
b2 = new BigInteger(“1000000007”);
ans = S.pow(N.intValue());
ans = ans.mod(b1);
ans = ans.pow(M.intValue());
ans = ans.mod(b2);
System.out.println(ans);
}
}

Solution in Python:
S = int(input(“Enter the value of S: “))
N = int(input(“Enter the value of N: “))
M = int(input(“Enter the value of M: “))
ans = S ** N
ans = ans%10
ans = ans ** M
ans = ans % 1000000007
print(ans)

Output:

Program 3_DIFFICULT LEVEL


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

Solution in C:
// 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);
}

Solution in C++:
// Arranging of flower sticks in a boquet – C++ code
#include
using namespace std;
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]) //Bubble sort is performed
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<k;i++) //Printing the elements till kth element in the sorted order
cout<<arr[i]<<” “;
for(i=n-1;i>=k;i–) //Printing the elements from (k+1)th element till nth element in the reverse order
cout<<arr[i]<<” “;
}
int main()
{
int n,k,i;
int arr[20];
cout<<“Enter the values of n and k : “<<endl;
cin>>n>>k;
cout<<“Enter the values of the array:”<<endl;
for(i=0;i<n;i++)
cin>>arr[i];
arrange(n,k,arr);//Calling the function
return 0;
}

Solution in JAVA:
// Arranging of flower sticks in a boquet – Java code
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n,k;
System.out.println(“Enter the values of n and k: “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
int arr[] = new int [n];
System.out.println(“Enter all the elements:”);
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
arrange(n,k,arr);
}
private static void arrange(int n, int k, int arr[])
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i=0;i<k;i++)
System.out.print(arr[i]);
for(int i=n-1;i>=k;i–)
System.out.print(arr[i]);
}
}

Solution in Python:
# Arranging of flower sticks in a boquet – Python code
n = int(input(“Enter the value of n: “))
k = int(input(“Enter the value of k: “))
arr = []
a = []
print(“Enter the values of the array: “)
for i in range (n):
arr.append(input())
arr.sort()
for i in range (0,k):
t = arr[0]
a.append(t)
del arr[0]
arr.sort(reverse = True)
for i in range (0,n-k):
t = arr[0]
a.append(t)
del arr[0]
print(a)

Output:
Program 4_DIFFICULT LEVEL
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.
Write an algorithm to help Noddy to find the house numbers between which he can build his 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]

Explanation: The largest land area with size 15 is available between the 4 and 5 numbered houses. So
the output contains these house number s in ascending order.

Solution: The following structure is used to represent a bounded array, and is already implemented in
the default code (Do not write this definition again in your code )
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);
}

Solution in C++:
// C++ code to build the largest house
#include <bits/stdc++.h>
using namespace std;
int main()
{
int num;
cout<<“Enter the number of houses: “;
cin>>num;
int house_number[num];
int position[num];
cout<<“Enter the house number and position of the house: “<< endl;
for(int i =0; i<num; i++)
{
cin>>house_number[i]; //Getting the house number as a single array
cin>>position[i]; //Getting the position as a single array
}
cout<<“Input: “<<endl;
for(int i=0; i<num; i++)
{
cout<<“[“<<house_number[i]<<“,”<<position[i]<<“] “;
}
cout<<endl;
int copy_position[num];
for(int i=0; i<num; i++)
{
copy_position[i] = position[i]; //Copying the position to another array so that the orginial array won’t
be affected
}
sort(copy_position, copy_position+num); //Sorting the copied array
int temp;
int x1, x2;
int position1, position2;
int maxi =0;
for(int i=0; i<num-1; i++)
{
int temp = abs(copy_position[i+1]-copy_position[i]); //Difference between two adjacent array
elements is calculated
if(temp > maxi)
{
maxi = temp; //The highest difference is noted
x1 = copy_position[i]; //The two elements are noted
x2 = copy_position[i+1];
}
}
for(int i=0; i<num; i++) //The elements position are found in the main position array
{
if(x1 == position[i])
{
position1 = i;
}
else if (x2 == position[i])
{
position2 = i;
}
}
if(house_number[position1]>house_number[position2]) //The house number is displayed which is
matched by the position obtained
{
cout<<“Result: “<<“[“<<house_number[position2]<<“,”<<house_number[position1]<<“] “;
}
else
{
cout<<“Result: “<<“[“<<house_number[position1]<<“,”<<house_number[position2]<<“] “;
}
return 0;
}

Solution in JAVA:
// Java code to build the largest house
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int num;
int temp;
int x1=0;
int x2=0;
int position1=0;
int position2=0;
int maxi=0;
System.out.println(“Enter the number of houses: “);
num = sc.nextInt();
int house_number[] = new int[num];
int position[] = new int[num];
int copy_position[] = new int[num];
System.out.println(“Enter the house number and position of the house: “);
for(int i=0; i<num; i++)
{
house_number[i] = sc.nextInt();
}
System.out.println(“Input: “);
for(int i=0; i<num; i++)
{
System.out.print(“[” + house_number[i] + “,” + position[i] + “] “);
}
System.out.println(“”);
for(int i=0; i<num; i++)
{
copy_position[i] = position[i];
}
Arrays.sort(copy_position);
for(int i=0; i<num-1;i++){
temp = copy_position[i+1] – copy_position[i];
if(temp>maxi)
{
maxi = temp;
x1 = copy_position[i];
x2 = copy_position[i+1];
}
}
for(int i=0; i<num; i++) //The elements position are found in the main position array
{
if(x1 == position[i])
{
position1 = i;
}
else if (x2 == position[i])
{
position2 = i;
}
}
if(house_number[position1]>house_number[position2]) //The house number is displayed which is
matched by the position obtained
{
System.out.println(“Result: [” + house_number[position2] + “,” + house_number[position1] + “] “);
}
else
{
System.out.println(“Result: [” + house_number[position1] + “,” + house_number[position2] + “] “);
}
}
}

Solution in Python:
// Python code to build the largest house
num = int(input(“Enter the number of houses: “))
x1=0
x2=0
position1=0
position2=0
maxi=0
house_number = []
position = []
copy_position = []
print(“Enter the number of houses and position of the houses: “)
for i in range(num):
house_number.append(int(input(“House Number:”)))
position.append(int(input(“Position:”)))
print(“Input”)
for i in range(num):
print (“[%d,%d]” %(house_number[i], position[i]))
for i in range(num):
copy_position.append(position[i])
copy_position.sort()
print(copy_position)
maxi=0
for i in range(num-1):
temp = copy_position[i+1] – copy_position[i]
if(temp > maxi):
x1 = copy_position[i]
x2 = copy_position[i+1]
for i in range(num):
if x1 == position[i]:
position1 = i
if x2 == position[i]:
position2 = i
print(“Result: “)
if house_number[position1] > house_number[position2]:
print (“[%d,%d]” %(house_number[position2], house_number[position1]))
else:
print (“[%d,%d]” %(house_number[position1], house_number[position2]))

Output:

You might also like