first largest number
import java.util.*;
class Abc {
public static void main(String[] args) {
int a[]= {10,20,44,1,5,8,99};
int max=0;
for(int i=0;i<a.length;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
System.out.println(max);
}
}
second largest number
import java.util.*;
class Abc {
public static void main(String[] args) {
int a[]= {10,20,44,1,5,8,99};
int max=0,smax=0;
for(int i=0;i<a.length;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]!=max)
{
smax=Math.max(smax,a[i]);
}
}
System.out.println(smax);
}
}
remove duplicate in string
import java.util.*;
class Abc {
public static void main(String[] args) {
String a="sabarii";
String b="";
int i;
for(i=0;i<a.length();i++)
{
char c=a.charAt(i);
if(b.indexOf(c)==-1)
{
b=b+c;
}
}
System.out.println(b);
}
}
biggest word in string
import java.util.*;
class Abc {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String s1[]=s.split(" ");
int max=0;
int j=0;
for (int i = 0; i < s1.length; i++)
{
if(s1[i].length()>max)
{
max=s1[i].length();
j=i;
}
}
System.out.println(s1[j]);
}
}
smallest word in string
import java.util.*;
class Abc {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String s1[]=s.split(" ");
int min=s1[0].length();
int j=0;
for (int i = 0; i < s1.length; i++)
{
if(s1[i].length()<min)
{
min=s1[i].length();
j=i;
}
}
System.out.println(s1[j]);
}
}
dead lock example
import java.util.*;
class Abc {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2= new Object();
Thread thread1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1 acquired lock1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("abc");
}
}
});
// Thread 2
Thread thread2 = new Thread(() -> {
synchronized (lock2) {
System.out.println("Thread 2 acquired lock2");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("hello");
}
}
});
// Start the threads
thread1.start();
thread2.start();
}
}
multidimentional array swapping
import java.util.*;
class Abc {
public static void swap(int [][]t,int a, int b,int c,int d) {
int temp = t[a][b];
t[a][b] = t[c][d];
t[c][d] = temp;
}
public static void main(String[] args) {
int[][] arr = {{1}, {2}, {3}};
swap(arr,0,0,1,0);
System.out.println(arr[0][0]);
}
}
No comments:
Post a Comment