Check Whether The Number Is Prime or No:: Print First Non Repeating Character From The String
Check Whether The Number Is Prime or No:: Print First Non Repeating Character From The String
import java.util.*;
class Test1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int cnt=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
{
cnt++;
}
}
if(cnt==2)
{
System.out.println("Prime");
}
else
{
System.out.println("Not Prime");
}
}
}
Ip=3
Op=prime
}
}
(or)
import java.util.*;
class Test7
{
public static void main(String args[])
{
ArrayList<Integer> d = new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int cnt=0;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=a;j++)
{
if(i%j==0)
{
cnt++;
}
}
if(cnt==2)
{
d.add(i);
}
cnt=0;
}
for(int i=0;i<d.size();i++)
{
System.out.println(d.get(i));
}
}
}
Ip=10
Op=2 3 5 7
Input:
First line consists of a single integer denoting N.
N lines follow each containing a string made of lower case English alphabets.
Output:
Print N lines, each containing an integer, where the integer in ith line denotes Niceness value of string A[i].
Constraints:
1≤N≤1000
1≤|A[i]|≤10 ∀ i where 1≤i≤N, where |A[i]| denotes the length of ith string.
Sample Input
4
a
c
d
b
Sample Output
0
1
2
1
Explanation
Number of strings having index less than 1 which are less than "a" = 0
Number of strings having index less than 2 which are less than "c": ("a") = 1
Number of strings having index less than 3 which are less than "d": ("a", "c") = 2
Number of strings having index less than 4 which are less than "b": ("a") = 1
import java.util.*;
class monkAndNiceStrings
{
public static void main(String args[] ) throws Exception
{
int N = Integer.parseInt(in.nextLine());
String str[] = new String[N];
for(int j=0;j<i;j++)
{
if(str[j].compareTo(str[i]) < 0)
niceness++;
}
System.out.println(niceness);
}
}
}
}
}
Ip=10
20
Op=11 13 17 19
Sample Input
aacb 3
Sample Output
b
Explanation
import java.io.*;
import java.util.*;
Input:
The first line will consists of one integer T, which denotes the number of test cases.
For each test case:
One line consists of a integer N, denotes the number of students in the class.
Second line contains N space separated integers, where ith integer denotes the height of the ith student in the
class.
Output:
For each test case, if the required difference exists then print its value, otherwise print 1. Print the answer for
each test case in a new line.
Constraints:
1≤T≤10
1≤N≤105
1≤height of the student≤106
Sample Input
1
6
3 1 3 2 3 2
Sample Output
2
Explanation
Here Mishki can choose students with height=3 (h1) and height = 1 (h2) , as the difference between number of
students having height (h1) and number of students having height (h2) is maximum and greater than 0.
import java.util.*;
}
}
}
//1
System.out.println(set);
//2
System.out.println(listOne);
}
}
Op=[a,b,c,d,e,f,g]
Join 2 strings:
import java.util.*;
class Test0
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=sc.nextLine();
System.out.println(s.concat(s1));
}
}
Ip=charitha
Sri
Op=charitha Sri
Compare 2 strings:
import java.util.*;
class Test0
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=sc.nextLine();
System.out.println(s.equals(s1));
}
}
Ip=charitha
Charitha
Op=true
Find the number of times digit 3 occurs in each and every number from 0 to n.
import java.util.*;
public class Nooftimes
{
static int count_3s(int n)
{
int count = 0;
while (n > 0)
{
if (n % 10 == 3)
{
count++;
}
n = n / 10;
}
return count;
}
static int count(int n)
{
int count = 0 ;
for (int i = 2; i <= n; i++)
{
count += count_3s(i);
}
return count;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(count(n));
}
}
Ip=100
Op=20
Reverse a string:
import java.util.*;
class Test13
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
System.out.println(sb.reverse());
}
}
Ip=charitha
Op=ahtirahc
Create Matrix:
import java.util.Scanner;
class Create {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int r = in.nextInt();
int c = in.nextInt();
int m[][] = new int[r][c];
for(int i=0; i < r; i++)
{
for(int j=0; j < c; j++)
{
m[i][j] = in.nextInt();
}
}
for(int i=0; i < r; i++)
{
for(int j=0; j < c; j++)
{
System.out.print(m[i][j] +" ");
}
System.out.println();
}
}
}
Ip=3
3
123456789
Op=1 2 3
456
789
Removing some random characters from the string which are given by the user:
import java.util.*;
class Test13
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String s1=sc.nextLine();
char c[]=s.toCharArray();
char c1[]=s1.toCharArray();
List<char[]> d=Arrays.asList(c);
List<char[]> b=Arrays.asList(c1);
List<Character> listc = new ArrayList<Character>();
for (char cc : c)
{
listc.add(cc);
}
List<Character> listc1 = new ArrayList<Character>();
for (char cc1 : c1)
{
listc1.add(cc1);
}
System.out.println(listc);
System.out.println(listc1);
listc.removeAll(listc1);
System.out.println(listc);
for(int i=0;i<listc.size();i++)
{
System.out.print(listc.get(i));
}
}
}
Ip=charitha
arh
op=cit
/*index = str.contain(match) // returns 0 because first appearance of "Tim" start from 0 index
str = str.substring(index+1) // returns "imsdsfTimsfdTim"
index = str.contain(match) // returns 6 because first appearance of "Tim" in updated 'str' start from 6
str = str.substring(index+1) // returns "imsfdTim"
index = str.contain(match) // returns 5 because first appearance of "Tim" in updated 'str' start from 5
str = str.substring(index+1) // returns "im"
System.out.println(Collections.min(list));
int j=Collections.max(list);
list.remove(list.indexOf(j));
int max2 = Collections.max(list);
System.out.println(max2);
int i=Collections.min(list);
list.remove(list.indexOf(i));
int min2=Collections.min(list);
System.out.println(min2);*/
double sum=0;
for(int i=0;i<list.size();i++)
{
sum+=list.get(i);
}
System.out.println(sum);
if((sum%2)==0)
System.out.println("even");
else
System.out.println("odd");
Collections.reverse(list);
System.out.println(list);
}
}
Ip=1 2 3 4 5
done
op=[1,2,3,4,5]
5
1
4
2
15
Odd
[5,4,3,2,1]
Sort first half in ascending and second half in descending order in a list:
import java.util.*;
public class Test16
{
public static void main (String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()){
list.add(sc.nextInt());
}
List<Integer> first = new ArrayList<Integer>();
List<Integer> second = new ArrayList<Integer>();
int size = list.size();
for (int i = 0; i < size / 2; i++)
first.add(list.get(i));
for (int i = size / 2; i < size; i++)
second.add(list.get(i));
System.out.println(first);
System.out.println(second);
Collections.sort(first);
Collections.sort(second);
Collections.reverse(second);
first.addAll(second);
System.out.println(first);
}
}
Ip=3 5 6 2 9 1
done
Op=[3,5,6]
[2,9,1]
[3,5,6,9,2,1]
if(start < 0)
start = 0;
if(maskLength == 0)
System.out.println(strText);
Remove duplicate elements from the list and write the duplicates from the list:
Write down the unique values from the list:
import java.util.*;
public class Mask
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
ArrayList<Integer> d= new ArrayList<Integer>();
while(sc.hasNextInt()){
d.add(sc.nextInt());
}
ArrayList<Integer> d1=new ArrayList<Integer>();
ArrayList<Integer> d2=new ArrayList<Integer>();
for(int ele : d)
{
if(!d1.contains(ele))
{
d1.add(ele);
}
else
{
d2.add(ele);
}
}
System.out.println(d1);
System.out.println(d2);
d1.removeAll(d2);
System.out.println(d1);
System.out.println(d1.get(d1.size()/2)); //to get the middle element in the arraylist.
}
}
Ip= 1234523
done
op= [1,2,3,4,5]
[2,3]
[1,4,5]
3
Conversion of string into list and max element and min element and reverse
string list:
import java.util.*;
class Test19
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char c[]=s.toCharArray();
List<char[]> d=Arrays.asList(c);
List<Character> list = new ArrayList<Character>();
for (char ele : c)
{
list.add(ele);
}
System.out.println(list);
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
//Collections.reverse(list);
//System.out.println(list);
}
}
Ip= charitha
done
[c,h,a,r,i,t,h,a]
t
a
[a,h,t,i,r,a,h,c]
Print the second largest number even if there are duplicate values:
import java.util.*;
public class Test22
{
public static void main (String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()){
list.add(sc.nextInt());
}
Set<Integer> s=new HashSet<Integer>(list);
ArrayList<Integer> list1 = new ArrayList<Integer>(s);
int j=Collections.max(list1);
list1.remove(list1.indexOf(j));
System.out.println(Collections.max(list1));
}
}
Ip= 1234525
done
4
Counting valleys:
import java.util.Scanner;
public class Valley
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt(); in.nextLine();
String str = in.nextLine();
int alt=0, res=0;
char c[]=str.toCharArray();
for(int i=0;i<c.length;i++)
{
if(c[i]=='U')
{
alt++;
if(alt==0)
res++;
}
else
{
alt--;
}
}
System.out.println(res);
}
}
}
}
Ip= 153
Op= Armstrong
import java.util.*;
class Valley
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<Integer> b=new ArrayList<Integer>();
int a = in.nextInt();
int n = in.nextInt();
int c=in.nextInt();
c=c-1;;
for(int i=a;i<=n;i++)
{
int digits=0,d=0,sum=0,temp=i;
while(temp!=0)
{
temp=temp/10;
digits++;
}
temp=i;
while(temp!=0)
{
d=temp%10;
sum+=(Math.pow(d,digits));
temp=temp/10;
}
if(sum==i)
{
b.add(i);
}
}
System.out.println(b);
System.out.println(b.get(c));
}
}
Ip= 1
1000
Op= 1 2 3 4 5 6 7 8 9 153 370 371 407
Write a Java program to print the first 15 numbers of the Pell series.
In mathematics, the Pell numbers are an infinite sequence of integers. The sequence of Pell numbers starts with
0 and 1, and then each Pell number is the sum of twice the previous Pell number and the Pell number before
that.:
thus, 70 is the companion to 29, and 70 = 2 × 29 + 12 = 58 + 12.
The first few terms of the sequence are :
0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860,…
import java.util.*;
class Pell
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=0,b=1;
System.out.print(a +" "+ b);
for(int i=0;i<n;i++)
{
int c=(2*b)+a;
System.out.print(" " + c);
a=b;
b=c;
}
}
}
Input:
The first line will consists of one integer T denoting the number of test cases.
For each test case:
1) The first line consists of two integers N and K, N being the number of elements in the array
and K denotes the number of steps of rotation.
2) The next line consists of N space separated integers , denoting the elements of the array A.
Output:
Print the required array.
Constraints:
1≤T≤20
1≤N≤105
0≤K≤106
0≤A[i]≤106
Sample Input
1
52
12345
Sample Output
45123
Explanation
Here T is 1, which means one test case.
N=5 denoting the number of elements in the array and K=2, denoting the number of steps of rotations.
The initial array is: 1,2,3,4,5
In first rotation, 5 will come in the first position and all other elements will move to one position ahead
from their current position. Now, the resultant array will be 5,1,2,3,4
In second rotation, 4 will come in the first position and all other elements will move to one position
ahead from their current position. Now, the resultant array will be 4,5,1,2,3
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(sc.hasNext())
{
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<k;i++)
{
int last=a[n-1];
for(int j=n-1;j>0;j--)
{
a[j]=a[j-1];
}
a[0]=last;
}
for(int i=0;i<n;i++)
{
System.out.print(a[i] +" ");
}
}
}
}
Ip= 1
52
12345
Op= 4 5 1 2 3 // runs only for this test case
(or)
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
System.out.print(sb);
System.out.println("");
}
}
}
Monk and Inversions:
Monk's best friend Micro, who happen to be an awesome programmer, got him an integer matrix M of
size N×N for his birthday. Monk is taking coding classes from Micro. They have just completed array
inversions and Monk was successful in writing a program to count the number of inversions in an
array. Now, Micro has asked Monk to find out the number of inversion in the matrix M. Number of
inversions, in a matrix is defined as the number of unordered pairs of cells {(i,j),(p,q)} such that M[i]
[j]>M[p][q] & i≤p & j≤q.
Monk is facing a little trouble with this task and since you did not got him any birthday gift, you need to
help him with this task.
Input:
First line consists of a single integer T denoting the number of test cases.
First line of each test case consists of one integer denoting N. Following N lines consists of N space
separated integers denoting the matrix M.
Output:
Print the answer to each test case in a new line.
Constraints:
1≤T≤100
1≤N≤20
1≤M[i][j]≤1000
Sample Input
2
3
123
456
789
2
43
14
Sample Output
0
2
Explanation
In first test case there is no pair of cells (x1,y1), (x2,y2), x1≤x2 & y1≤y2 having M[x1][y1]>M[x2][y2], so
the answer is 0.
In second test case M[1][1]>M[1][2] and M[1][1]>M[2][1], so the answer is 2.
import java.util.Scanner;
public class MonkAndInversions {
int n = in.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
a[i][j] = in.nextInt();
}
}
int inversions = 0;
System.out.println(inversions);
t--;
}
}
}
int n= j-i+1;
int count=0;
while(i<=j){
int number= i;
int reverse= 0;
while(number!= 0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
int diff= Math.abs(i-reverse);
if(diff % k==0||diff==0){
count++;
}
i++;
}
System.out.println(count);
}
}
Electronics shop:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
Angry Professor:
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int j=0;j<t;j++)
{
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int cnt=0;
for(int i=0;i<n;i++)
{
if(a[i]<=0)
{
cnt++;
}
}
if(cnt>=k)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
}
}
}
}
Unique tree:
import java.util.*;
public class test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int b=0;
ArrayList<Integer> d=new ArrayList<Integer>();
for(int i=0;i<60;i++)
{
if(i%2==0)
{
b=b+1;
d.add(b);
}
else
{
b=b*2;
d.add(b);
}
}
for(int i=0;i<n;i++)
{
System.out.println(d.get(a[i]));
}
}
}
Complete the function that accepts a string parameter, and reverses each word
in the string. All spaces in the string should be retained.
import java.util.*;
public class Reversestring {
}
public static String reverseWords(final String original)
{
String[] arr=original.split(" ");
if(arr.length==0)
return original;
String result="";
for(String word:arr){
StringBuilder input = new StringBuilder();
result+=input.append(word).reverse().toString()+" ";
}
return result.trim();
}
}
Ip= Let’s reverse this string
Op= s’teL esrever siht gnirts