## AMCAT Automata Questions with Solutions

AMCAT Automata Questions, AMCAT Programming Questions, AMCAT Coding Questions, AMCAT Automata Questions for Cognizant, AMCAT Pattern Programs, Automata Programming, AMCAT Java Coding Questions, AMCAT Automata Questions and Answers PDF, AMCAT Automata Programming Questions.

2. Pattern – 1 Easy Questions + 1 Medium/Difficult Questions

AMCAT automata section, is an additional topic that many companies ask for while giving aspiring minds test. Companies like Wipro, CTS etc use use this section as the last section of their written test paper.

Paid Papers AMCAT Previous Papers with Solutions PDF

#### AMCAT Automata Round Facts –

Infosys Automata Questions and AnswersMatrixArithmeticStringPatternDSA and Dynamic Programming
ImportanceMediumMediumHighHighLow
Questions201014417
ChancesHighLowMediumHighLow
DifficultyMediumMediumHighLowMedium

### AMCAT Automata Questions with Answers PDF

Question 1: Program to print the given 2D Array or Matrix in spiral form

Input:

1    2    3    4    5    6
7    8    9    10  11  12
13  14  15  16  17  18

In C

#include
#define R 3
#define C 6
void spiralPrint(int m, int n, int a[R][C])

int i, k = 0, l = 0;
/* k – starting row index
m – ending row index
l – starting column index
n – ending column index
i – iterator */
while (k < m && l < n)

/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)

printf(“%d “, a[k][i]);

k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)

printf(“%d “, a[i][n-1]);

n–;
/* Print the last row from the remaining rows */
if ( k < m)

for (i = n-1; i >= l; –i)

printf(“%d “, a[m-1][i]);

m–;

/* Print the first column from the remaining columns */
if (l < n)

for (i = m-1; i >= k; –i)

printf(“%d “, a[i][l]);

l++;

/* Driver program for the above functions */
int main()

int a[R][C] = { {1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}
};
spiralPrint(R, C, a);
return 0;

Output:

1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11

Question 2: Program to check if two given matrices are identical

In C Language

#include
#define N 4
// This function returns 1 if A[][] and B[][] are identical. Otherwise, it returns 0
int areSame(int A[][N], int B[][N])

int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if (A[i][j] != B[i][j]
return 0;
return 1;

int main()

int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int B[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
if (areSame(A, B))
printf(“Matrices are identical”);
else
printf(“Matrices are not identical”);
return 0;
}

The above is the program in C language.

In Java

The same program written in Java is given below:

package org.sb.twoMatrices;
import java.util.Scanner;
public class TwoMatricesAreEqualOrNot {
private static int size;
public static int checkMatrices(int A[][], int B[][]) {
int i = 0, j = 0;
for (i = 0; i <= size – 1; i++) {
for (j = 0; j <= size – 1; j++) {
if (A[i][j] != B[i][j])
return 0;
}
}
return 1;

public static void main(String[] args) {
Scanner sc = null;
int i = 0, j = 0;
int result = 0;
// create scanner object
sc = new Scanner(in);
System.out.println(“Please enter the size of array::”);
size = sc.nextInt();
sc.nextLine();
int A[][] = new int[size][size];
int B[][] = new int[size][size];
for (i = 0; i <= size – 1; i++) {
for (j = 0; j <= size – 1; j++) {
System.out.println(“Please enter Array A[” + i + “][” + j + “] value::”);
A[i][j] = sc.nextInt();
sc.nextLine();
System.out.println(“Please enter Array B[” + i + “][” + j + “] value::”);
B[i][j] = sc.nextInt();
sc.nextLine();
} // inner for
} // outer for
// invoke the checkMatrices method
result = checkMatrices(A, B);
if (result == 1)
System.out.println(“Matrices are identical..”);
else
System.out.println(“Matrices are not identical..”);
// close scanner
sc.close();
}// main
}// class

Question 3: A n-by-n matrix of 0’s and 1’s is given, where all 1’s in each row come before all 0’s, find the most efficient way to return the row with the maximum number of 0’s.

In C

#include
#include
#include
#define COL4
#define ROW4 using namespace std;
intmain()
{int arr[ROW][COL]= { {1,1,1,1}, {1,1,0,0}, {1,0,0,0}, {1,1,0,0}, };
intrownum;
inti = 0, j = COL-1;
while(i;0)

if(arr[i][j]==0)

j–;
rownum=i;

elsei++;

printf(“%d”,rownum);
getch();
return 0;
}

Using Temp

#include
#include
#define COL 4
#define ROW 4
int main() {
int arr[ROW][COL] = {
{1, 1, 1, 1},
{1, 1, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
};
int rownum;
int i = 0, j = 0, temp, count = 0;
temp = count;
for (i = 0; i < 4; i++) {
count = 0;
for (j = 0; j < 4; j++) {
if (arr[i][j] == 0) {
count++;
}
if (count > temp) {
temp = count;
rownum = i;
}
}
}
printf(“%d”, rownum);
return 0;
}

Question 4: A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit. Limit = 20.

First, understanding the question is important. In this question, The output required is
3     4      5
8     6     10
5     12   13
15   8     17
12   16   20

This is because, 32 + 42 = 52 .

There are two ways to solve this question.

1) The simple way is to generate triplets smaller than t given limit and check if it meets the given condition every time. If the condition is true, print the triplets. Even though this method is simple, this is not effective as the code will be long for larger limits.

2) The effective way to solve is problem is by using the variables m and n instead of a, b and c by using the following relation:

a = m2 – n2
b = 2 * m * n
c = m2 + n2 because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m2 * n2
c2 = m4 + n4 + 2* m2 * n2

In C

The above method is written in C program as given below:

// A C program to generate Pythagorean triplets smaller than a given limit
#include
#include // Function to generate pythagorean triplets
// smaller than limit
void pythagoreanTriplets(int limit)

// triplet: a^2 + b^2 = c^2
int a, b, c=0;
// loop from 2 to max_limitit
int m = 2;
// Limiting c would limit all a, b and c
while (c < limit)

// now loop on j from 1 to i-1
for (int n = 1; n < m; ++n)

// Evaluate and print triplets using
// the relation between a, b and c
a = m*m – n*n;
b = 2*m*n;
c = m*m + n*n;
if (c > limit)
break;
printf(“%d %d %d\n”, a, b, c);

m++;

// Driver program
int main()

int limit = 20;
pythagoreanTriplets(limit);
return 0;
}

In Java

public class Amcat2 {
public static void main(String[] args) {
Scanner sc = new Scanner(in);
int n = sc.nextInt();
int[] arrivingTime = new int[n];
int[] burstTime = new int[n];
int timeQuant;
int[] ans;
for(int i = 0; i < n; i++){
arrivingTime[i] = sc.nextInt();

for(int i = 0; i < n; i++)
burstTime[i] = sc.nextInt();
timeQuant = sc.nextInt();
ans = waitingTime(n, arrivingTime, burstTime, timeQuant);
for(int i = 0; i < n; i++)
out.print(ans[i] + ” “);
out.println();

private static int[] waitingTime(int n, int[] a, int[] b, int q) {
int[] result = new int[n];
int[] processNo = new int[n];
for(int i = 0; i < n; i++)
processNo[i] = i;
b = sort(a,b,processNo);
sort(a);
int max = b;
int index = 0;
for(int i = 1; i < n; i++)
if(max < b[i]){
max = b[i];
index = i;
}
int[] c = new int[n];
int time = 0;
for(int i = 0; i < n; i++){
c[i] = b[i];
out.println(a[i] + ” ” + b[i]);
}
//System.out.println(“max : ” + max);
while(b[index] != 0){
for(int i = 0; i < n; i++){
if(b[i] == 0)
continue;
if(b[i] >= q){
b[i] -= q;
time += q;
if(b[i] == 0)
result[i] = time – a[i] – c[i];
}else{
time += q – b[i];
b[i] = 0;
result[i] = time – a[i] – c[i];

}
}
int temp;
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
if(processNo[i] < processNo[j]){
temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
return result;

private static int[] sort(int[] a, int[] b, int[] processNo) {
int temp;
for(int i = 1; i < a.length; i++){
for(int j = 0; j < i; j++){
if(a[i] < a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
temp = processNo[i];
processNo[i] = processNo[j];
processNo[j] = temp;
}
}

return b;

}

Tab Content

Question 5: Write a program to find the sum of contiguous sub array within a one-dimensional array of numbers which has the largest sum.

Explanation:

Simple idea of the Kadane’s algorithm is used here. The algorithm is, to look for all positive contiguous segments of the array (max_ending_here is used for this) and keep a track of maximum sum of contiguous segment among all positive segments. max_so_far is used for this. Each time we get a positive sum, compare it with max_so_far and update max_so_far if it is greater than max_so_far.

In C++

#include
#include
using namespace std;
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a);
int max_sum = maxSubArraySum(a, n);
cout << “Maximum contiguous sum is ” << max_sum;
return 0;
}

Question 6: Write a program for bubble sorting.

Explanation:

The simple way to solve this is to compare two adjacent numbers in an array and swap their position if the second number is greater than the first. Continue this until the output is obtained.

In C:

#include
void swap(int *, int *);
void bubble(int[], int);
int main()
{
int n;
printf(“Enter the size of n\n”);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“Enter %d Element\n”, i);
scanf(“%d”, &arr[i]);
}
printf(“Array before sorting\n”);
for (int i = 0; i < n; i++)
{
printf(“%d\t”, arr[i]);
}
bubble(arr, n);
printf(“\nArray after sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d\t”, arr[i]);
}
printf(“\n”);
return 0;
}
void bubble(int a[], int n) {
int i, j;
for (i = 0; i < n – 1; i++) {
for (j = 0; j < n – i – 1; j++) {
if (a[j] > a[j + 1]) {
swap(&a[j], &a[j + 1]);
}
}
}
}
void swap(int *a, int *b)
{
*a = *a + *b;
*b = *a – *b;
*a = *a – *b;
}

Question 7: Write a program to find the GCD of two Integers.

In C

#include
int main()

int n1, n2, i, gcd;
printf(“Enter two integers: “);
scanf(“%d %d”, &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)

// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;

printf(“G.C.D of %d and %d is %d”, n1, n2, gcd);
return 0;
}

In Java

package arrayProject;
import java.util.*;
import java.math.*;
public class GCDPhrSe
{
public static int gcd(int n1,int n2)
{
if(n1==0)
return n2;
if(n2==0)
return n1;
if(n1>n2)
return gcd(n1-n2,n2);
else
return gcd(n2-n1,n1);
}
public static void main(String args[])
{
Scanner sc=new Scanner(in);
int n1,n2;
System.out.println(“Enter two no”);
n1=sc.nextInt();
n2=sc.nextInt();
int n3=gcd(n1,n2);
System.out.println(n3);

}

Question 8: Write a program to remove all vowels from a string.

There are two ways to write this program.

In C Using Pointers

#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#define TRUE 1
# define FALSE 0
int check_vowel(char);
main() {
char string, * temp, * pointer, ch, * start;
printf(“Enter a string\n”);
gets(string);
temp = string;
pointer = (char * ) malloc(100);
if (pointer == NULL) {
printf(“Unable to allocate memory.\n”);
exit(EXIT_FAILURE);
}
start = pointer;
while ( * temp) {
ch = * temp;
if (!check_vowel(ch)) { * pointer = ch;
pointer++;
}
temp++;
} * pointer = ‘\0’;
pointer = start;
strcpy(string, pointer); /* If you wish to convert original string */
free(pointer);
printf(“String after removing vowel is \”%s\”\n”, string);
return 0;
}
int check_vowel(char a) {
if (a >= ‘A’ && a <= ‘Z’) a = a + ‘a’ – ‘A’;
if (a == ‘a’ || a == ‘e’ || a == ‘i’ || a == ‘o’ || a == ‘u’) return TRUE;
return FALSE;

In C Using Switch Case

#include
#include
int check_vowel(char);
int main()

char s, t;
int i, j = 0;
printf(“Enter a string to delete vowels\n”);
gets(s);
for(i = 0; s[i] != ‘\0’; i++) {
if(check_vowel(s[i]) == 0) { //not a vowel
t[j] = s[i];
j++;

t[j] = ‘\0’;
strcpy(s, t); //We are changing initial string
printf(“String after deleting vowels: %s\n”, s);
return 0;

int check_vowel(char c)

switch(c) {
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
return 1;
default:
return 0;

}

Question 9: Write a program to return a sorted array from two unsorted array.

In C++

#include <bits/stdc++.h>
using namespace std;
// Function to merge array in sorted order
void sortedMerge(int a[], int b[], int res[], int n, int m)
{
// Concatenate two arrays
int i = 0, j = 0, k = 0;
while (i < n)
{
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;

// sorting the res array
sort(res, res + n + m);

// Driver code
int main()
{
int a[] = { 10, 5, 15 };
int b[] = { 20, 3, 2, 12 };
int n = sizeof(a) / sizeof(a);
int m = sizeof(b) / sizeof(b);
// Final merge list
int res[n + m];
sortedMerge(a, b, res, n, m);
cout << “Sorted merged list :”;
for (int i = 0; i < n + m; i++)
cout << ” ” << res[i];
cout << “n”;
return 0;
}

In Java

public class SortedArray {
public static void main(String[] args) {
int a[]= {8,7,9};
int b[]= {10,5};
sort(a,b);

public static void sort(int[] a,int[] b) {
int[] c= new int[a.length+b.length];
for (int i = 0; i < a.length; i++) {
c[i]=a[i];

for (int i = 0, j=a.length; i < b.length; i++,j++) {
c[j]=b[i];

for (int i = 0; i < c.length; i++) {
for (int j = i+1; j < c.length; j++) {
if(c[i]>c[j]) {
int temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}

out.print(“Sorted Array : “);
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+” “);
}
}
}

Question 10: Write a program to swap two numbers without using third variable,

In C

This question can be solved using three ways as follows:

1) Using + and –

#include
intmain()
{
inta=10, b=20;
printf(“Before swap a=%d b=%d”,a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf(“\nAfter swap a=%d b=%d”,a,b);
return0;
}

2) Using * and /

#include
#include
intmain()
{
inta=10, b=20;
printf(“Before swap a=%d b=%d”,a,b);
a=a*b;//a=200 (10*20)
b=a/b;//b=10 (200/20)
a=a/b;//a=20 (200/10)
system(“cls”);
printf(“\nAfter swap a=%d b=%d”,a,b);
return0;
}

3) Using Bitwise XOR

#include
int main()

int x = 10, y = 5;
// Code to swap ‘x’ (1010) and ‘y’ (0101)
x = x ^ y;  // x now becomes 15 (1111)
y = x ^ y;  // y becomes 10 (1010)
x = x ^ y;  // x becomes 5 (0101)
printf(“After Swapping: x = %d, y = %d”, x, y);
return 0;
}

Question 11: Write a program to find out sum of digits of given number.

This is a tricky question to solve. The algorithm to solve the question is given below:

1) Take the integer as input.

2) Divide the input integer by 10, obtain its remainder and quotient.

3) Increment the new variable with the remainder obtained in step 2.

4) Repeat the step 2 & 3 with the quotient obtained until the quotient becomes zero.

5) Print the output and exit.

In C

/*C program to accept an integer & find the sum of its digits*/
#include
void main()

long num, temp, digit, sum = 0;
printf(“Enter the number \n”);
scanf(“%ld”, &num);
temp = num;
while (num > 0)

digit = num % 10;
sum = sum + digit;
num /= 10;

printf(“Given number = %ld\n”, temp);
printf(“Sum of the digits %ld = %ld\n”, temp, sum);
}

Question 12: Write a program to print Fibonacci series of given range.

In C

#include
#include
void main()

long a=1 ,b=1 , c ;
int i , n ;
printf(“Enter n: “) ;
scanf(“%d” , &n) ;
printf(“Fibonacci series is as shown: \n”) ;
if(n==1)
printf(“% ld \n %ld \n” , a , b) ;
else if(n>1)

printf(“% ld \n %ld \n” , a , b) ;
c=a+b ;
do

printf(” %ld \n” , c) ;
a=b ;
b=c ;
c=a+b ;

while(c<=n) ;

} Name of Exam AMCAT Automata No. of Questions 2 Cut Off 2 Partial or 1 Full Marks 900 Type Online