SET A - Coding Questions - MEDIUM - VVCE
SET A - Coding Questions - MEDIUM - VVCE
Program 1
Robert is expert in strings where he challenges everyone to write a program for the below implementation.
Two strings and comprising of lower case English letters are compatible if they are equal or can be made equal by
following this step any number of times:
Select a prefix from the string (possibly empty), and increase the alphabetical value of all the characters in the
prefix by the same valid amount.
For example if the string is abc and we select the prefix ab then we can convert it to bcc by increasing the
alphabetical value by 1. But if we select the prefix abc then we cannot increase the alphabetical value.
Your task is to determine if given strings and are compatible.
Input format
First line: String A
Next line: String B
Output format:
For each test case, print YES if string can be converted to string , otherwise print NO.
Constrain:
1<=(len of A,B)<1000005
Sample Input:
abaca
cdbda
Sample Output:
YES
Explanation:
The string abaca can be converted to bcbda in one move and to cdbda in the next move.
Test Case 1
Input
abaca cdbda
Output
YES
1
Test Case 2
Input
abcda abcda
Output
NO
Solution:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[1000005];
char str2[1000005];
cin>>str1>>str2;
int max = str2[0] - str1[0];
if(max > 0)
{
for(int i =1;i<strlen(str1);i++)
{
int x = str2[i] - str1[i];
if(x > max)
{
cout<<"NO";
return 0;
}
}
cout<<"YES";
}
else
cout<<"NO";
return 0;
}
2
Program 2
Carry digit
Problem Statement
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two numbers from right-to-left
one digit at a time
You are required to implement the following function.
Int NumberOfCarries(int num1 , int num2);
The functions accepts two numbers ‘num1’ and ‘num2’ as its arguments. You are required to calculate and
return the total number of carries generated while adding digits of two numbers ‘num1’ and ‘ num2’.
Assumption: num1, num2>=0
Example:
Input
Num 1: 451
Num 2: 349
Output
2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10,
again 1 is carried. Hence 2 is returned.
Sample Input
Num 1: 23
Num 2: 563
Sample Output
0
• Case 1
Input (stdin)
23 563
Output (stdout)
0
• Case 2
Input (stdin)
123 463
Output (stdout)
0
#include<iostream>
3
using namespace std;
else
{
carry = 0;
}
num1 = num1/10;
num2 = num2/10;
}
return count;
}
int main()
{
int x, y, a;
a = numberOfCarries(x, y);
cout << a;
return 0;
}
4
Program 3
Replace character
Problem Statement
You are given a function,
Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);
The function accepts a string ‘ str’ of length n and two characters ‘ch1’ and ‘ch2’ as its arguments . Implement the
function to modify and return the string ‘ str’ in such a way that all occurrences of ‘ch1’ in original string are
replaced by ‘ch2’ and all occurrences of ‘ch2’ in original string are replaced by ‘ch1’.
Assumption: String Contains only lower-case alphabetical letters.
Note:
• Return null if string is null.
• If both characters are not present in string or both of them are same , then return the string unchanged.
Example:
Input:
Str: hello
ch1:e
ch2:o
Output:
holle
Explanation:
‘e' in original string is replaced with ‘o’ and ‘o’ in original string is replaced with ‘e’, thus output is holle.
• Case 1
Input (stdin)
hello e o
Output (stdout)
holle
• Case 2
Input (stdin)
tamil a i
Output (stdout)
timal
#include<iostream>
#include<string.h>
using namespace std;
5
int i;
for(i=0; i<n ; i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
}
else if(str[i]==ch2)
{
str[i]=ch1;
}
}
6
Program 4
Sample Input:
5
12 5 7 3 90
Sample Output:
12
• Case 1
Input (stdin)
5
12 5 7 3 90
Output (stdout)
12
• Case 2
Input (stdin)
10
10 20 50 43 60 51 70 82 93 55
Output (stdout)
82
#include<iostream>
using namespace std;
#include <limits.h>
7
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
if (second == INT_MIN)
cout<<"There is no second largest element";
else
cout<<second;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
print2largest(arr, n);
return 0;
}
8
Program 5
sample Input:
Happy coding
Sample Output:
p
• Case 1
Input (stdin)
Happy coding
Output (stdout)
p
• Case 2
Input (stdin)
programming is good
Output (stdout)
p
#include<iostream>
using namespace std;
#include <string.h>
int main()
{
char str[100], result;
int i, len;
int max = -1;
int freq[256] = {0};
scanf("%[^\n]s",str);
len = strlen(str);
for(i = 0; i < len; i++)
{
freq[str[i]]++;
}
for(i = 0; i < len; i++)
{
if(max < freq[str[i]])
{
9
max = freq[str[i]];
result = str[i];
}
}
cout<<result;
return 0;
}
10
Program 6
Integer Difference
Given an array arr and two integer value n and m as input, take the first element and find the difference between
the first element and the integer value n. If the difference is less than m then increment that particular array
element. Do this for all the element in an array and print the final modified array as output.
Sample Input:
5
21457
32
Sample Output:
32557
• Case 1
Input (stdin)
5
21457
32
Output (stdout)
32557
• Case 2
Input (stdin)
5
12345
35
Output (stdout)
23456
#include<iostream>
using namespace std;
int main ()
{
int x, i, n,m;
cin>>x;
int arr[x];
11
for (i = 0; i < x; i++)
{
cin>>arr[i];
}
cin>>n;
cin>>m;
result (arr, x, n, m);
for(i=0;i<x;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
12
Program 7
Search an element
Write a program to search an element in an array and print its index value. If the element is not present, then print
-1.
Sample Input:
5
46728
7
Sample Output:
index = 2
• Case 1
Input (stdin)
5
46728
7
Output (stdout)
index = 2
• Case 2
Input (stdin)
5
46328
5
Output (stdout)
-1
#include<iostream>
using namespace std;
int main ()
{
int n, i, ele;
cin>>n;
int arr[n];
for (i = 0; i < n; i++)
{
cin>>arr[i];
13
}
cin>>ele;
search (arr, ele, n);
return 0;
}
if (flag == 0)
{
cout<<"-1";
}
}
14
Program 8
Sample Input:
10
Sample Output:
1010
• Case 1
Input (stdin)
10
Output (stdout)
1010
• Case 2
Input (stdin)
2
Output (stdout)
10
#include<iostream>
using namespace std;
int main()
{
int a[50],n,i=0;
cin>>n;
while(n>0)
{
a[i++]=n%2;
n=n/2;
}
for(i=i-1;i>=0;i--)
cout<<a[i];
return 0;
}
15
Program 9
Sample Input:
1010
Sample Output:
10
• Case 1
Input (stdin)
1010
Output (stdout)
10
• Case 2
Input (stdin)
11111
Output (stdout)
31
#include<iostream>
using namespace std;
int main()
{
int num, binary, decimal = 0, base = 1, rem;
cin>>num;
binary = num;
while (num > 0)
{
rem = num % 10;
decimal = decimal + rem * base;
num = num / 10 ;
base = base * 2;
}
cout<<decimal;
return 0;
}
16
Program 10
Insert an element
Write a program to create a dynamic array and insert an element in it, in the specified position.
Input Format
First line consists of integer value N
Second line consists of an array elements
Third line consists of integer value as position and value
Output Format
Output consists of an array element
• Case 1
Input (stdin)
5
12345
2 10
Output (stdout)
1 10 2 3 4 5
• Case 2
Input (stdin)
5
17845
2 20
Output (stdout)
1 20 7 8 4 5
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
17
int *array, position, i, n, value;
cin>>n;
array = (int*) malloc(n * sizeof(int));
if(array == NULL)
{
cout<<"Error! memory not allocated.";
exit(0);
}
for(i = 0; i < n; ++i)
{
cin>>array[i];
}
cin>>position>>value;
array[position-1] = value;
18
Program 11
Maximum Exponent
You are given a function,
Int MaxExponents (int a , int b);
You have to find and return the number between ‘a’ and ‘b’ ( range inclusive on both ends) which has the
maximum exponent of 2.
The algorithm to find the number with maximum exponent of 2 between the given range is
1. Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
2. Find the exponent (power) of 2 for each ‘i’ and store the number with maximum exponent of 2 , let say
‘max’. Set ‘max’ to ‘i’ only if ‘i’ has more exponent of 2 than ‘max’.
3. Return ‘max’.
Assumption: a <b
Note: If two or more numbers in the range have the same exponents of 2 , return the small number.
Example
Input:
7
12
Output:
8
Explanation:
Exponents of 2 in:
7-0
8-3
9-0
10-1
11-0
12-2
Hence maximum exponent if two is of 8.
• Case 1
Input (stdin)
7
12
Output (stdout)
8
• Case 2
19
Input (stdin)
4
20
Output (stdout)
16
#include <iostream>
using namespace std;
int main() {
int a,b;
cin>>a;
cin>>b;
cout<<maxExp(a,b);
return 0;
}
20
Program 12
Test Cases:
TestCase 1:
Input :
10 80
Expected Result:
11 22 33 44 55 66 77
Test Case 2:
Input:
100 200
Expected Result:
101 111 121 131 141 151 161 171 181 191
• Case 1
Input (stdin)
10
80
Output (stdout)
11 22 33 44 55 66 77
• Case 2
Input (stdin)
100
200
Output (stdout)
101 111 121 131 141 151 161 171 181 191
#include<iostream>
using namespace std;
int reverse(int);
int main()
{
int i,f,l;
cin>>f;
cin>>l;
for (i = f; i <= l; i++)
21
{
if(i==reverse(i))
cout<<i<<" ";
}
return 0;
}
int reverse(int a)
{
int n=0,d=0,rev=0;
n = a;
while (n != 0)
{
d = n % 10;
rev = rev * 10 + d;
n = n / 10;
}
return rev;
}
22
Program 13
• Case 1
Input (stdin)
5
12345
2
Output (stdout)
1345
• Case 2
Input (stdin)
5
12345
5
Output (stdout)
1234
#include <stdio.h>
#include<stdlib.h>
int main()
{
int *array, position, i,c, n, value;
scanf("%d", &n);
array = (int*) malloc(n * sizeof(int));
if(array == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
for(i = 0; i < n; ++i)
{
23
scanf("%d", &array[i]);
}
scanf("%d", &position);
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
for( c = 0 ; c < n - 1 ; c++ )
printf("%d ", array[c]);
}
return 0;
}
24
Program 14
Sample Input:
5
01010
Sample Output:
00011
• Case 1
Input (stdin)
5
01010
Output (stdout)
00011
• Case 2
Input (stdin)
6
110000
Output (stdout)
000011
#include<iostream>
using namespace std;
25
arr[left] = 0;
arr[right] = 1;
left++;
right--;
}
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
segregate0and1(arr, n);
for(int i=0;i<n;i++)
cout<<arr[i];
return 0;
}
26
Program 15
Insertion sort
Write a program to create a dynamic array and sort the elements in it using Insertion sort.
Input Format
First line consists of integer value N
Second line consists of an array element
Output Format
Output consists of an array element
• Case 1
Input (stdin)
6
123433
Output (stdout)
123334
• Case 2
Input (stdin)
7
8125642
Output (stdout)
1224568
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n, i, j, *ptr, temp;
cin>>n;
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL)
27
{
cout<<"Error! memory not allocated.";
exit(0);
}
for(i = 0; i < n; ++i)
{
cin>>ptr[i];
}
for(i=1;i<n;i++)
{
temp = ptr[i];
j=i-1;
while(temp < ptr[j] && j>=0)
{
ptr[j+1] = ptr[j];
--j;
}
ptr[j+1]=temp;
}
for(i=0; i<n; i++)
cout<<ptr[i]<<" ";
free(ptr);
return 0;
}
28
Program 16
Sorting the first half of the array in ascending order and the second half in descending order
Write a program to sort the first half of an array in ascending order and the second half in descending order.
Input Format:
The first line contains an integer 'N', denoting the size of the array.
The next line contains space-separated integers denoting the elements of the array.
Output Format:
Print sorted array
Sample Input:
6
123456
Sample Output:
123654
• Case 1
Input (stdin)
9
524793168
Output (stdout)
245798631
• Case 2
Input (stdin)
6
10 30 20 40 60 50
Output (stdout)
10 20 30 60 50 40
#include<iostream>
using namespace std;
int insertion_sort(int n,int list[]);
int main()
{
int n;
cin>>n;
29
int list[n];
for(int index = 0;index < n; index++)
{
cin>>list[index];
}
insertion_sort(n,list);
return 0;
}
int insertion_sort(int n,int list[])
{
for (int idx1 = 1; idx1 < n/2; idx1++)
{
int key = list[idx1];
int idx2 = idx1 - 1;
while((idx2 >= 0) && (list[idx2] > key))
{
list[idx2 + 1] = list[idx2];
idx2--;
}
list[idx2 + 1] = key;
}
for (int idx1 = n/2; idx1 < n; idx1++)
{
int key = list[idx1];
int idx2 = idx1 - 1;
while((idx2 >= n/2) && (list[idx2] < key))
{
list[idx2 + 1] = list[idx2];
idx2--;
}
list[idx2 + 1] = key;
}
for(int i = 0; i < n; i++)
{
cout<< list[i]<<" ";
}
return 0;
}
30
Program 17
Regular Expression
Write a program to check the regular expression matching. Hint : ‘+’ indicates consecutive multiple occurrence of
that particular character.
Input Format
First line consists of a string s
Second line consists of string s
Output Format
Output consists of string whether it is matched or not
• Case 1
Input (stdin)
a+b+c
aaabbc
Output (stdout)
Matched
• Case 2
Input (stdin)
b+c+x
bbcxxz
Output (stdout)
Not Matched
#include<iostream>
using namespace std;
int main()
{
char arr[20],ans[20];
int i=0,j=0,len;
31
cin>>arr>>ans;
for(len=0;arr[len]!='\0';len++);
while(arr[i]==ans[j] && arr[i]!='\0')
{
if(arr[i+1]=='+')
{
for( ; ans[j]==arr[i] ; j++);
i=i+2;
}
else
{
i++;
j++;
}
}
32
Program 18
#includ<stdio.h>;
int differenceofSum(int n, int m)
{
int i, sum1 = 0, sum2 = 0;
for(i=1; i<=m; i++)
{
if(i%n==0)
{
sum1 = sum1 + i;
}
else
{
sum2 = sum2 + i;
33
}
}
return sum2 - sum1;
}
int main()
{
int n, m;
int result;
scanf("%d",&n);
scanf("%d",&m);
result = differenceofSum(n, m);
printf("%d",result);
return 0;
}
Input:
3
10
Output:
19
34
Program 19
Assumption:
• All array elements are unique
• Treat the 0th position a seven
NOTE
• Return 0 if array is empty
• Return 0, if array length is 3 or less than 3
Example
Input
arr:3 2 1 7 5 4
Output
7
Explanation
• Second largest among even position elements(1 3 5) is 3
• Second largest among odd position element is 4
• Thus output is 3+4 = 7
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
8
#include <stdio.h>;
35
else
{
even[0] = array[0];
evencount = 1;
for(i=1; i<n; i++) //creating two array even and odd
{
if(i%2==0)
{
even[evencount] = array[i];
evencount++;
}
else
{
odd[oddcount] = array[i];
oddcount++;
}
}
for(i=0; i<evencount; i++) //sorting of even array
{
for(j=i+1; j<evencount; j++)
{
if(even[i]>even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}
for(i=0; i<oddcount; i++) //sorting of odd array
{
for(j=i+1; j<oddcount; j++)
{
if(odd[i]>odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}
answer = even[evencount-2] + odd[1];
}
return answer;
}
36
int main()
{
int n, result, i;
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = largeSmallSum(array, n);
printf("%d",result);
return 0;
}
37
Program 20
Example
Input
sum:9
Arr:5 2 4 3 9 7 1
Output
2
Explanation
Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2
Sample Input
sum:4
Arr:9 8 3 -7 3 9
Sample Output
-21
#include<stdio.h>;
38
for(j=i+1; j<n; j++)
{
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
check = array[0] + array[1];
if(check<=sum)
{
answer = array[0] * array[1];
}
else
{
answer = 0;
}
}
return answer;
}
int main()
{
int n, sum, result, i;
scanf("%d",&sum);
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d",result);
return 0;
}
39
Program 21
Problem Statement
You are required to input the size of the matrix then the elements of matrix, then you have to divide the main
matrix in two sub matrices (even and odd) in such a way that element at 0 index will be considered as even and
element at 1st index will be considered as odd and so on. then you have sort the even and odd matrices in
ascending order then print the sum of second largest number from both the matrices
Example
• enter the size of array : 5
• enter element at 0 index : 3
• enter element at 1 index : 4
• enter element at 2 index : 1
• enter element at 3 index : 7
• enter element at 4 index : 9
Sorted even array : 1 3 9
Sorted odd array : 4 7
10
#include <stdio.h>
int main()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];
for(i=0;i<length;i++)
{
printf("Enter element at %d index : ",i);
scanf("%d",&arr[i]);
}
if(length%2==0)
{
oddlen = length/2;
evenlen = length/2;
}
else
{
oddlen = length/2;
40
evenlen = (length/2) + 1;
}
41
{
printf("%d ",even[i]);
}
printf("\n");
42
Program 22
In this lockdown a family of N members decided to play a game the rules of which are :-
• All N members are made to sit uniformly in a circle (ie. from 1 to N in clockwise direction).
• The game start with the person sitting at first position.
• A song is played in the background. The lyrics of the song are denoted by a string which consists of only
letters 'x' and 'y'. Assume that each lyric of the song is a single letter.
• If the lyric 'x' occurs in the song, the member who is currently holding the Parcel passes it on to the next
member. This passing takes place in clockwise direction.
• If the lyric 'y' occurs in the song, the member who is currently holding the Parcel loses his/her chances of
winning the game. He/she hands over the parcel to the next member (in clockwise direction) and moves
out.
• The game continues until a single member survives in the end. He/she will be the winner of the game.
• Note that the song repeats continuously ie. while the game is going on, if at all the song ends, the stereo
system will automatically start playing the song from the start without any delay.
You have to find out the member who wins the game.
Input :
The input consists of 2 lines. The first line consists of N, the member of family in the class. The next line consists
of a string denoting the lyrics of the song the teacher plays.
Output :
Print a single integer denoting the roll number of the student who wins the game.
Constraints :
2≤N≤100000
1≤|S|≤10000, where |S| denotes the length of the input string. It is guaranteed that at least 1 lyric in the song will
be a 'y'
Sample Input:
3
xyx
Sample Output:
1
Explanation:
Starting from 1 lyrics : 'x' therefore he passes the ballto 2nd
2nd turn lyrics : 'y' therefore 2nd member gets out of game and passes to 3rd
3rd turn lyrics : 'x' therefore 3rd passes ball to first.
4th turn lyrics : 'x' passes to 3rd
5th turn lyrics: 'y' therefore gets eliminated.
43
Hence person sitting at position 1 won this game.
Test Case 1
Input
3 xyx
Output
1
Test Case 2
Input
6 xxyyxy
Output
2
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define ll long long
ll n,slen;
}
cout<<i<<endl;
return;
}
if(l==slen) l=0;
if(k==n+1) k=1;
if(ingame[k]==0)
{
fun(size,k+1,s,l,ingame);
}
else
{
if(s[l]=='x')
44
fun(size,k+1,s,l+1,ingame);
else{
ingame[k]=0;
fun(size-1,k+1,s,l+1,ingame);
}
}
return;
}
int main()
{
cin>>n;
string s;
cin>>s;
slen=s.length();
ll ingame[n+1];
for(int i =1;i<=n;i++)
{
ingame[i]=1;
}
fun(n,1,s,0,ingame);
}
45
Program 23
Sample Input 1:
6
123433
Sample Output 1:
4
Sample Input 2:
6
564231
Sample Output 2:
No Equilibrium element found
Test Case 1
Input
6123433
Output
4
Test Case 2
Input
6564231
Output
No Equilibrium element found
#include<iostream>
using namespace std;
46
int suffixSum[n];
suffixSum[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--)
suffixSum[i] = suffixSum[i + 1] + arr[i];
for (int i = 1; i < n - 1; i++)
if (prefixSum[i] == suffixSum[i])
return arr[i];
return -1;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int result = findElement(arr, n);
if(result == -1)
cout<<"No Equilibrium element found";
else
cout<<result;
return 0;
}
47
Program 24
Input Format
Given input is in given string
Output Format
Output is in string format
Explanation:
The string “Move-Hyphens-to-Front” has 3 hyphens(-) which are moved to the front of the string thus output is “-
--MoveHyphenstoFront”.
• Case 1
Input (stdin)
Move-Hyphens-to-Front
Output (stdout)
---MoveHyphenstoFront
• Case 10
Input (stdin)
Hello-world-is
Output (stdout)
--Helloworldis
48
Program 25
Sample Input:
54
12865
2 6 8 10
Sample Output:
1 5 10
3
• Case 1
Input (stdin)
54
12865
2 6 8 10
Output (stdout)
1 5 10
3
• Case 2
Input (stdin)
55
10 20 30 40 50
60 70 80 90 50
Output (stdout)
10 20 30 40 60 70 80 90
8
49