Set W - Vvce
Set W - Vvce
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.*;
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)
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:
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.
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: