Week 4 Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Assignment 4

1.Given two arrays of integers output the smallest number in the


first array not present in the second one.

Input Specification:
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array.

Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.

Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.

Example:
Input:
3
234
4
1357

Output: 2

Input
1
1
2
12
Output: NO

Sol. #include<stdio.h>
#define MAX 20
int read_array(int arr[])
{
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
return n;
}

int present(int arr[], int n, int elt) {


int i;
for (i = 0; i < n; i++) {
if (arr[i] == elt) {
return 1;
}
}
return 0;
}
int main() {
int arr1[MAX], n1;
int arr2[MAX], n2;
n1 = read_array(arr1);
n2 = read_array(arr2);
int i, small_np = 0, flag = 0;
for (i = 0; i < n1; i++) {
if (!present(arr2, n2, arr1[i])) {
if (!flag || (small_np > arr1[i]) ) {
flag = 1;
small_np = arr1[i];
}
}
}
if (flag) {
printf("%d", small_np);
} else {
printf("NO");
}
return 0;
}
2. There are 'n' ants on a 'n+1' length rod. The ants are numbered from 1 to n and are
initially placed at positions starting from position 1 till position n. They are moving either in
left direction (denoted by '-1') or in the right direction (denoted by '1'). Whenever an ant
crosses the boundary of the rod it falls off the rod. You are given the initial direction of the
ants. Now, whenever two ants collide their direction switches, i.e. the ant going in left
direction ('-1) changes it's direction towards right ('1') and the ant going in the right
direction ('1') changes it's direction towards left ('-1'). Find last ant to fall off the rod.
Note: In case two ants are falling simultaneously in the end print the index of the lower
indexed ant.

Input Format:
First line contains the integer 'n' denoting the total number of ants s.t. 1 <= n <= 1,000
Second line contains 'n' space separated numbers (either '1' or '-1') denoting the initial
directions of the ants.

Output Format:
Output a single integer which is the index (lower index in case two ants are falling
simultaneously in the end) of the last ant to fall off the table.
Sol. #include <stdio.h>
int main(){
// Number of ants
int n;
scanf("%d", &n);
int a[n];
int i;
// Directions of ants
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
int left = 0, right = 0;
// Number of contiguous ants at left edge and moving towards left
for(i=0; i<n; i++){
if(a[i] == -1){
left ++;
}
else{
break;
}
}
for(i=n-1; i>=0; i--){
if(a[i] == 1){
right++;
}
else{
break;
}
}

// Number of ants between 'left' and 'right' position and moving towards left
int leftgoingants = 0;
for(i=left; i <= n-right-1; i++){
if(a[i] == -1){
leftgoingants ++;
}
}
if(left >= right){
printf("%d", left + leftgoingants);
}
else{
printf("%d", left + leftgoingants + 1);
}
return 0;
}
3. Write a program that replaces the occurence of a given character (say c)
in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.

NOTE:
- There are no whitespaces in PS or s.
- Maximum length of PS is 100.
- Maximum length of s is 10.

Sol. #include<stdio.h>
int length(char *s)
{
int i=0;
while(s[i]!='\0')
{
i++;
}
return i;
}
void Replace(char *str, char *sub, int start , int end)
{
int i=-1;
int len = length(str);
char temp[100];
do
{
i++;
temp[i]=str[i];
} while(i!=len);
int sublen=length(sub);
int j = start;

for(i=0;i<sublen;i++)
{
str[j++]=sub[i];
}

for(i=end+1;i<=len;i++)
{
str[j++]=temp[i];
}
}
int charCheck (char * str , char a ,int i )
{
if(str[i]==a)
return i;
else
return -1;

}
int main()
{
char str[100] = {0};
int i =-1,start=0,end=0;
scanf("%s",str);
char c1;
scanf("\n%c",&c1);
char subs1[15];
scanf("%s",subs1);
for(i=0;i<length(str);i++)
{
int val=charCheck(str,c1,i);
if(val>=0)
{
start=i;
end=val;
Replace(str,subs1,start,end);
i=start+length(subs1);
}

}
printf("%s",str);
return 0;
}

You might also like