[go: up one dir, main page]

0% found this document useful (0 votes)
27 views49 pages

SET A - Coding Questions - MEDIUM - VVCE

SET A - Coding Questions - MEDIUM_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)
27 views49 pages

SET A - Coding Questions - MEDIUM - VVCE

SET A - Coding Questions - MEDIUM_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/ 49

CODING – MEDIUM LEVEL

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;

int numberOfCarries(int num1 , int num2)


{
int carry = 0, sum, p, q, count = 0;
while((num1!=0)&&(num2!=0))
{
p = num1 % 10;
q = num2 % 10;
sum = carry + p + q;
if(sum>9)
{
carry = 1;
count++;
}

else
{
carry = 0;

}
num1 = num1/10;
num2 = num2/10;
}
return count;
}
int main()
{
int x, y, a;

cin >> x >> y;

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;

void ReplaceCharacter(char str[], int n, char ch1, char ch2)


{

5
int i;
for(i=0; i<n ; i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
}
else if(str[i]==ch2)
{
str[i]=ch1;
}
}

cout << str;


}
int main()
{
char a[100];
char b, c;
int len;
cin >> a >> b >> c;
len = strlen(a);
ReplaceCharacter(a, len, b, c);
return 0;
}

6
Program 4

Second greatest element


Write a program to print the second greatest element in the given array. In case, if the second largest element is
not present in the array, print "There is no second largest element" and if the size of the array is less than 2, print
"Invalid Input".

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>

void print2largest(int arr[], int arr_size)


{
int i, first, second;
if (arr_size < 2)
{
cout<<"Invalid Input";
return;
}
first = second = INT_MIN;
for (i = 0; i < arr_size ; i ++)

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

Most occurring character


Write a program to find the most occurring character in the string.

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 result (int arr[], int x, int n,int m);

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;
}

int result (int arr[], int x, int n,int m)


{
int i;
for (i = 0; i < x; i++)
{
if ( (arr[i]-n ) < m)
{
arr[i]=arr[i]+1;
}
}
}

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 search (int arr[], int ele, int n);

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;
}

int search (int arr[], int ele, int n)


{
int i, flag = 0;

for (i = 0; i < n; i++)


{
if (ele == arr[i])
{
cout<<"index = "<< i;
flag = 1;
break;
}
}

if (flag == 0)
{
cout<<"-1";
}
}

14
Program 8

Decimal to Binary equivalent


Write a program to convert decimal number to binary equivalent number.

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

Binary to decimal equivalent


Write a program to convert a binary number to an equivalent decimal value.

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

Refer the sample output for formatting


Sample Input:
5
12345
2 10
Sample Output:
1 10 2 3 4 5

• 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;

for (i = n - 1; i >= position - 1; i--)


array[i+1] = array[i];

array[position-1] = value;

for (i = 0; i <= n; i++)


cout<<array[i]<<" ";
return 0;
}

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 countExp(int i){


int count=0;
while((i%2==0) && (i!=0)){
count+=1;
i=i/2;
}
return count;
}

int maxExp(int a, int b){


int max = -1,num=0,temp;
for(int i=a;i<=b ;i++){
temp=countExp(i);
if(temp>max){
max=temp;
num=i;
}
}
return num;
}

int main() {
int a,b;
cin>>a;
cin>>b;
cout<<maxExp(a,b);
return 0;
}

20
Program 12

Intermediate palindrome numbers


Write a program that it takes a lower limit and upper limit as inputs and print all the intermediate palindrome
numbers.

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

Delete at specific position


Write a program to create a dynamic array and delete an element from an array from the specified position. If
deletion is not possible, print "Deletion not possible.".
Sample Input:
5
12345
2
Sample Output:
1345

• 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);

if ( position >= n+1 )


printf("Deletion not possible.\n");

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

Segregate 0's and 1's


Write a program to segregate all the 0’s in the left side and 1’s in the right side in the same array.

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;

void segregate0and1(int arr[],int n)


{
int left = 0, right = n-1;
while(1)
{
if(left >= right)
break;
if(arr[left] == 0)
left++;
else if(arr[right] == 1)
right--;
else
{

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

Refer the sample output for formatting


Sample Input:
6
123433
Sample Output:
123334

• 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

Refer the sample output for formatting


Sample Input:
a+b+c
aaabbc
Sample Output:
Matched

• 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++;
}
}

if(i>=len && ans[j]=='\0')


{
cout<<"Matched";
}
else
{
cout<<"Not Matched";
}
return 0;
}

32
Program 18

Implement the following Function


def differenceofSum(n. m)
The function accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both
inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of
numbers divisible by n.
Assumption:
• n>0 and m>0
• Sum lies between integral range
Example
Input
n:4
m:20
Output
90
Explanation
• Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60
• Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 + 18 + 19 =
150
• Difference 150 – 60 = 90
Sample Input
n:3
m:10
Sample Output
19

#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

You are required to implement the following Function


def LargeSmallSum(arr)
The function accepts an integers arr of size ’length’ as its arguments you are required to return the sum of second
largest largest element from the even positions and second smallest from the odd position of given ‘arr’

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>;

int largeSmallSum(int *array, int n)


{
int answer, i, j, temp;;
int even[n], odd[n];
int evencount = 0, oddcount = 0;
if(n<=3)
{
answer = 0;
}

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

Implement the following Function


def ProductSmallestPair(sum, arr)
The function accepts an integers sum and an integer array arr of size n. Implement the function to find the pair,
(arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k] are the least two elements of array (arr[j] + arr[k] <= sum)
and return the product of element of this pair
NOTE
• Return -1 if array is empty or if n<2
• Return 0, if no such pairs found
• All computed values lie within integer range

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>;

int productSmallestPair(int *array, int n, int sum)


{
int answer, temp, i, j, check;
if(n<=2)
{
answer = -1;
}
else
{
for(i=0; i<n; i++) //sorting of array
{

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];

printf("enter the length of array : ");


scanf("%d",&length);

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;
}

for(i=0;i<length;i++) // seperation of even and odd array


{
if(i%2==0)
{
even[i/2] = arr[i];
}
else
{
odd[i/2] = arr[i];
}
}

for(i=0; i<evenlen-1; i++) // sorting of even array


{
for(j=i+1; j<evenlen; j++)
{
temp = 0;
if(even[i]>even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}

for(i=0; i<oddlen-1; i++) // sorting of odd array


{
for(j=i+1; j<oddlen; j++)
{
temp = 0;
if(odd[i]>odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}

printf("\nSorted even array : "); // printing even array


for(i=0;i<evenlen;i++)

41
{
printf("%d ",even[i]);
}

printf("\n");

printf("Sorted odd array : "); // printing odd array


for(i=0;i<oddlen;i++)
{
printf("%d ",odd[i]);
}

printf("\n\n%d",even[evenlen-2] + odd[1]); // printing final result


}

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;

void fun (ll size,ll k,string s, ll l, ll *ingame)


{
if(size==1)
{
ll i=1;
while(ingame[i]!=1)
{i++;

}
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

Write a program to find the equilibrium element in the given array.


Hint : A[i] is equilibrium element if A[0] + A[1] + … A[i-1] == A[i+1] + A[i+2] + … + A[n-1]

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;

int findElement(int arr[], int n)


{
int prefixSum[n];
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++)
prefixSum[i] = prefixSum[i - 1] + arr[i];

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

Move hyphen to the beginning


Implement the following function: char* MoveHyphen(char str[], int n); The function accepts a string ‘str’ of
length ‘n’, that contains alphabets and hyphens (-). Implement the function to move all hyphens (-) in the string to
the front of the given string. Note: Return null if str is null.

Input Format
Given input is in given string

Output Format
Output is in string format

Refer the sample output for formatting


Sample Input:
Move-Hyphens-to-Front
Sample Output:
---MoveHyphenstoFront

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

Non repeating elements


Write a program to eliminate the common elements in the given 2 arrays and print only the non repeating
elements and the total.

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

You might also like