Mettl Programs

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

Day4

Is even

if (input1%2==0)
{
return 2;
}
else
{
return 1;
}

Is odd

if (input1%2!=0)
{
return 2;
}
else
{
return 1;
}

Return last digit


input1=Math.abs(input1);
if(input1<10)
{
return input1;
}
else
{
return input1%10;
}
Return second last digit
input1=Math.abs(input1);
if(input1<10)
{
return -1;
}
else
{

input1=input1/10;
return(input1%10);
}
Sum of last two digits

input1=Math.abs(input1%10);
input2=Math.abs(input2%10);
return (input1+input2);

Day5

Exact multiple of m?

if(input1==0||input2==0)
{
return 3;
}
else if((input1%input2)!=0)
{
return 1;
}
else
{
return 2;
}

How many is even in given 5 numbers

int count=0;
if(input1%2==0)
{
count++;
}
if(input2%2==0)
count++;
{
}
if(input3%2==0)
{
count++;
}
if(input4%2==0)
{
count++;
}
if(input5%2==0)
{
count++;
}
return count;
OR

int arr[]={input1,input2,input3,input4,input5};
int count=0;
for(int i:arr)
{
if(i%2==0)
{
count++;
}
}
return count;

How many is odd in given 5 numbers

int arr[]={input1,input2,input3,input4,input5};
int count=0;
for(int i:arr)
{
if(i%2!=0)
{
count++;
}
}
return count;

How many are even or odd

int arr[]={input1, input2,input3,input4,input5};


int ecount=0;
int ocount=0;
for(int i : arr)
{
if(i%2==0)
{
ecount++;
}
else
{
ocount++;
}
}
if(input6.equals("even"))
{
return ecount;
}
else
{
return ocount;
}

Day 6

Is prime

for(int i=2;i<input1;i++)
{
if(input1%i==0)
{
return 1;
}

}
return 2;

Factorial of number

int result=1;
for(int i=1;i<=input1;i++)
{
result*=i;
}
return result;

OR
int result=1;
while(input1>0)
{
result*=input1;
input1--;
}
return result;

Nth fibonacci
int a=1;
int b=0;
int c=0;
if(input1>1)
{
for(int i=2;i<=input1;i++)
{
c=a+b;
a=b;
b=c;
}
return c;
}
else
{
return 0;
}

Nth prime
int c=0;
for(int num=2; ;num++)
{
if(isprime(num))
{
c++;
}
if(c==input1)
{
return num;
}

}
}

boolean isprime(int num)


{
for(int i=2;i<num;i++)
{
if(num%i==0)
{
return false;
}
}
return true;

Day 7

Numbers of prime in specified range

int c=0;
for(int num=input1;num<=input2;num++)
{
if(isprime(num))
{
c++;
}
}

return c;

boolean isprime(int N)
{
for(int i=2;i<N;i++)
{
if(N%i==0)
{
return false;
}
}
return true;

All digits count

int count=0 ;
while(input1>0)
{
input1=input1/10;
count++;
}
return count;

OR
String str=String.valueof(input1);
return str.length();

OR
LinkedList<Integer> list=new LinkedList();
while(input1!=0)
{
list.add(input1%10);
input1/=10;
}
return list.size();

3:unique digits count


int arr[]=new int[10];
while(input1!=0)
{
int val=input1%10;
arr[val]++;
input1/=10;
}
int count=0;
for(int i=0;i<10;i++)
{
if(arr[i]!=0)
{
count++;
}

}
return count;

OR
TreeSet<Integer> tset = new TreeSet<Integer>();
while(input1!=0)
{
tset.add(input1%10);
input1/=10;
}
return tset.size();

Non repeated digits count:

int arr[]=new int[10];


while(input1!=0)
{
int val=input1%10;
arr[val]++;
input1/=10;
}
int count=0;
for(int i=0;i<10;i++)
{
if(arr[i]==1)
{
count++;
}

}
return count;

OR
LinkedList <Integer> list=new LinkedList <Integer> ();
while(input1!=0)
{
list.add(input1%10);
input1/=10;
}
int count=0;
for(int i:list)
{
int freq=Collections.frequency(list,i);
if(freq==1)
{
count++;
}
}
return count;

Day8

1.digitsum: sum of all digits in N:

int flag=0;
if(input1<0)
{
flag=1;
input1=Math.abs(input1);
}
while(input1>9)
{
input1=makeSingle(input1);
}
return (flag==0)?input1:-input1;

}
int makeSingle(int num)
{
int sum=0;
while(num!=0)
{
sum+=num%10;
num/=10;
}
return sum;

OR

int flag=0;
if(input1==0)
{
retrun 0;
}

if(input1<0)
{
flag=1;
input1=Math.abs(input1);
}
input1=input1%9;
input1=(input1==0)?9:input1;
Return (flag==1)?-input1:input1

digitsum even: sum of even digits in N:

int sum=0, a;
while(input1!=0)
{
a=input1%10;
if(a%2==0)
{
sum+=a;
}
input1=input1/10;
}
return sum;

digitsum odd: sum of odd digits in N:

int sum=0, a;
while(input1!=0)
{
a=input1%10;
if(a%2!=0)
{
sum+=a;
}
input1=input1/10;
}
return sum;
digitsum odd or even: sum of odd or even digits in N:
int ocount=0,ecount=0, num;
while(input1!=0)
{
num=input1%10;
if(num%2!=0)
{
ocount+=num;
}
else
{
ecount+=num;
}

input1=input1/10;
}
if(input2.equals("even"))
{
return ecount;
}
else
{
return ocount;
}

DAY9

Is palindrome
String str=String.valueOf(input1);
StringBuffer sb=new StringBuffer(str);
sb.reverse();
String str2=String.valueOf(sb);
if(str.equals(str2))
{
return 2;
}
else
{
return 1;
}

Is palindrome possible
int arr[]= new int[10];
int count=0;
while(input1!=0)
{
arr[input1%10]++;
input1/=10;
}
for(int i=0;i<10;i++)
{
if(arr[i]%2!=0)
{
if(count!=0)
{
return 1;
}
count++;
}
}
return 2;

3.create pin using alpha, beta, gamma :


int u1=input1%10,u2=input2%10,u3=input3%10;
int t1=(input1/10)%10,t2=(input2/10)%10,t3=(input3/10)%10;
int h1=input1/100,h2=input2/100,h3=input3/100;
int u=Math.min(u1,Math.min(u2,u3));
int t=Math.min(t1,Math.min(t2,t3));
int h=Math.min(h1,Math.min(h2,h3));
int th=Math.max(u1,Math.max(u2,Math.max(u3,Math.max(t1,Math.max(t2,
Math.max(t3,Math.max(h1,Math.max(h2,h3))))))));
int num=th*1000+h*100+t*10+u;
return num;

OR

int u1[]={(input1%10),(input2%10),(input3%10)};
int t1[]={(input1/10)%10,(input2/10)%10,(input3/10)%10};
int h1[]={(input1/100),(input2/100),(input3/100)};
Arrays.sort(u1);
Arrays.sort(t1);
Arrays.sort(h1);
int th[]={u1[2],t1[2],h1[2]};
Arrays.sort(th);
int num=th[2]*1000+h1[0]*100+t1[0]*10+u1[0];
return num;

4.weight of a hill pattern :

int sum=0;
for( int i=0;i<input1;i++)
{
for(int j=0;j<=i;j++)
{

sum+=input2;

}
input2+=input3;
}
return sum;

DAY 10
1. return second word in uppercase :

String stra[];
stra=input1.split(" ");
if(stra.length<=1)
{
return "LESS";
}
else
{
String a=stra[1];
a=a.toUpperCase();
return a;
}

OR
String stra[];
stra=input1.split(" ");
if(stra.length<=1)
{
return "LESS";
}
else
{
return str[1].toUpperCase();
}
is palindrome(string) :

StringBuffer sb=new StringBuffer(input1);


sb.reverse();
String str=String.valueOf(sb);
if(input1.equalsIgnoreCase(str))
{
return 2;
}
else
{
return 1;
}

weight of string:

int sum=0;
input1=input1.toUpperCase();

if(input2==0)
{
input1=input1.replaceAll("[AEIOU]","");
}
int len=input1.length();
for(int i=0;i<len;i++)
{
char ch=input1.charAt(i);
if(Character.isAlphabetic(ch))
{
sum+=ch-64;
}
}
return sum;

4.most frequent digit:

int arr[]=new int[10];


int input[]={input1,input2,input3,input4};
for(int inval:input)
{
if(inval==0)
{
arr[0]++;
}
while(inval!=0)
{
int val=inval%10;
arr[val]++;
inval/=10;
}
}
int max=0,ans=0;;
for(int i=0;i<10;i++)
{
if(max<=arr[i])
{
max=arr[i];
ans=i;
}
}
return ans;

OR

int arr[]={input1,input2,input3,input4};
LinkedList<Integer> list1=new LinkedList();
int maxfreq=0,ans=0;
for(int i:arr)
{ if(i==0)
{
list1.add(i);
}
while(i!=0)
{
list1.addFirst(i%10);
i/=10;
}
}
Collections.sort(list1);
for(int i:list1)
{
int freq=Collections.frequency(list1,i);

if(freq>=maxfreq)
{
maxfreq=freq;
ans=i;
}
}
return ans;
METTL-2

M2 (A)

String based programs (part1)


1. Find string code:
input1=input1.toUpperCase();
String stra[]=input1.split(" ");
String ans="";

for( String str:stra)


{
int sum=0;
int len=str.length();
for(int i=0;i<len/2;i++)
{
int temp=(str.charAt(i)-64)-(str.charAt(len-1-i)-64);
sum+=Math.abs(temp);
}
if(len%2!=0)
{
sum+=str.charAt(len/2)-64;
}
ans+=sum;
}
return Integer.valueOf(ans);

2. get code through Strings:


String str[]=input1.split(" ");
int len=0;
for(int i=0;i<str.length;i++)
{
len+=str[i].length();
}
int sum=0;
while(len>10)
{
while(len!=0)
{
sum+=len%10;
len/=10;
}
}
return sum;

//OR//

input1=input1.replaceAll(" ","");
int len=input1.length();
int sum=0;
while(len!=0)
{
sum+=len%10;
len/=10;

if(sum>10)
{
len=sum;
sum=0;
}
}

return sum;
3.Addition using Strings

BigInteger big1=new BigInteger(input1);


BigInteger big2=new BigInteger(input2);
big1=big1.add(big2);
return String.valueOf(big1);

Array based programs


1.simple encoded array:

int sum=input1[input2-1];
for(int i=input2-2;i>=0;i--)
{
input1[i]=input1[i]-input1[i+1];
sum+=input1[i];
}
Result obj=new Result(input1[0],sum);
return obj;

OR

int sum=input1[input2-1];
LinkedList<Integer> list=new LinkedList();

list.add(input1[input2-1]);
for(int i=input2-2;i>=0;i--)
{
list.addFirst(input1[i]-list.getFirst());
sum+=list.getFirst();
}
Result obj=new Result(list.getFirst(),sum);
return obj;
2.Decreasing sequence:

int count=0,result1=0,result2=0;

for(int i=0;i<input2-1;i++)

if(input1[i]>input1[i+1])

count++;

else

if(count>0)

result1++;

count=0;

if(result2<count)

result2=count;

if(count!=0)

{
result1++;

if(result2>0)

result2++;

Result ob=new Result(result1,result2);

return ob;

OR

LinkedList<Integer>list=new LinkedList<Integer>();
int count=1,seq=0;
list.add(0);
for(int i=0;i<input2-1;i++)
{
if(input1[i]>input1[i+1])
{
count++;
}
else if(count>1)
{
list.add(count);
seq++;
count=1;
}
}
if(count>1)
{
list.add(count);
seq++;
}
return new Result(seq,Collections.max(list));
3.Most frequent digit in array

LinkedList <Integer> list=new LinkedList();


int maxfreq=0,ans=0;
for(int i:input1)
{
while(i!=0)
{
list.addFirst(i%10);
i/=10;
}
}
Collections.sort(list);
for(int i:list)
{
int freq=Collections.frequency(list,i);

if(freq>=maxfreq)
{
maxfreq=freq;
ans=i;
}
}
return ans;

Number based programs

1.SUM OF power digits

int sum=0, value1=0,value2=0;


Double base,power ,square_root;
while(input1>0)
{
value1=input1%10;
base=Double.valueOf(value1);
power=Double.valueOf(value2);
square_root=Math.pow(base,power);
sum+=square_root;
value2=value1;
input1/=10;
}
return sum;
}
}
/* or*/

LinkedList<Integer>list=new LinkedList();
int sum=1;
while(input1!=0)
{
list.addFirst(input1%10);
input1/=10;
}
for(int i=0;i<list.size()-1;i++)
{
sum+=Math.pow(list.get(i),list.get(i+1));

}
return sum;

2.Sum of sums of digits in cyclic order

LinkedList<Integer> list=new LinkedList();


int sum=0;
while(input1!=0)
{
list.addFirst(input1%10);
input1/=10;
}
for(int i=0;i<list.size();i++)
{
for(int j=i;j<list.size();j++)
{
sum+=list.get(j);
}
}
return sum;

String based programs (part2)

1.Identify possible words:

String arr[]=input2.split(":");
input1=input1.toUpperCase();
String out="";
for(String temp :arr)
{ temp=temp.toUpperCase();
int count=0;
if(temp.length()==input1.length())
{
for(int i=0;i<temp.length();i++)
{
if(input1.charAt(i)!='_')
{
if(input1.charAt(i)==temp.charAt(i))
{
count++;
}
}
}
if(count==temp.length()-1)
{
out+= temp +":";
}

}
}
if(out=="")
{
return "ERROR-009";
}
return out.substring(0,out.length()-1);

OR
String arr[]=input2.split(":");
input1=input1.toUpperCase();
int idx=input1.indexOf("_");
input1=input1.replace("_","");
String out="";
for(String temp :arr)
{
temp=temp.toUpperCase();
if(temp.length()-1==input1.length())
{
String str=temp.substring(0,idx)+temp.substring(idx+1);
if(input1.equals(str))
{
out+=temp+":";
}
}
}
if(out=="")
{
return "ERROR-009";
}
return out.substring(0,out.length()-1);

2.Encoding three strings:


String out1="",out2="",out3="";
int sep=0;
String []arr={input1,input2,input3};
for(String str:arr)
{
if(str.length()%3==0)
{
sep=str.length()/3;
out1+=str.substring(0,sep);
out2+=str.substring(sep,2*sep);
out3+=str.substring(2*sep);
}
else if(str.length()%3==1)
{
sep=str.length()/3;
out1+=str.substring(0,sep);
out2+=str.substring(sep,2*sep+1);
out3+=str.substring(2*sep+1);
}
else
{
sep=str.length()/3;
out1+=str.substring(0,sep+1);
out2+=str.substring(sep+1,2*sep+1);
out3+=str.substring(2*sep+1);
}
}
String out3_="";
for(int i=0;i<out3.length();i++)
{

if(Character.isUpperCase(out3.charAt(i)))
{
out3_+=Character.toLowerCase(out3.charAt(i));
}
else
{
out3_+=Character.toUpperCase(out3.charAt(i));

}
}
Result obj=new Result(out1,out2,out3_);
return obj;
OR

LinkedList<String> list1=new LinkedList();


String arr[]={input1,input2,input3};
for(String str:arr)
{
int len=str.length();
int sep=len/3;
if(len%3==0)
{

list1.add(str.substring(0,sep));
list1.add(str.substring(sep,2*sep));
list1.add(str.substring(2*sep));
}
else if(len%3==1)
{
list1.add(str.substring(0,sep));
list1.add(str.substring(sep,2*sep+1));
list1.add(str.substring(2*sep+1));
}
else
{
list1.add(str.substring(0,sep+1));
list1.add(str.substring(sep+1,2*sep+1));
list1.add(str.substring(2*sep+1));
}
}
String out1=list1.get(0)+list1.get(3)+list1.get(6);
String out2=list1.get(1)+list1.get(4)+list1.get(7);
String out=list1.get(2)+list1.get(5)+list1.get(8);
String out3="";
for(int i=0;i<out.length();i++)
{
if(Character.isLowerCase(out.charAt(i)))
{
out3+=Character.toUpperCase(out.charAt(i));
}
else
{
out3+=Character.toLowerCase(out.charAt(i));
}
}
return new Result(out1,out2,out3);

METTL -3

Number based programs


1.Generate series and find Nth element:

int diff=0,next=0;
for(inti=4;i<=input4;i++)
{
diff=input2-input1;
next=input3+diff;
input1=input2;
input2=input3;
input3=next;
}
return next;

OR

int d=0;
for(int i=1;i<=input4-3;i++)
{
d=input3+(input2-input1);
input1=input2;
input2=input3;
input3=d;
}
return d;

2.Find result after alternate add_sub on N:

int result=0;
if(input2==1)
{
for(int i=0;i<=input1;i++)
{
if(i%2==0)
{
result+=input1-i;
}
else
{
result-=input1-i;
}
}
}
else
{
for(int i=0;i<=input1;i++)
{
if(i%2==0&&i!=0)
{
result-=input1-i;
}
else
{
result+=input1-i;
}
}
}
return result;

3.Find password( stable unstable)


intarr[]={input1,input2,input3,input4,input5};
LinkedList<Integer> list=newLinkedList();
int stable=0,unstable=0;
Set<Integer> set=newTreeSet();
for(intnum:arr)
{
int temp=num;
while(num!=0)
{
list.add(num%10);
num/=10;
}
for(int i:list)
{
intfreq=Collections.frequency(list,i);
set.add(freq);
}
if(set.size()==1)
{
stable+=temp;
}
else
{
unstable+=temp;
}
list.clear();
set.clear();
}
return stable-unstable;

4.calculate sum of non-prime index values:


int sum= input1[0]+input1[1];
int index=3;
while(index<input2)
{
int count=0;
for(int j=2;j<index;j++)
{
if(index%j==0)
{
count++;
}
}
if(count!=0)
{
sum+=input1[index];
}
index++;
}
return sum;

5.Find the one digit to be removed to form palindrome:

String input=String.valueOf(input1);
intarr[]=newint[input.length()];
ArrayList<Integer>list=newArrayList();
intccnt=0,result=0,max=0;
for(inti=0;i<input.length();i++)
{
arr[i]=Character.getNumericValue(input.charAt(i));
list.add(arr[i]);
}
for(intin:list)
{
ccnt=Collections.frequency(list, in);
if(ccnt==1)
{

max=in;
if(result<max)
{
result=max;
}
}

}
if(result==0)
{
result=-1;
}
if(input.length()==1)
{
result=-1;
}
return result;

String Based Programs


2.User id generation:

int len1=input1.length();
int len2=input2.length();
String longer="";
String smaller="";
String output="";
if(len1==len2)
{
if(input1.compareTo(input2)>0)
{
longer=input1;
smaller=input2;
}
else
{
longer=input2;
smaller=input1;
}
}
elseif(len1>len2)
{
longer=input1;
smaller=input2;
}
else
{
longer=input2;
smaller=input1;
}
String small=smaller.substring(smaller.length()-1);
String pin=String.valueOf(input3);
String output1=small+longer+pin.charAt(input4-1)+pin.charAt(pin.length()-
input4);
for(inti=0;i<output1.length();i++)
{
if(Character.isLowerCase(output1.charAt(i)))
{
output+=Character.toUpperCase(output1.charAt(i));
}
else
{
output+=Character.toLowerCase(output1.charAt(i));
}
}
return output;

Message controlled Robet movement:

String path[]=input3.split("-");
int x=Integer.valueOf(path[0]);
int y=Integer.valueOf(path[1]);
String dir=path[2];
String arr[]=input4.split(" ");
int err=0;
String ans="";
for(String str:arr)
{
if(str.equals("R"))
{
if(dir.equals("E"))
{
dir="S";
}
elseif(dir.equals("S"))
{
dir="W";
}
elseif(dir.equals("W"))
{
dir="N";
}
else
{
dir="E";
}
}
elseif(str.equals("L"))
{
if(dir.equals("E"))
{
dir="N";
}
elseif(dir.equals("S"))
{
dir="E";
}
elseif(dir.equals("W"))
{
dir="S";
}
else
{
dir="W";
}
}
else
{
if(dir.equals("E"))
{
if(x<input1)
{
x++;
}
else
{
err++;
}
}
elseif(dir.equals("W"))
{
if(x>0)
{
x--;
}
else
{
err++;
}
}
elseif(dir.equals("S"))
{
if(y>0)
{
y--;
}
else
{
err++;
}
}
else
{
if(y<input1)
{
y++;
}
else
{
err++;
}
}
}
}
if(err==0)
{
ans=x+"-"+y+"-"+dir;
}
else
{
ans=x+"-"+y+"-"+dir+"-"+"ER";
}
return ans;

OR
String Dir[]={"NW","N","NE","E","SE","S","SW","W","NW","N","NE"};
String temp[]=input3.split("-");
int x=Integer.valueOf(temp[0]);
int y=Integer.valueOf(temp[1]);
String D=temp[2];
int flag=0;

String robo[]=input4.split(" ");


for(String mov : robo)
{
if(mov.equals("R")|| mov.equals("r"))
{
for(int i=0;i<Dir.length;i++)
{
if(D.equals(Dir[i]))
{
D=(mov.equals("R"))?Dir[i+2]:Dir[i+1];
break;
}

}
}
if(mov.equals("L")|| mov.equals("l"))
{
for(int i=Dir.length-1;i>=0;i--)
{
if(D.equals(Dir[i]))
{
D=(mov.equals("L"))?Dir[i-2]:Dir[i-1];
break;
}

}
}
if(mov.equals("m"))
{
if(D.equals("N")&&(y+1<=input2))
{
y=y+1;
}
elseif(D.equals("S")&&(y-1<=input2)&&(y-1>=0))
{
y=y-1;
}
elseif(D.equals("W")&&(x-1<=input1)&&(x-1>=0))
{
x=x-1;
}
elseif(D.equals("E")&&(x+1<=input1))
{
x=x+1;
}
elseif(D.equals("NE")&&(x+1<=input1)&&(y+1<=input2))
{
x=x+1; y=y+1;
}
elseif(D.equals("SE")&&(x+1<=input1)&&(y-1>=0))
{
x=x+1; y=y-1;
}
elseif(D.equals("SW")&&(x-1>=0)&&(y-1>=0))
{
x=x-1; y=y-1;
}
elseif(D.equals("NW")&&(x-1>=0)&&(y+1<=input2))
{
x=x-1;y=y+1;
}
else
{
flag=1;
break;
}

}
if(mov.equals("M"))
{
if(D.equals("N")&&(y+2<=input2))
{
y=y+2;
}
elseif(D.equals("S")&&(y-2<=input2)&&(y-2>=0))
{
y=y-2;
}
elseif(D.equals("W")&&(x-2<=input1)&&(x-2>=0))
{
x=x-2;
}
elseif(D.equals("E")&&(x+2<=input1))
{
x=x+2;
}
elseif(D.equals("NE")&&(x+2<=input1)&&(y+2<=input2))
{
x=x+2; y=y+2;
}
elseif(D.equals("SE")&&(x+2<=input1)&&(y-2>=0))
{
x=x+2; y=y-2;
}
elseif(D.equals("SW")&&(x-2>=0)&&(y-2>=0))
{
x=x-2; y=y-2;
}
elseif(D.equals("NW")&&(x-2>=0)&&(y+2<=input2))
{
x=x-2;y=y+2;
}
else
{
flag=1;
break;
}
}
}
if(flag==1)
{
return x+"-"+y+"-"+D+"-"+"ER";
}
else
{
return x+"-"+y+"-"+D;
}

Nambiar Number

int num=0,sum=0,face=-1;
String ans="";
for(int i=0;i<input1.length();i++)
{
num=input1.charAt(i)-48;
sum+=num;
if(face==-1)
{
face=(num%2==0)?2:1;
}
if((face==2&&sum%2==1)||(face==1&&sum%2==0))
{
ans+=sum;
face=-1;
sum=0;
}
}

if(face!=-1)
{
ans+=sum;
}
return Integer.valueOf(ans);

You might also like