Mettl Programs
Mettl Programs
Mettl Programs
Is even
if (input1%2==0)
{
return 2;
}
else
{
return 1;
}
Is odd
if (input1%2!=0)
{
return 2;
}
else
{
return 1;
}
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;
}
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;
int arr[]={input1,input2,input3,input4,input5};
int count=0;
for(int i:arr)
{
if(i%2!=0)
{
count++;
}
}
return count;
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;
}
}
}
Day 7
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;
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();
}
return count;
OR
TreeSet<Integer> tset = new TreeSet<Integer>();
while(input1!=0)
{
tset.add(input1%10);
input1/=10;
}
return tset.size();
}
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
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
int sum=0, a;
while(input1!=0)
{
a=input1%10;
if(a%2==0)
{
sum+=a;
}
input1=input1/10;
}
return sum;
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;
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;
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) :
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;
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)
//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
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++;
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
if(freq>=maxfreq)
{
maxfreq=freq;
ans=i;
}
}
return ans;
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;
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);
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
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
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;
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;
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;
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;
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;
}
}
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);