21 June 2018
TIC TAC TOE GAME (JAVA GAME)
RUN:
player X: pls,enter the row and column
0
0
X | | |
------------
| | |
------------
| | |
------------
player Y: pls,enter the row and column
1
1
X | | |
------------
| O | |
------------
| | |
------------
player X: pls,enter the row and column
1
0
X | | |
------------
X | O | |
------------
| | |
------------
player Y: pls,enter the row and column
2
0
X | | |
------------
X | O | |
------------
O | | |
------------
player X: pls,enter the row and column
2
1
X | | |
-------------
X | O | |
-------------
O | X | |
-------------
player Y: pls,enter the row and column
0
2
X | | O |
-------------
X | O | |
-------------
O | X | |
-------------
Player Y wins the match
import java.util.Scanner;
public class main {
void display(char c[][]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (c[i][j] == '\0') {
System.out.print(" |");
} else {
System.out.print(" " + c[i][j] + " |");
}
}
System.out.println();
System.out.println("------------");
}
}
boolean result(boolean a, char c, int l) {
if (a == true && c == 'X') {
System.out.println("Player X wins the game");
return true;
} else if (a == true && c == 'O') {
System.out.println("Player Y wins the match");
return true;
} else if (a == false && l == 8) {
System.out.println("game over draw");
return true;
}
return false;
}
boolean checkWins(char c[][]) {
//we done also using row,column,diagonal,anti-diagonal wise using for loop but it will not efficiency.
//for optimal solution i use multiple if statement.
if (c[0][1] != '\0' && c[0][1] == c[0][2] && c[0][1] == c[0][0]) {
return true;
} else if (c[1][0] != '\0' && c[1][0] == c[0][0] && c[2][0] == c[1][0]) {
return true;
} else if (c[1][1] != '\0' && c[1][1] == c[1][0] && c[1][1] == c[1][2] || c[1][1] != '\0' && c[1][1] == c[0][1] && c[1][1] == c[2][1]) {
return true;
} else if (c[1][2] != '\0' && c[1][2] == c[0][2] && c[1][2] == c[2][2]) {
return true;
} else if (c[2][1] != '\0' && c[2][1] == c[2][0] && c[2][1] == c[2][2]) {
return true;
} else if (c[0][0] != '\0' && c[0][0] == c[1][1] && c[2][2] == c[1][1] || c[0][2] != '\0' && c[0][2] == c[1][1] && c[2][0] == c[1][1]) {
return true;
}
return false;
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
char c[][] = new char[3][3];
main ob = new main();
boolean a = false;
int l = 0, i, j;
while (l < 9) { // TO GET N VALUES
while (true) { //TO CHECK THE BOX IS ALREADY FILLED
if (l % 2 == 0) {
System.out.println("player X: pls,enter the row and column");
i = s.nextInt();
j = s.nextInt();
if (c[i][j] == '\0') {
c[i][j] = 'X';
break;
} else {
System.out.println("already filled pls enter new one:");
continue;
}
} else {
System.out.println("player Y: pls,enter the row and column");
i = s.nextInt();
j = s.nextInt();
if (c[i][j] == '\0') {
c[i][j] = 'O';
break;
} else {
System.out.println("already filled pls enter new one:");
continue;
}
}
}
ob.display(c);
a = ob.checkWins(c);
a = ob.result(a, c[i][j], l);
if (a) {
break;
}
l++;
}
}
}
16 June 2018
Majority Element II
Given an integer array of size n, find all elements that appear more than
Note: The algorithm should run in linear time and in O(1) space.
Example 1:
⌊ n/3 ⌋ times.Note: The algorithm should run in linear time and in O(1) space.
Example 1:
Input: [3,2,3] Output: [3]Example 2:
Input: [1,1,1,3,3,2,2,2] Output: [1,2]
import java.util.*;
public class NewClass7 {
HashMap<Integer,Integer> ar=new HashMap<Integer,Integer>();
public void majorityElement(int[] nums) {
for(int i=0;i<nums.length;i++)
{
if(ar.containsKey(nums[i]))
{
ar.put(nums[i],ar.get(nums[i])+1);
}else
{
ar.put(nums[i],1);
}
}
for(int i:ar.keySet())
{
if(ar.get(i)>nums.length/3)
{
System.out.print(i+" ");
}
}
}
public static void main(String args[])
{
int ar[]={1,1,1,3,3,2,2,2};
NewClass7 ob=new NewClass7();
ob.majorityElement(ar);
}
}
print frequency of characters using efficient method
i/p:
c[]={'a','a','d','a'}
o/p:
a=3 d=1
import java.util.*;
public class NewClass {
void frequency(HashMap<Character,Integer> a,char c[])
{
for(int i=0;i<c.length;i++)
{
if(a.containsKey(c[i]))
{
a.put( c[i],a.get(c[i])+1);
}
else
a.put(c[i],1);
}
}
public static void main(String args[])
{
char c[]={'a','b','b','e','d'};
HashMap<Character,Integer> a=new HashMap<Character,Integer>();
NewClass ob=new NewClass();
ob.frequency(a,c);
System.out.print(a);
}
}
c[]={'a','a','d','a'}
o/p:
a=3 d=1
import java.util.*;
public class NewClass {
void frequency(HashMap<Character,Integer> a,char c[])
{
for(int i=0;i<c.length;i++)
{
if(a.containsKey(c[i]))
{
a.put( c[i],a.get(c[i])+1);
}
else
a.put(c[i],1);
}
}
public static void main(String args[])
{
char c[]={'a','b','b','e','d'};
HashMap<Character,Integer> a=new HashMap<Character,Integer>();
NewClass ob=new NewClass();
ob.frequency(a,c);
System.out.print(a);
}
}
Valid Parentheses
Example 1:
Example 3:
Example 4:
Example 5:
Input: "()" Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]" Output: false
Example 4:
Input: "([)]" Output: false
Example 5:
Input: "{[]}"
Output: true
import java.util.*;
public class NewClass1 {
boolean parenthesis(String s)
{
Stack<Character> st=new Stack<Character>();
HashMap<Character,Character> hm=new HashMap<Character,Character>();
hm.put('{','}');
hm.put('(',')');
hm.put('[',']');
for(int i=0;i<s.length();i++)
{
if(hm.containsKey(s.charAt(i)))
{
st.push(s.charAt(i));
}else if(hm.containsValue(s.charAt(i)))
{
if(!st.empty() && hm.get(st.peek())==s.charAt(i))
{
st.pop();
}else
return false;
}
}
if(st.empty())
return true;
return false;
}
public static void main(String args[])
{
String k="(()())";
NewClass1 ob=new NewClass1();
boolean a=ob.parenthesis(k);
System.out.println(a);
}
}
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example,
i/p:
n = 3
o/p:
For example,
i/p:
n = 3
o/p:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
public class NewClass2 {
Set<String> br=new HashSet<String>();
void generate()
{
for(String i:br)
System.out.println(i);
}
void checkBalanced(String name)
{
HashMap<Character,Character> hm=new HashMap<Character,Character>();
hm.put('(',')');
Stack<Character> st=new Stack<Character>();
int c=0;
for(int i=0;i<name.length();i++)
{
if(hm.containsKey(name.charAt(i)))
{
st.push(name.charAt(i));
}else if(hm.containsValue(name.charAt(i)))
{
if(!st.empty() && hm.get(st.peek())==name.charAt(i))
{
st.pop();
}else
{c=1;
break;
}
}else
{
c=1;
break;
}
}
if(c==0 && st.empty())
br.add(name);
}
String swap(String s,int i,int j)
{
char c[]=s.toCharArray();
char t=c[i];
c[i]=c[j];
c[j]=t;
return String.valueOf(c);
}
void per(String s,int l,int r)
{
if(l==r)
checkBalanced(s);
for(int i=l;i<r;i++)
{
s=swap(s,l,i);
per(s,l+1,r);
s=swap(s,l,i);
}
}
public static void main(String args[])
{
int n=3;
String s="";
for(int i=0;i<n;i++)
{
s+="()";
}
NewClass2 ob=new NewClass2();
ob.per(s,0,s.length());
ob.generate();
}
}
First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Example 2:
Example 3:
import java.util.*;
class NewClass5 {
HashMap<Integer,Integer> arr=new HashMap<Integer,Integer>();
int Max(int nums[])
{
int max=nums[0];
for(int i=0;i<nums.length;i++)
{
if(nums[i]>max)
max=nums[i];
arr.put(nums[i], 1);
}
return max;
}
void firstMissingPositive(int max) {
for(int i=1;i<=max+1;i++)
{
if(!arr.containsKey(i)){
System.out.println(i); break;}
}
}
public static void main(String args[])
{
int ar[]={3,4,-1,1};
NewClass5 ob=new NewClass5();
int max=ob.Max(ar);
ob.firstMissingPositive(max);
}
}
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
import java.util.*;
class NewClass5 {
HashMap<Integer,Integer> arr=new HashMap<Integer,Integer>();
int Max(int nums[])
{
int max=nums[0];
for(int i=0;i<nums.length;i++)
{
if(nums[i]>max)
max=nums[i];
arr.put(nums[i], 1);
}
return max;
}
void firstMissingPositive(int max) {
for(int i=1;i<=max+1;i++)
{
if(!arr.containsKey(i)){
System.out.println(i); break;}
}
}
public static void main(String args[])
{
int ar[]={3,4,-1,1};
NewClass5 ob=new NewClass5();
int max=ob.Max(ar);
ob.firstMissingPositive(max);
}
}
15 June 2018
merging two sorted arrays with O(n)
Input: ar1[] = {10};
ar2[] = {2, 3};
Output: ar1[] = {2}
ar2[] = {3, 10}
Input: ar1[] = {1, 5, 9, 10, 15, 20};
ar2[] = {2, 3, 8, 13};
Output: ar1[] = {1, 2, 3, 5, 8, 9}
ar2[] = {10, 13, 15, 20}
public class NewClass33 {
void print(int ar[],int br[])
{
for(int i=0;i<ar.length;i++)
System.out.print(ar[i]+" ");
System.out.println();
for(int j=0;j<br.length;j++)
System.out.print(br[j]+" ");
}
void Sort(int ar[],int i)
{
for(int j=i+1;j<ar.length;j++)
{
if(ar[i]>ar[j])
{
ar[i]=ar[i]+ar[j];
ar[j]=ar[i]-ar[j];
ar[i]=ar[i]-ar[j];
}
}
if(i<ar.length-1)
{
Sort(ar,++i);
}
}
void ArraySort(int ar[],int br[],int i)
{
for(int j=0;j<br.length;j++)
{
if(ar[i]>br[j])
{
ar[i]=ar[i]+br[j];
br[j]=ar[i]-br[j];
ar[i]=ar[i]-br[j];
}
}
if(i<ar.length-1)
ArraySort(ar,br,++i);
}
public static void main(String args[])
{
int ar[]={1,2,9,10,15,20};
int br[]={2,3,8,13};
NewClass33 ob=new NewClass33();
ob.ArraySort(ar,br,0);
ob.Sort(br,0);
ob.print(ar,br);
}
}
Smallest greater elements in whole array
nput : 6 3 9 8 10 2 1 15 7 Output : 7 6 10 9 15 3 2 _ 8 Here every element of array has next greater element but at index 7, 15 is the greatest element of given array and no other element is greater from 15 so at the index of 15 we fill with '_' . Input : 13 6 7 12 Output : _ 7 12 13 Here, at index 0, 13 is the greatest value in given array and no other array element is greater from 13 so at index 0 we fill '_'.
public class NewClass32 {
void nextgreatest(int ar[])
{
for(int i=0;i<ar.length;i++)
{
int max=Integer.MAX_VALUE,c=0;
for(int j=0;j<ar.length;j++)
{
if(ar[i]<ar[j] && ar[j]<max)
{ max=ar[j]; c=1; }
}
if(c==1)
System.out.print(max+" ");
else
System.out.print("_ ");
}
}
public static void main(String args[])
{
int ar[]={6,3,9,8,10,2,1,15,7};
NewClass32 ob=new NewClass32();
ob.nextgreatest(ar);
}
}
print mid element of sum of left & right in array
array of numbers were given to find a number which has same sum of numbers in it’s either side.
I/p 1, 2, 3, 7, 6
O/p 7(has 1+ 2+3 in left 6 in right)
public class NewClass31 {
void side(int ar[])
{
int c=1,sum1;
for(int i=0;i<ar.length-2;i++)
{
sum1=0;
for(int j=0;j<ar.length;j++)
{
if(j<c)
sum1+=ar[j];
if(j>c)
sum1-=ar[j];
}
if(sum1==0)
System.out.println(ar[c]);
c++;
}
}
public static void main(String args[])
{
int ar[]={-2, 9, -9, 8, -1};
NewClass31 ob=new NewClass31();
ob.side(ar);
}
}
I/p 1, 2, 3, 7, 6
O/p 7(has 1+ 2+3 in left 6 in right)
public class NewClass31 {
void side(int ar[])
{
int c=1,sum1;
for(int i=0;i<ar.length-2;i++)
{
sum1=0;
for(int j=0;j<ar.length;j++)
{
if(j<c)
sum1+=ar[j];
if(j>c)
sum1-=ar[j];
}
if(sum1==0)
System.out.println(ar[c]);
c++;
}
}
public static void main(String args[])
{
int ar[]={-2, 9, -9, 8, -1};
NewClass31 ob=new NewClass31();
ob.side(ar);
}
}
anagram in string array
It’s about anagram.i/p was array of strings .and a word was given to find whether it has anagram in given array.
I/p catch, got, tiger, mat, eat, Pat, tap, tea
Word: ate
O/p eat, tea
public class NewClass30 {
int changeanagram(String a,String b)
{
char c[]=b.toCharArray();
int count=0;
for(int i=0;i<a.length();i++)
{
for(int j=0;j<b.length();j++)
{
if(c[j]==a.charAt(i) && c[j]!='*')
{
count++;
c[j]='*';
}
}
}if(count==a.length())
{
return 1;
}return 0;
}
void checkanagrams(String k,String ar[])
{
for(int i=0;i<ar.length;i++)
{ int c=0;
if(k.length()==ar[i].length())
{
c=changeanagram(k,ar[i]);
}if(c==1)
System.out.print(ar[i]+" ");
}
}
public static void main(String args[])
{
String ar[]={"catch","got","tiger","mat","eat","pat","tap","tea"};
String k="tpa";
NewClass30 ob=new NewClass30();
ob.checkanagrams(k, ar);
}
}
I/p catch, got, tiger, mat, eat, Pat, tap, tea
Word: ate
O/p eat, tea
public class NewClass30 {
int changeanagram(String a,String b)
{
char c[]=b.toCharArray();
int count=0;
for(int i=0;i<a.length();i++)
{
for(int j=0;j<b.length();j++)
{
if(c[j]==a.charAt(i) && c[j]!='*')
{
count++;
c[j]='*';
}
}
}if(count==a.length())
{
return 1;
}return 0;
}
void checkanagrams(String k,String ar[])
{
for(int i=0;i<ar.length;i++)
{ int c=0;
if(k.length()==ar[i].length())
{
c=changeanagram(k,ar[i]);
}if(c==1)
System.out.print(ar[i]+" ");
}
}
public static void main(String args[])
{
String ar[]={"catch","got","tiger","mat","eat","pat","tap","tea"};
String k="tpa";
NewClass30 ob=new NewClass30();
ob.checkanagrams(k, ar);
}
}
search on dictionary
Given an input string and a dictionary of words, find out if the
input string can be segmented into a space-separated sequence of
dictionary words. See following examples for more details.
public class NewClass27 {
void print(String k,String res)
{
if(k.equals(res))
{
System.out.println("yes");
}else
{
System.out.println("No");
}
}
int dictionary(String ar[],String a)
{
int k=0;
for(int i=0;i<ar.length;i++)
{
if(ar[i].equals(a))
{
k=1;
}
}return k;
}
String search(String k,String ar[])
{ String temp="",result="";
int t;
for(int i=0;i<k.length();i++)
{
for(int j=k.length();j>=i+1;j--)
{
temp=k.substring(i,j);
t=dictionary(ar,temp);
if(t==1)
{
result+=temp;
i=j-1;
break;
}
}
}return result;
}
public static void main(String args[])
{
String dic[]={"i","like","sam","sung","samsung","mobile","ice",
"cream","icecream","man","go","mango"};
String k="ilikemangogomanice";
String result="";
NewClass27 ob=new NewClass27();
result=ob.search(k, dic);
ob.print(k,result);
}
}
Consider the following dictionary
{ i, like, sam, sung, samsung, mobile, ice,
cream, icecream, man, go, mango}
Input: ilike
Output: Yes
The string can be segmented as "i like".
Input: ilikesamsung
Output: Yes
The string can be segmented as "i like samsung"
or "i like sam sung".<>
public class NewClass27 {
void print(String k,String res)
{
if(k.equals(res))
{
System.out.println("yes");
}else
{
System.out.println("No");
}
}
int dictionary(String ar[],String a)
{
int k=0;
for(int i=0;i<ar.length;i++)
{
if(ar[i].equals(a))
{
k=1;
}
}return k;
}
String search(String k,String ar[])
{ String temp="",result="";
int t;
for(int i=0;i<k.length();i++)
{
for(int j=k.length();j>=i+1;j--)
{
temp=k.substring(i,j);
t=dictionary(ar,temp);
if(t==1)
{
result+=temp;
i=j-1;
break;
}
}
}return result;
}
public static void main(String args[])
{
String dic[]={"i","like","sam","sung","samsung","mobile","ice",
"cream","icecream","man","go","mango"};
String k="ilikemangogomanice";
String result="";
NewClass27 ob=new NewClass27();
result=ob.search(k, dic);
ob.print(k,result);
}
}
print adjacent of two strings
Two strings of equal length will be given. Print all the adjacent pairs which are not equal.
Input: asdfghij and adsfgijh
Output: sd-ds, hij-ijh
public class NewClass26 {
String reverse(String k)
{
String l="";
for(int i=k.length()-1;i>=0;i--)
l+=k.charAt(i);
return l;
}
void seperate(String a,String b)
{
String x="",y="",z="";
for(int i=0;i<a.length();i++)
{
for(int j=b.length()-1;j>=i+1;j--)
{
x=a.substring(i,j+1);
y=b.substring(i,j+1);
z=reverse(y);
if(x.equals(z))
{
System.out.println(x+"-"+y);
i=j;
break;
}
}
}
}
public static void main(String args[])
{
String a="adsfgijh";
String b="asdfghji";
NewClass26 ob=new NewClass26();
ob.seperate(a,b);
}
}
Input: asdfghij and adsfgijh
Output: sd-ds, hij-ijh
public class NewClass26 {
String reverse(String k)
{
String l="";
for(int i=k.length()-1;i>=0;i--)
l+=k.charAt(i);
return l;
}
void seperate(String a,String b)
{
String x="",y="",z="";
for(int i=0;i<a.length();i++)
{
for(int j=b.length()-1;j>=i+1;j--)
{
x=a.substring(i,j+1);
y=b.substring(i,j+1);
z=reverse(y);
if(x.equals(z))
{
System.out.println(x+"-"+y);
i=j;
break;
}
}
}
}
public static void main(String args[])
{
String a="adsfgijh";
String b="asdfghji";
NewClass26 ob=new NewClass26();
ob.seperate(a,b);
}
}
pattern bi-triangle
i/p:
4
o/p:
1 3 2 6 5 4 10 9 8 7 10 9 8 7 6 5 4 3 2 1
public class NewClass25 {
void pat(int n)
{
int len=n*2,c=0,p=0,k=0;
for(int i=1;i<=len;i++)
{
if(i<=n){
c+=i; k=c; p=c;}
for(int j=1;j<=n;j++)
{
if(i<=n && j<=n-i)
{
System.out.print(" ");
}else if(i<=n)
{
System.out.printf(" %-2d",k--);
}else if(i>n && j<i-n)
{
System.out.print(" ");
}else
{
System.out.printf(" %-2d",p--);
}
}System.out.println();
}
}
public static void main(String args[])
{
int n=9;
NewClass25 ob=new NewClass25();
ob.pat(n);
}
}
play with numbers
Given an array as input, The condition is if the number is repeated
you must add the number and put the next index value to 0. If the number
is 0 print it at the last.
public class NewClass24 {
void print(int ar[],int n)
{ int c=0;
for(int i=0;i<n;i++)
{
if(ar[i]!=0)
{
System.out.print(ar[i]+" ");
}else
{
c++;
}
}
for(int i=0;i<c;i++)
System.out.print(0+" ");
}
void repeat(int ar[],int n)
{
for(int i=0;i<n-1;i++)
{
if(ar[i]==ar[i+1])
{
ar[i]+=ar[i+1];
ar[i+1]=0;
}
}
}
public static void main(String args[])
{
int ar[]={0,2,2,2,0,6,6,0,8};
NewClass24 ob=new NewClass24();
ob.repeat(ar,ar.length);
ob.print(ar,ar.length);
}
}
Eg: arr[] = { 0, 2, 2, 2, 0, 6, 6, 0, 8}
Output: 4 2 12 8 0 0 0 0 0
public class NewClass24 {
void print(int ar[],int n)
{ int c=0;
for(int i=0;i<n;i++)
{
if(ar[i]!=0)
{
System.out.print(ar[i]+" ");
}else
{
c++;
}
}
for(int i=0;i<c;i++)
System.out.print(0+" ");
}
void repeat(int ar[],int n)
{
for(int i=0;i<n-1;i++)
{
if(ar[i]==ar[i+1])
{
ar[i]+=ar[i+1];
ar[i+1]=0;
}
}
}
public static void main(String args[])
{
int ar[]={0,2,2,2,0,6,6,0,8};
NewClass24 ob=new NewClass24();
ob.repeat(ar,ar.length);
ob.print(ar,ar.length);
}
}
14 June 2018
Find New Friends in Social Network
In a social network, a person can invite friends of his/her friend. John wants to invite and add new friends. Complete the program below so that it prints the names of the persons to whom John can send a friend request.
Input format:
The first line contains the value of the N which represent the number of friends. N lines contain the name of the friend F followed by the number of friends of F finally their names separated by space.
Input:
Output:3 Mani 3 Ram Raj Guna Ram 2 Kumar Kishore Mughil 3 Praveen Naveen Mani
Explanation:Raj Guna Kumar Kishore Praveen Naveen
Ram is not present in the output as Ram is already John's friend.
import java.util.*;
public class NewClass22 {
void findFriend(int n,String a[],ArrayList<String> ar)
{
for(int i=0;i<ar.size();i++)
{
int c=0;
for(int j=0;j<a.length;j++)
{
if(ar.get(i).toLowerCase().equals(a[j].toLowerCase()))
{
c=1;
break;
}
} if(c==0)
System.out.print(ar.get(i)+" ");
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
ArrayList<String> ar=new ArrayList<String>();
int n=s.nextInt();
String a[]=new String[n];
for(int i=0;i<n;i++)
{
a[i]=s.next();
int l=s.nextInt();
for(int j=0;j<l;j++)
{
ar.add(s.next());
}
}
NewClass22 ob=new NewClass22();
ob.findFriend(n,a,ar);
}
}
Series of number consists only 3 & 4
i/p:
9
o/p:
3 4 33 34 43 44 333 334 343
import java.util.*;
public class NewClass21 {
void print(int ar[],int n)
{
for(int i=0;i<n;i++)
System.out.print(ar[i]+" ");
}
void series(int ar[],int n)
{
ar[0]=3; ar[1]=4;
int k=1,i;
for(i=0;k<n;i++) //or k<n-2
{
ar[++k]=(ar[i]*10)+3;
ar[++k]=(ar[i]*10)+4;
}
//or if(n%2==1)
// ar[++k]=ar[i]*10+3;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int ar[]=new int[n+2]; //or n
NewClass21 ob=new NewClass21();
ob.series( ar,n);
ob.print(ar,n);
}
}
9
o/p:
3 4 33 34 43 44 333 334 343
import java.util.*;
public class NewClass21 {
void print(int ar[],int n)
{
for(int i=0;i<n;i++)
System.out.print(ar[i]+" ");
}
void series(int ar[],int n)
{
ar[0]=3; ar[1]=4;
int k=1,i;
for(i=0;k<n;i++) //or k<n-2
{
ar[++k]=(ar[i]*10)+3;
ar[++k]=(ar[i]*10)+4;
}
//or if(n%2==1)
// ar[++k]=ar[i]*10+3;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int ar[]=new int[n+2]; //or n
NewClass21 ob=new NewClass21();
ob.series( ar,n);
ob.print(ar,n);
}
}
pattern for lower triangular
i/p:
5
o/p:
1
12 2
11 13 3
10 15 14 4
9 8 7 6 5
i/p:
10
o/p:
1
27 2
26 28 3
25 45 29 4
24 44 46 30 5
23 43 54 47 31 6
22 42 53 0 48 32 7
21 41 52 51 50 49 33 8
20 40 39 38 37 36 35 34 9
19 18 17 16 15 14 13 12 11 10
import java.util.*;
public class NewClass20 {
void print(int ar[][],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.printf("%-2d ", ar[i][j]);
}System.out.println();
}
}
void triangularpat(int ar[][],int n)
{
int t=0,l=0,b=n-1,r=n-1,c=1;
while(t<b && l<r)
{
for(int i=t,j=l;i<=b&&j<=r;i++,j++)
{
ar[i][j]=c++;
}t++; r--;
for(int i=r;i>=l;i--)
{
ar[b][i]=c++;
}
r--;b--;
for(int i=b;i>=t;i--)
{
ar[i][l]=c++;
}
t++;l++;
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int ar[][]=new int[n][n];
NewClass20 ob=new NewClass20();
ob.triangularpat(ar, n);
ob.print(ar,n);
}
}
5
o/p:
1
12 2
11 13 3
10 15 14 4
9 8 7 6 5
i/p:
10
o/p:
1
27 2
26 28 3
25 45 29 4
24 44 46 30 5
23 43 54 47 31 6
22 42 53 0 48 32 7
21 41 52 51 50 49 33 8
20 40 39 38 37 36 35 34 9
19 18 17 16 15 14 13 12 11 10
import java.util.*;
public class NewClass20 {
void print(int ar[][],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.printf("%-2d ", ar[i][j]);
}System.out.println();
}
}
void triangularpat(int ar[][],int n)
{
int t=0,l=0,b=n-1,r=n-1,c=1;
while(t<b && l<r)
{
for(int i=t,j=l;i<=b&&j<=r;i++,j++)
{
ar[i][j]=c++;
}t++; r--;
for(int i=r;i>=l;i--)
{
ar[b][i]=c++;
}
r--;b--;
for(int i=b;i>=t;i--)
{
ar[i][l]=c++;
}
t++;l++;
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int ar[][]=new int[n][n];
NewClass20 ob=new NewClass20();
ob.triangularpat(ar, n);
ob.print(ar,n);
}
}
print pattern
i/p:
6
o/p:
1st quad | 2nd quad -->for understating
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0
3rd quad | 4th quad ->for understanding
public class NewClass19 {
void pattern(int n)
{
int len=(2*n)+1;
for(int i=0;i<len;i++)
{ int c=0,k=i,l=len-i-1;
for(int j=0;j<len;j++)
{
if(i<= n &&j<=i ) //1st quad
{
System.out.print(j+" ");
}
else if( i<=n && j>=len-1-i) //2nd quad
{
if(i==n){ k--; System.out.print(k+" "); }
else
System.out.print((k--)+" ");
}
else if(i>n && j<(len-i)) //3rd quad
{
System.out.print(j+" ");
}
else if(i>n && j>=i) //4th quad
{
System.out.print(l--+" ");
}
else
System.out.print(0+" ");
}System.out.println();
}
}
public static void main(String args[])
{
int n=6;
NewClass19 ob=new NewClass19();
ob.pattern(n);
}
}
6
o/p:
1st quad | 2nd quad -->for understating
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0
3rd quad | 4th quad ->for understanding
public class NewClass19 {
void pattern(int n)
{
int len=(2*n)+1;
for(int i=0;i<len;i++)
{ int c=0,k=i,l=len-i-1;
for(int j=0;j<len;j++)
{
if(i<= n &&j<=i ) //1st quad
{
System.out.print(j+" ");
}
else if( i<=n && j>=len-1-i) //2nd quad
{
if(i==n){ k--; System.out.print(k+" "); }
else
System.out.print((k--)+" ");
}
else if(i>n && j<(len-i)) //3rd quad
{
System.out.print(j+" ");
}
else if(i>n && j>=i) //4th quad
{
System.out.print(l--+" ");
}
else
System.out.print(0+" ");
}System.out.println();
}
}
public static void main(String args[])
{
int n=6;
NewClass19 ob=new NewClass19();
ob.pattern(n);
}
}
count sum based on given kth value
Given an array and a threshold value k where k is used to divide each element of the array. Find the total number of divisions we get after dividing each element of the array by k.
for example:
A[ ] = 5 8 10 13 6 2 and k is 3
Output will be 17
Explanation:
Number Parts counts
5 {3,2} 2
8 {3,3,2} 3
10 {3,3,3,1} 4
13 {3,3,3,3,1} 5
6 {3,3} 2
2 {2} 1
The result thus will be 2+3+4+5+2+1 = 17
for example:
A[ ] = 5 8 10 13 6 2 and k is 3
Output will be 17
Explanation:
Number Parts counts
5 {3,2} 2
8 {3,3,2} 3
10 {3,3,3,1} 4
13 {3,3,3,3,1} 5
6 {3,3} 2
2 {2} 1
The result thus will be 2+3+4+5+2+1 = 17
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N and threshold value k, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the total count.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 500
1 ≤ A[i] ≤ 1000
1 ≤ Threshold value(k) ≤ 20
1 ≤ N ≤ 500
1 ≤ A[i] ≤ 1000
1 ≤ Threshold value(k) ≤ 20
Example:
Input
1
6 3
5 8 10 13 6 2
1
6 3
5 8 10 13 6 2
Output
17
17
public class NewClass18 {
void sum(int ar[],int k)
{
int sum=0;
for(int i=0;i<ar.length;i++)
{
sum+=ar[i]/k;
if(ar[i]%k!=0)
sum=sum+1;
}
System.out.println(sum);
}
public static void main(String args[])
{
int ar[]={5,8,10,13,6,2};
int k=3;
NewClass18 ob=new NewClass18();
ob.sum(ar, k);
}
}
sum palindrome
A given number check it palindrome or not
under following conditions,
1, you can add up to 5times of reversed number is possible.
2, until the addition of 5times it will not palindrome it will returns -1.
explanation:
i.p:
no.of.case
numbers
sample
o.p:
palindrome or -1
samples:
i/p:
4
987
979783
23
33
o/p:
66066 (987+789=1776 ,1776+6771=8547 ,8547+7458=16005 ,16005+50061=66066)
-1
55 (23+32=55)
33
import java.util.*;
public class NewClass17 {
int reverse(int a)
{
int rev=0;
while(a>0)
{
rev=rev*10+(a%10);
a=a/10;
}
return rev;
}
void palindrome(int k)
{
int i;
for(i=0;i<5;i++)
{
int l=reverse(k);
if(l==k)
{
System.out.println(l);
break;
}else
{
k=k+l;
}
}
if(i==5)
System.out.println(-1);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
NewClass17 ob=new NewClass17();
for(int i=0;i<n;i++)
{
int k=s.nextInt();
ob.palindrome(k);
}
}
}
under following conditions,
1, you can add up to 5times of reversed number is possible.
2, until the addition of 5times it will not palindrome it will returns -1.
explanation:
i.p:
no.of.case
numbers
sample
o.p:
palindrome or -1
samples:
i/p:
4
987
979783
23
33
o/p:
66066 (987+789=1776 ,1776+6771=8547 ,8547+7458=16005 ,16005+50061=66066)
-1
55 (23+32=55)
33
import java.util.*;
public class NewClass17 {
int reverse(int a)
{
int rev=0;
while(a>0)
{
rev=rev*10+(a%10);
a=a/10;
}
return rev;
}
void palindrome(int k)
{
int i;
for(i=0;i<5;i++)
{
int l=reverse(k);
if(l==k)
{
System.out.println(l);
break;
}else
{
k=k+l;
}
}
if(i==5)
System.out.println(-1);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
NewClass17 ob=new NewClass17();
for(int i=0;i<n;i++)
{
int k=s.nextInt();
ob.palindrome(k);
}
}
}
13 June 2018
calculate sum of primes present as digits of given number N.
i/p:
1st line:-nbr of test case (n).
nextlines:- line:-numbers for(n)
sample
i/p:
3
123456
123456789
111111111
o/p:
10 (2+3+5)
17(2+3+5+7)
0(none of are the prime)
import java.util.*;
public class NewClass16 {
void primesum(String s)
{
int sum=0,i=0;
while(i<s.length())
{
int a=s.charAt(i)-'0';
if(a%10==2 || a%10==3 || a%10==5 || a%10==7)
sum+=a%10;
i++;
}
System.out.println(sum);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
NewClass16 ob=new NewClass16();
for(int i=0;i<n;i++)
{
String k=s.next();
ob.primesum(k);
}
}
}
1st line:-nbr of test case (n).
nextlines:- line:-numbers for(n)
sample
i/p:
3
123456
123456789
111111111
o/p:
10 (2+3+5)
17(2+3+5+7)
0(none of are the prime)
import java.util.*;
public class NewClass16 {
void primesum(String s)
{
int sum=0,i=0;
while(i<s.length())
{
int a=s.charAt(i)-'0';
if(a%10==2 || a%10==3 || a%10==5 || a%10==7)
sum+=a%10;
i++;
}
System.out.println(sum);
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
NewClass16 ob=new NewClass16();
for(int i=0;i<n;i++)
{
String k=s.next();
ob.primesum(k);
}
}
}
alternate sorting 2
to sort the elements in odd positions in descending order and elements in even positions ascending order
i/p:6,5,4,9,8,7,1
o/p:
1,9,4,7,6,5,8
public class NewClass15 {
void print(int ar[])
{
for(int i=0;i<ar.length;i++)
System.out.print(ar[i]+" ");
}
void alternatesort(int ar[],int i)
{
if(i%2==0){
for(int j=i+2;j<ar.length;j+=2)
{
if(ar[i]>ar[j])
{
ar[i]=ar[i]+ar[j];
ar[j]=ar[i]-ar[j];
ar[i]=ar[i]-ar[j];
}
}
}
else {
for(int j=i+2;j<ar.length;j+=2)
{
if(ar[i]<ar[j])
{
ar[i]=ar[i]+ar[j];
ar[j]=ar[i]-ar[j];
ar[i]=ar[i]-ar[j];
}
}
}
if(i<ar.length-1)
alternatesort(ar,++i);
}
public static void main(String args[])
{
NewClass15 ob=new NewClass15();
int ar[]={5,4,3,9,8,7,2};
ob.alternatesort(ar,0);
ob.print(ar);
}
}
encode the string
i/p:
aaaabbbc
o/p:
a4b3c1
public class NewClass14 {
void print(char c,int n)
{
System.out.print(c+""+n);
}
void encode(String s)
{
for(int i=0;i<s.length();i++)
{
char temp=s.charAt(i);
i++;
int c=1;
while(i<s.length())
{
if(temp==s.charAt(i))
{ c++; i++; }
else {
i--;
break;
}
}
print(temp,c);
}
}
public static void main(String args[])
{
String s="aaaabbbc";
NewClass14 ob=new NewClass14();
ob.encode(s);
}
}
Decode the given string
exaample 1:
i/p:
a2b3c
o/p:
aabbb (c will be 0)
example 2:
i/p:
a12b12c100
o/p:
aaaaaaaaaaaabbbbbbbbbbbbccccccccccccccc...
ccccccccccccccccccccccccccccccccccccccccc...
cccccccccccccccccccccccccccccccccccccccccccc
public class NewClass13 {
void print(char c,int n)
{
for(int i=0;i<n;i++)
System.out.print(c);
}
void decode(String s)
{
for(int i=0;i<s.length();i++)
{
char temp=s.charAt(i);
i++;
int c=0;
while(i<s.length())
{
if(s.charAt(i)>='0' && s.charAt(i)<='9')
{
c=c*10+(s.charAt(i)-'0');
i++;
}
else
{
i--;
break;
}
}
print(temp,c);
}
}
public static void main(String args[])
{
String s="a12b12c100";
NewClass13 ob=new NewClass13();
ob.decode(s);
}
}
i/p:
a2b3c
o/p:
aabbb (c will be 0)
example 2:
i/p:
a12b12c100
o/p:
aaaaaaaaaaaabbbbbbbbbbbbccccccccccccccc...
ccccccccccccccccccccccccccccccccccccccccc...
cccccccccccccccccccccccccccccccccccccccccccc
public class NewClass13 {
void print(char c,int n)
{
for(int i=0;i<n;i++)
System.out.print(c);
}
void decode(String s)
{
for(int i=0;i<s.length();i++)
{
char temp=s.charAt(i);
i++;
int c=0;
while(i<s.length())
{
if(s.charAt(i)>='0' && s.charAt(i)<='9')
{
c=c*10+(s.charAt(i)-'0');
i++;
}
else
{
i--;
break;
}
}
print(temp,c);
}
}
public static void main(String args[])
{
String s="a12b12c100";
NewClass13 ob=new NewClass13();
ob.decode(s);
}
}
Subscribe to:
Posts (Atom)