Amcat Automata3 PDF
Amcat Automata3 PDF
Amcat Automata3 PDF
Also
Input:
Arr1 = {1,2,3,4,5}
Arr 2 = {2,6,8,10}
In C Language
#include<stdio.h>
int Not_common (int *arr1, int *arr2, int l1, int l2)
flag1=0;
{
if(arr1[i] == arr2[j])
flag1=1; break;
}}
if(flag1 ==0)
count++;
printf("%d,", arr1[i]);
flag1=0;
{
if(arr2[i] == arr1[j])
flag1=1;
break;
}}
if(flag1 ==0)
count++;
printf("%d,", arr2[i]);
}}
return count;
int main()
int arr1[10],arr2[10];
scanf("%d %d", &len1, &len2);
scanf("%d", &arr1[i]);
scanf("%d", &arr2[i]);
return 0;
Input:
aAbcDefabcAdf
Substring : abc
In C
#include <stdio.h>
#include <string.h>
int main()
scanf("%[^\n]s", str);
len1 = strlen(str);
len2 = strlen(sub);
{
j = 0, count = 0;
if (count == len2)
{ count1++;
count = 0;
printf(“%d”, count1);
return 0; }
Find the number of all possible triplets in the array that can form the triangle( condition is a+b>c) .
In C
#include <stdio.h>
int a[100];
int n,n1,n2;
int i,j,k;
int p,q,r;
int main(void)
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-2;i++)
p=a[i];
for(j=i+1;j<n-1;j++)
q=a[j];
for(k=j+1;k<n;k++)
r=a[k];
printf("%d %d %d ",p,q,r);
printf("Yes\n");
else
printf("%d %d %d ",p,q,r);
printf("No\n");
return 0;
Print all the prime numbers which are below the given number separated by comma
Input: 50
In C
#include<stdio.h>
int main()
int n,i,j,ct=0;
scanf("%d",&n);
for(i=2;i<=n;i++)
{
ct=0;
for(j=2;j<i;j++)
if(i%j==0)
ct=1;
break;
if(ct==0)
if(i>2)
printf(", ");
printf("%d",i);
}
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
Input: PrepInsta
In C
#include <stdio.h>
#include <string.h>
int main()
char str1[100];
int size;
scanf("%[^n]s", str1);
size = strlen(str1);
printf("%s", str1);
return 0;
{ char temp;
temp = str1[index];
if (index == size / 2)
return;
Output: atsnIperP
Write a function to return a sorted array after merging two unsorted arrays, the parameters will be
two integer pointers for referencing arrays and two int variable, the length of arrays (Hint: use
malloc() to allocate memory for 3rd array):
In C
#include <stdio.h>
#include <stdlib.h>
int i,index=0,j,t;
for(i=0;i<n;i++)
arr3[index]=*p;
index++;
p++;
for(i=0;i<m;i++)
arr3[index]=*q;
index++;
q++;
t = arr3[j-1];
arr3[j-1] = arr3[j];
arr3[j] = t;
}
return arr3;
} int main()
scanf("%d", &n1);
for(i=0;i<n1;i++)
scanf ("%d",&arr1[i]);
for(i=0;i<n2;i++)
m = n1+n2;
}
for(i=1;i<=m;i++)
printf("%d\n", *(ptr+i));
return 0; }
Examples:
b[] = {20, 3, 2}
b[] = {20, 0, 2}
#include <bits/stdc++.h>
int n, int m)
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;
// Driver code
int main()
return 0;
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
---
#include <stdio.h>
int main(void) {
printf("Enter number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
printf("%d",k);
if(i != j) printf("*");
k++;
printf("\n");
k-=n;
temp=k;
for(i=n;i>=1;i--)
for(j=1;j<=i;j++)
printf("%d",temp);
if(j == i) continue;
else printf("*");
temp++;
printf("\n");
k-=(i-1);
temp=k;
return 0;
}
C++
----
#include <stdio.h>
#include <iostream>
int main()
int n;
cin>>n;
int a=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
a=a+1;
cout<<a;
if(j!=i)
cout<<"*";
cout<<"\n";
for(int i=n;i>=1;i--)
a=a-i;
for(int j=1;j<=i;j++)
cout<<a+j;
if(j!=i)
cout<<"*";
cout<<"\n";
}
return 0;
---
#include<stdio.h>
int i, j, n = 0;
n++;
printf("%d", n);
n++;
printf("*%d", n);
}
printf("\n");
n = n - i + 1;
printf("%d", n);
n++;
printf("*%d", n);
printf("\n");
n = n - i;
}
}
int main()
scanf("%u", &rows);
pattern(rows);
1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8
----
#include<stdio.h>
#include<stdlib.h>
int main(void)
{ int n,d,i;
scanf("%d",&n);
for(i=0;i<n;i+=2)
{ d=n*i+1;
for(int j=0;j<n;j++)
{ if(j==n-1)
printf("%d",d);
else printf("%d*",d);
d++;
printf("\n");
if(n%2)
i=n-2;
else
i=n-1;
for(;i>0;i-=2)
{ d=n*i+1;
for(int j=0;j<n;j++)
{if(j==n-1)
printf("%d",d);
else printf("%d*",d);
d++;
}
printf("\n");
return 0;}
---
void print(int n)
int m = 1;
int i,j;
if(i!=2)
printf(“%d ”, m);
m++;
printf( "\n");
}else{
m = m+n;
printf(“%d ”, m);
m++;
}
printf(“\n”);
for(i=n+1 ;i<=(n+n);i++){
printf(“%d ”, i);
printf(“\n”);
C++
----
#include <iostream>
int main()
int n;
cin>>n;
int p=n;
for(int i=1;i<=n;i+=2)
int k=(i-1)*n+1;
for(int j=0;j<n-1;j++)
cout<<k<<" * ";
k++;
cout<<k<<" ";
cout<<endl;
if(n%2!=0)
p=n-1;
for(int i=p;i>0;i-=2)
int k=(i-1)*n+1;
for(int j=0;j<n-1;j++)
cout<<k<<" * ";
k++;
cout<<k<<" ";
cout<<endl;
else
return 0;
(imp)
----
include <stdio.h>
int main()
scanf("%d",&n);
for(i=1;i<=n;i++)
if(i%2!=0){
for(j=1;j<=n;j++)
printf("%d",count);
if(j!=n)
printf("*");
count++;
printf("\n");
}
else{
for(j=1;j<=n;j++)
arr[num]=count;
count++;
num++;
for(i=1;i<=n/2;i++)
int num2=0;
num2=num-(i*n);
for(j=1;j<=n;j++)
{
printf("%d",arr[num2++]);
if(j!=n)
printf("*");
printf("\n");
return 0;
Pseudocode
----------
if(N%2)
i-=2;
----
void main()
{
int i,n;
scanf(“%d”,&n);
printf(“%d*%d*%d*%d*\n”,(4*i+1),(4*i+2),(4*i+3),(4*i+4));
if(n%2)
i-=2;
printf(“%d*%d*%d*%d*\n”,(4*i+1),(4*i+2),(4*i+3),(4*i+4));
C++
----
#include <iostream>
int n = 5;
int a1 = 1;
int a2 = 2*n + 1;
if(j % 2 == 0){
cout<<"*";
else{
if(i==1 || i == n) {
cout<<a1;
a1++;
}
else{
cout<<a2;
a2++;
cout<<"\n";
----
#include<stdio.h>
int main(){
int m ;
int k = 0;
int i,j, n ;
printf("enter the number");
scanf("%d",&m);
if(m%2==0){
for(i=1 ;i<=(m/2);i++){
for(j=1;j<=m;j++){
printf("%d",(m*k)+j);
if(j<m){
printf("*");
printf("\n");
k = k+2;
int p , s;
int l = m-1;
for(p =1;p<=((m)/2);p++){
for(s=1;s<=m;s++){
printf("%d",(m*l)+s);
if(s<m){
printf("*");
printf("\n");
l = l-2;
else{
int g = m+1 ;
for(i=1 ;i<=(g/2);i++){
for(j=1;j<=m;j++){
printf("%d",(m*k)+j);
if(j<m){
printf("*");
printf("\n");
k = k+2;
int p , s;
int l = m-2;
for(p =1;p<((g)/2);p++){
for(s=1;s<=m;s++){
printf("%d",(m*l)+s);
if(s<m){
printf("*");
}
printf("\n");
l = l-2;
return 0;
---
int n=13;
int p=n;
for(int i=1;i<=n;i+=2)
{ int k=(i-1)*n+1;
for(int j=0;j<n-1;j++)
{ System.out.print(k+"*");
k++;
System.out.print(k+" ");
System.out.println();
if(n%2!=0) p=n-1;
for(int i=p;i>0;i-=2)
int k=(i-1)*n+1;
for(int j=0;j<n-1;j++)
System.out.print(k+"*");
k++;
}
System.out.print(k+" ");
System.out.println();
---
#include <stdio.h>
int i,j,N=n=4,k=1;
for(i=1;i<=N-1;i++)
if(i!=2){
for(j=1;j<=N;j++){
if(j==N){
printf("%d",k);
k++;}
else{
printf("%d*",k);
k++;}
}printf("\n");
else{
k=k+N;
for(j=1;j<=N;j++){
if(j==N){
printf("%d",k);
k++;
else{
printf("%d*",k);
k++;}
printf("\n");
k=N+1;
for(j=1;j<=N;j++){
if(j==N){
printf("%d",k);
else{
printf("%d*",k);
k++;
}
}
return 0;
You need to find the logic of the program which was given.
logic optimization
int i=1;
for(int k=0;k<4;k++)
JAVA
------
package a;
public class pattern {
if(n%2==0)
for(int i=0;i<=(n/2)-1;i++)
a[i]=j;
j=j+2;
for(int i=(n/2);i<n;i++)
a[i]=l;
l=l-2;
else
for(int i=0;i<=(n-1)/2;i++)
a[i] = j;
j = j+2;
for(int i=(n+1)/2;i<n;i++)
a[i] = k;
k=k-2;
}
int[][] b = new int[n][n];
for(int i=0;i<n;i++)
for(int t=0;t<n;t++)
b[i][t]=count;
count++;
while(p!=n)
for(int i=0;i<n;i++)
System.out.printf("%02d",b[a[p]-1][i]);
if(i<n-1)
{
System.out.print('*');
System.out.println();
p++;
JAVA
-----
import java.util.Scanner;
Scanner sc=new Scanner(system.in - This website is for sale! - System Resources and
Information.);
System.out.println("Please enter the input");
int n=sc.nextInt();
int p=1;
int d=1;
if(n%2==0)
for(int i=1;i<=n;i++)
if(i==1)
for(int j=1;j<=2*n-1;j++)
if(j%2==1)
System.out.print(p++);
}
else
System.out.print("*");
System.out.println();
if((i>1)&&(i<=n/2))
int j=1;
while(j<=2*n-1)/*for(int j=1;j<=2*n-1;j++)*/
if(j%2==1)
if(j==1)
{
p=p+n;
System.out.print(p);
p++;
else
System.out.print("*");
j++;
System.out.println();
//********************************before half************************************//
if(i==n/2+1)
for(int j=1;j<=2*n-1;j++)
{
if(j%2==1)
System.out.print(p++);
else
System.out.print("*");
System.out.println();
//**************after half***********//
if((i<n)&&(i>n/2))
int j=1;
while(j<=2*n-1)//for(int j=1;j<=2*n-1;j++)
{
if(j%2==1)
if(j==1)
p=p-n+1;
//d++;
p=p-(n*2)-d;
System.out.print(p);
p++;
else
System.out.print("*");
j++;
}
System.out.println();
//***************************after half*******************************//
else{
for(int i=1;i<=n;i++)
if(i==1)
for(int j=1;j<=2*n-1;j++)
if(j%2==1)
System.out.print(p++);
}
else
System.out.print("*");
System.out.println();
if((i>1)&&(i<=n/2+1))
int j=1;
while(j<=2*n-1)/*for(int j=1;j<=2*n-1;j++)*/
if(j%2==1)
if(j==1)
{
p=p+n;
System.out.print(p);
p++;
else
System.out.print("*");
j++;
System.out.println();
//********************************half ends************************************//
//**************after half***********//
if((i<n)&&(i>n/2))
int j=1;
while(j<=2*n-1)//for(int j=1;j<=2*n-1;j++)
if(j%2==1)
if(j==1)
p=p-(2*n)-n;
System.out.print(p+n);
p++;
else
System.out.print("*");
j++;
}
System.out.println();
//***************************after half*******************************//
---
#include <iostream>
#include <cstdio>
int main() {
int N, i,;
cin>>N;
printf("%d", (4*i+j));
if(j!=N)
cout<<"*";
cout<<"\n";
if(N%2)
i-=2;
for(int j=1;j<=N;j++)
printf("%d", (4*i+j));
if(j!=N)
cout<<"*";
}
cout<<"\n";
return 0;
C++
----
#include <iostream>
int main()
// cout<<"Hello World";
int n,w=0,v=0,temp;
cout<<"row=";
cin >>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
w++;
if(i%2==0){
continue;
else
cout<<w;
cout<<" ";
if(i%2==0){
continue;
else
cout<<"\n";
}
v=w;
for(int i=n;i>=1;i--){
v=v-n;
for(int j=1;j<=n;j++){
if(i%2==0){
cout<<v+j;
cout<<" ";
if(i%2==0){
cout<<"\n";
return 0;
}
C
----
void series() {
int m=sc.nextInt();
int n=1;
int count=1;
for(int i=1;i<m;i++) {
System.out.print(n);
n++;
if(j<m) {
System.out.print("*");
count++;
n=(count*m)+1;
System.out.println();
System.out.print(i);
if(i<m+m) {
System.out.print("*");
123
654
789
12 11 10
---
#include <stdio.h>
int main()
int i,j,n;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
printf(“%d ”,i);
if(i%3 == 0 && i != n)
j = i + 3;
printf(“\n”);
while(j != i)
if(j > n)
{
j--;
else
printf(“%d ”,j);
j--;
i = i + 3;
printf(“\n”);
return 0;
}
C++
---
#include <iostream>
int main()
int n,k;
cin>>n;
for(int i=0;i<n;i++)
if(i%2==0)
k=i*n+1;
for(int j=0;j<n;j++)
cout<<k<<" ";
k++;
else
k=(i+1)*n;
for(int j=0;j<n;j++)
cout<<k<<" ";
k--;
cout<<endl;
}
return 0;
int count = 1;
while(true) {
break;
if (backwd) {
else {
count += 3;
backwd = !backwd;
--
#include<stdio.h>
int i = 0;
int rev = 0;
int val = 0;
rev = (i / cols) % 2;
return 0;
FIND SUM LEAVING OUR ROW AND COL: COCUBES CODING QUESTION
PROBLEM STATEMENT:
The function takes a two-dimensional array ‘arr’, its number of rows ‘m’, its number of columns ‘n’
and integers ‘i’ and ‘j’ as input. Implement the function to find and return the sum of elements of the
array leaving out the elements of the i and j column. The algorithm to find the sum is as follows:
1.Iterate over every row except for i row,and keep on adding each element except for the elements
of j column to a variable ‘sum’.
NOTE:
CODING:
***********************
Program
***********************
/*
*/
#include <stdio.h>
void main ()
int i, j, m, n, sum = 0;
scanf(“%d”, &array[i][j]);
{
sum = sum + array[i][j] ;
sum = 0;
sum = 0;
sum = 0;
Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the
elements to its right side.
And the rightmost element is always a leader. For example int the array {16, 19, 4, 3, 8, 3}, leaders
are 19, 8 and 3?
With C++
-------
#include<iostream>
int j;
break;
int main()
int n = sizeof(arr)/sizeof(arr[0]);
printLeaders(arr, n);
return 0;
With Java
--------
class LeadersInArray
{
int j;
break;
if (j == size)
}
/* Driver program to test above functions */
int n = arr.length;
lead.printLeaders(arr, n);
}?
Maximum difference between two elements such that larger element appears after the smaller
number
Given an array arr[] of integers, find out the difference between any two elements such that larger
element appears after the smaller number in arr[].
Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If
array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9)
Java
---
#include
elements in array.
int i, j;
return max_diff;
int main()
{
getchar();
return 0;
Java
-----
class MaximumDiffrence
elements in array.
int i, j;
return max_diff;
}
/* Driver program to test above functions */
System.out.println(“Maximum differnce is ” +
maxdif.maxDiff(arr, 5));
In this method, instead of taking difference of the picked element with every other element, we take
the difference with the minimum element found so far. So we need to keep track of 2 things:
---
#include<stdio.h>
elements in array.
int i;
min_element = arr[i];
return max_diff;
int main()
getchar();
return 0;
Java
----
class MaximumDiffrence
elements in array.
int i;
min_element = arr[i];
return max_diff;
System.out.println(“MaximumDifference is ” +
maxdif.maxDiff(arr, size));
Given a string of character, find the length of longest proper prefix which is also a proper suffix.
Example:
S = abab
Each test case has one line denoting the string of length less than 100000.
Output:
Example:
Input:
abab
aaaa
Output:
C++
---
int lps(string);
int main() {
//code
int T;
cin >> T;
getchar();
while (T–) {
string s;
cin >> s;
printf(“%d\n”, lps(s));
return 0;
int lps(string s) {
int n = s.size();
int lps[n];
int i = 1, j = 0;
lps[0] = 0;
while (i < n) {
if (s[i] == s[j]) {
j++;
lps[i] = j;
i++;
} else {
if (j != 0)
j = lps[j – 1];
else {
lps[i] = 0;
i++;
Java
----
import java.util.*;
import java.lang.*;
import java.io.*;
class PreSuf {
//code
int t = s.nextInt();
String s1 = s.next();
while (j < l) {
if (s1.charAt(j) == s1.charAt(len)) {
len++;
lps[j] = len;
j++;
} else {
if (len != 0) {
} else {
lps[j] = 0;
j++;
System.out.println(lps[l – 1]);
}
Find the number closest to n and divisible by m
Given two integers n and m. The problem is to find the number closest to n and divisible by m. If
there are more than one such number, then output the one having maximum absolute value. If n is
completely divisible by m, then output n only. Time complexity of O(1) is required.
Constraints: m != 0
We find value of n/m. Let this value be q. Then we find closest of two possibilities. One is q * m other
is (m * (q + 1)) or (m * (q – 1)) depending on whether one of the given two numbers is negative or
not.
Algorithm:
closestNumber(n, m)
Declare q, n1, n2
q=n/m
n1 = m * q
if (n * m) > 0
n2 = m * (q + 1)
else
n2 = m * (q - 1)
return n1
return n2
C++
// and divisible by m
#include <bits/stdc++.h>
using namespace std;
// and divisible by m
int q = n / m;
int n1 = m * q;
return n1;
return n2;
int main()
int n = 13, m = 4;
n = -15; m = 6;
n = 18; m = -7;
return 0;
Java
// and divisible by m
// and divisible by m
int q = n / m;
int n1 = m * q;
return n1;
// else n2 is the required closest number
return n2;
int n = 13, m = 4;
System.out.println(closestNumber(n, m));
n = -15; m = 6;
System.out.println(closestNumber(n, m));
n = 0; m = 8;
System.out.println(closestNumber(n, m));
n = 18; m = -7;
System.out.println(closestNumber(n, m));
A = AND
B = OR
C = XOR
Calculate the value of the string assuming no order of precedence and evaluation is done from left to
right.
Constraints – The length of string will be odd. It will always be a valid string.
Example, 1AA0 will not be given as an input.
Examples:
Input : 1A0B1
Output : 1
1 AND 0 OR 1 = 1
Input : 1C1B1B0A0
Output : 0
C/C++
#include <bits/stdc++.h>
int evaluateBoolExpr(string s)
int n = s.length();
// is AND.
if (s[i + 1] ==’A’) {
s[i + 2] =’0’;
else
s[i + 2] =’1’;
}
// If operator next to current operand
// is OR.
s[i + 2] =’1’;
else
s[i + 2] =’0’;
else {
if (s[i + 2] == s[i])
s[i + 2] =’0’;
else
s[i + 2] =’1’
// Driver code
int main()
string s = “1C1B1B0A0”;
return 0;
Java
// Java program to evaluate value of an expression.
int n = s.length();
// is AND.
if (s.charAt(i + 1) == ‘A’) {
if (s.charAt(i + 2) == ‘0’ ||
s.charAt(i) == 0)
s.setCharAt(i + 2, ‘0’);
else
s.setCharAt(i + 2, ‘1’);
// is OR.
s.charAt(i + 1 ) == ‘B’) {
if (s.charAt(i + 2) == ‘1’ ||
s.charAt(i) == ‘1’)
s.setCharAt(i + 2, ‘1’);
else
s.setCharAt(i + 2, ‘0’);
else {
if (s.charAt(i + 2) == s.charAt(i))
s.setCharAt(i + 2, ‘0’);
else
s.setCharAt(i + 2 ,’1');
// Driver code
String s = “1C1B1B0A0”;
System.out.println(evaluateBoolExpr(sb));
}
// Driver code
int main()
string s = “1C1B1B0A0”;
return 0;
Ques. 1 Write a program to find out total marks obtained by a student if the student gets 3 marks for
the correct answer and -1 for the wrong answer?
LOGIC
1. Convert the string in 0’s and 1’s. All consonants will be considered as 1 and
vowels as 0.
2. Make 2 D array (this is dynamic programming approach) and start matching.
3. For string matching, one side will be the string (converted with 0’s and 1’s)
and another side with vowels and consonants as 0 and 1.
cons & Vow/ String W A Y T O C R A C K
01 0 1 1 0 1 1 0 1 1
0 00 1 0 0 1 0 0 1 0 0
1 01 0 1 2 0 1 2 0 1 2
1. Whenever the 0 will be matched with 0, increment previous array element by
1. Similarly when 1 will be matching with 1, increment previous array element
by 1.
2. In case of 3 consecutive consonants, the current array value reaches to 3 or
5 consecutive vowels the string is said to “BAD”.
In case of question mark “?” add one to both consonant and vowel array. Also,
make a Boolean flag which will be true in case of “?” occurs. If “?” and 3 consecutive
consonants or 5 consecutive vowels occurred then the string is said to be “MIXED”,
otherwise the string is “GOOD”.
Please see below code for clear understanding, some of the corner test cases are
tested which are also commented there, to test uncomment it and run.
Some more test cases are also given at the end of this post.
Code –
#include "string.h"
#include"iostream"
using namespace std;
//alphabet check
bool alphabetcheck(char alphabet){
if (alphabet >= 'a' && alphabet <= 'z')
return true;
return false;
}
//vowel check
int vowelcheck(char vowel)
{
if (vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel
== 'u')
return 0;
return 1;
}
int main(void) {
//the two sequences
//string X = "waytocrack";// ---- GOOD
//string X = "waaaaaaytocrack";//--BAD
string X = "wayt?arack"; // mixed
//length of the sequences
int XLen = X.size();
int Arr[2][20];
memset(Arr, 0, sizeof(Arr[0][0]) * 2 * 20);
int max0 = 0;
int max1 = 0;
int index;
bool mixed_value = false;
for (size_t i = 0; i < XLen; i++)
{
//alphabet check
if (alphabetcheck(X[i]))
{
//vowel check fucntion:
if ((vowelcheck(X[i])) == 0)
{
Arr[0][i+1] = Arr[0][i] + 1;
if (Arr[0][i + 1] > max0)
max0 = Arr[0][i + 1]; //check for max 0
if ((mixed_value == true) && (max0 >= 5) && (Arr[0][i + 1] >= 5))
// Arr[0][i + 1] >= 5 when current value is greater than 5
{
if ((i - index >= 5 || (Arr[1][index] + Arr[0][index + 5] ==
7)))
mixed_value = false;
}
}
else
{
Arr[1][i + 1] = Arr[1][i] + 1;
if (Arr[1][i + 1] > max1)
max1 = Arr[1][i + 1]; //check for max 1
if (mixed_value == true && max1 >= 3 && Arr[1][i + 1] >= 3 )
// Arr[0][i + 1] >= 5 when current value is greater than 3
{
if ((i - index >= 3 || (Arr[0][index] + Arr[1][index + 3] ==
7)) )
mixed_value = false;
}
}
if (mixed_value == false && (max0 >= 5 || max1 >= 3)){
//checking the count value GREATER than or 3
cout << " BAD " << endl;
exit(0);
}
}
return 0;
}
a?fafff BAD
??aa?? MIXED
abc GOOD
aaa?aaafff BAD
aaaa?ff?aaa?aaa?fff BAD
aaaaff? MIXED
aaaaf? GOOD
?aaaaffaaf?aaaafff BAD
?aaaaffaaf?aaaaff MIXED
vaxaaaa?bbadadada BAD
aaaa?bb BAD
vabb?aaaadadada BAD
vabab?aaaadadada MIXED
The solution is 3 or more than 3 consecutive consonants and 5 and more than 5
consecutive vowels.
You can check some of the tested test cases at the end of the code.
We are given an array with n elements from {1,2,3,4}. Find the number of minimum changes
required to be performed so that no two adjacent numbers are same?
We are given a count of songs to be played – n, highest volume allowed – h, initial volume –
i, and list of allowed volume change A[] of size n. The singer can either increase/decrease the
volume of sound system for the next song by the allowed volume change A[j] for jth song
from the volume of the j-1th song. The aim is to maximize the volume of last sound. Find the
maximum volume that can be attained, or return -1 if there is no possibility of changing
volume due to the given constrains. (Volume cannot be in negative.)
Write a program to print all Subsequences of String which Start with Vowel and End with
Consonant
Given a string return all possible subsequences which start with vowel and end with a
consonant. A String is a subsequence of a given String, that is generated by deleting some
character of a given string without changing its order.
Examples:
Input : ‘abc’
Output : ab, ac, abc
Input : ‘aab’
Output : ab, aab
Output:
[ef, ab, ac, aef, abc, abf, af, acf, abcef, abcf, acef, abef]
A man starts from his house with a few pan cakes. let they be N. Now he visits K places
before reaching home. At each place he can buy a cake, sell a cake or do nothing. But he
must sell L cakes before reaching home.Find the maximum number of cakes he can have at
any point in his journey.N,K,L are given as input?
A man starts from his house with a few pan cakes. let they be N. Now he visits K places
before reaching home. At each place he can buy a cake, sell a cake or do nothing. But he
must sell L cakes before reaching home.Find the maximum number of cakes he can have at
any point in his journey.N,K,L are given as input?
Given a boolean 2D array, where each row is sorted. Find the row with the maximum number
of 1s.
Example
Input matrix
0 1 1 1
0 0 1 1
1 1 1 1 // this row has maximum 1s
0 0 0 0
Output: 2
A simple method is to do a row wise traversal of the matrix, count the number of 1s in each
row and compare the count with max. Finally, return the index of row with maximum 1s. The
time complexity of this method is O(m*n) where m is number of rows and n is number of
columns in matrix.
We can do better. Since each row is sorted, we can use Binary Search to count of 1s in each
row. We find the index of first instance of 1 in each row. The count of 1s will be equal to
total number of columns minus the index of first 1.
#include <stdio.h>
#define R 4
#define C 4
/* A function to find the index of first index of 1 in a boolean array arr[] */
int first(bool arr[], int low, int high)
{
if(high >= low)
{
// get the middle index
int mid = low + (high - low)/2;
// check if the element at middle index is first 1
if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
return mid;
// if the element is 0, recur for right side
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
else // If element is not first 1, recur for left side
return first(arr, low, (mid -1));
}
return -1;
}
// The main function that returns index of row with maximum number of 1s.
int rowWithMax1s(bool mat[R][C])
{
int max_row_index = 0, max = -1; // Initialize max values
// Traverse for each row and count number of 1s by finding the index
// of first 1
int i, index;
for (i = 0; i < R; i++)
{
index = first (mat[i], 0, C-1);
if (index != -1 && C-index > max)
{
max = C - index;
max_row_index = i;
}
}
return max_row_index;
}
/* Driver program to test above functions */
int main()
{
bool mat[R][C] = { {0, 0, 0, 1},
{0, 1, 1, 1},
{1, 1, 1, 1},
{0, 0, 0, 0}
};
printf("Index of row with maximum 1s is %d n", rowWithMax1s(mat));
return 0;
}
Output: