Java Document 1
Java Document 1
Java Document 1
System.out.println("Hello\nAlexandra Abramov!");
System.out.println(24+26);
System.out.println(50/3);
System.out.println(-5 + 8 * 6);
System.out.println((55+9) % 9);
System.out.println(5 + 15 / 3 * 2 - 8 % 3);
import java.util.Scanner;
(num1 + num2));
System.out.println(num1 + " - " + num2 + " = " +
(num1 - num2));
(num1 * num2));
(num1 / num2));
(num1 % num2));
(num1 * (i+1)));
}
}
System.out.println(" J a v v a ");
System.out.println(" J a a v v a a");
System.out.println(" JJ a a V a a");
System.out.println(result); //
}
import java.util.Scanner;
System.out.println("* * * * * *
==================================");
System.out.println(" * * * * *
==================================");
System.out.println("* * * * * *
==================================");
System.out.println(" * * * * *
==================================");
System.out.println("* * * * * *
==================================");
System.out.println(" * * * * *
==================================");
System.out.println("* * * * * *
==================================");
System.out.println(" * * * * *
==================================");
System.out.println("* * * * * *
==================================");
System.out.println("=====================================
=========");
System.out.println("=====================================
=========");
System.out.println("=====================================
=========");
System.out.println("=====================================
=========");
System.out.println("=====================================
=========");
System.out.println("=====================================
=========");
a = 15;
b = 27;
temp = a;
a = b;
b = temp;
System.out.println(" | ^ | ");
import java.util.Scanner;
int i = 0, remainder = 0;
binary1 = in.nextLong();
binary2 = in.nextLong();
if (remainder != 0) {
sum[i++] = remainder;
--i;
System.out.print(sum[i--]);
System.out.print("\n");
binary1 = in.nextLong();
binary2 = in.nextLong();
while (binary2 != 0)
else
factor = 10;
int i = 0, remainder = 0;
int binary_prod_result = 0;
if (remainder != 0)
sum[i++] = remainder;
--i;
while (i >= 0)
return binary_prod_result;
dec_num = scan.nextInt();
quot = dec_num;
while(quot != 0)
bin_num[i++] = quot%2;
quot = quot/2;
System.out.print(bin_num[j]);
System.out.print("\n");
import java.util.Scanner;
String hexdec_num="";
dec_num = in.nextInt();
while(dec_num>0)
rem = dec_num%16;
dec_num = dec_num/16;
dec_num = scan.nextInt();
quot = dec_num;
while(quot != 0)
oct_num[i++] = quot%8;
quot = quot/8;
System.out.print(oct_num[j]);
System.out.print("\n");
binaryNumber = sc.nextLong();
while (binaryNumber != 0)
import java.util.Scanner;
bin = in.nextInt();
rem = bin % 2;
i = i * 2;
i = 0;
while (dec != 0) {
i++;
if (hex[j] > 9)
{
System.out.print((char)(hex[j] + 55)+"\n");
} else
System.out.print(hex[j]+"\n");
binnum = scan.nextInt();
binnum1=binnum;
while(binnum > 0)
//System.out.println(rem);
i = i*2;
binnum = binnum/10;
i=1;
quot = decnum;
while(quot > 0)
{
octnum[i++] = quot % 8;
quot = quot / 8;
System.out.print(octnum[j]);
System.out.print("\n");
int i = 0;
octal_num = in.nextLong();
while (octal_num != 0)
{
decimal_num = (long)(decimal_num + (octal_num % 10) *
Math.pow(8, i++));
import java.util.Scanner;
int rem;
octal_num = in.nextLong();
tempoctal_num = octal_num;
binary_num = 0;
place = 1;
while (tempoctal_num != 0)
tempoctal_num /= 10;
place *= 1000;
import java.util.Scanner;
int decnum;
hex_num = Integer.toHexString(decnum);
import java.util.Scanner;
public class Exercise28 {
s = s.toUpperCase();
int val = 0;
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
return val;
String hexdec_num;
int dec_num;
hexdec_num = scan.nextLine();
dec_num = hex_to_decimal(hexdec_num);
System.out.print("Equivalent decimal number is: " +
dec_num+"\n");
import java.util.Scanner;
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
return val;
String hexdec_num;
hexdec_num = scan.nextLine();
dec_num = hex_to_decimal(hexdec_num);
bin_num[i++] = dec_num%2;
dec_num = dec_num/2;
System.out.print(bin_num[j]);
System.out.print("\n");
s = s.toUpperCase();
int val = 0;
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
return val;
String hexdec_num;
hexdec_num = in.nextLine();
dec_num = hex_to_decimal(hexdec_num);
while(dec_num != 0)
octal_num[i++] = dec_num%8;
dec_num = dec_num/8;
}
System.out.print(octal_num[j]);
System.out.print("\n");
System.out.println("\nJava Version:
"+System.getProperty("java.version"));
System.out.println("Java Home:
"+System.getProperty("java.home"));
System.out.println("Java Vendor:
"+System.getProperty("java.vendor"));
if ( number1 == number2 )
long n = input.nextLong();
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
return sum;
import java.util.Scanner;
double s = input.nextDouble();
return (6*(s*s))/(4*Math.tan(Math.PI/6));
int ns = input.nextInt();
lat1 = Math.toRadians(lat1);
lon1 = Math.toRadians(lon1);
lat2 = Math.toRadians(lat2);
lon2 = Math.toRadians(lon2);
431
432
Total number of the three-digit-number is 24
import java.util.Scanner;
int amount = 0;
amount++;
}
}
...
x-SJIS_0213
x-UTF-16LE-BOM
X-UTF-32BE-BOM
X-UTF-32LE-BOM
x-windows-50220
x-windows-50221
x-windows-874
x-windows-949
x-windows-950
x-windows-iso2022jp
import java.nio.charset.Charset;
import java.io.Console;
Console cons;
try {
pass_ward = cons.readPassword("Input your
Password:");
} finally {
if (pass_ward != null) {
} else {
}
44. Write a Java program that accepts an integer (n) and
computes the value of n+nn+nnn.
Sample Output:
Input number: 5
5 + 55 + 555
import java.util.Scanner;
int n;
n = in .nextInt();
import java.io.File;
47. Write a Java program to display the current date and time in
a specific format.
Sample Output:
Now: 2017/06/16 08:52:03.066
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
cdt.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("G
MT")));
System.out.println("\nNow:
"+cdt.format(System.currentTimeMillis()));
}
91
93
95
97
99
import java.util.*;
if (i % 2 != 0) {
System.out.println(i);
}
49. Write a Java program to accept a number and check whether
the number is even or not. Prints 1 if the number is even or 0 if
odd.
Sample Output:
Input a number: 20
1
import java.util.*;
int n = in.nextInt();
if (n % 2 == 0) {
System.out.println(1);
else {
System.out.println(0);
Divided by 5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90,
95,
Divided by 3 & 5:
15, 30, 45, 60, 75, 90,
public class Exercise50 {
System.out.println("\nDivided by 3: ");
if (i%3==0)
System.out.println("\n\nDivided by 5: ");
System.out.println("\n");
System.out.printf("\n");
int x = in.nextInt();
int z = in.nextInt();
System.out.print("\n");
53. Write a Java program that accepts three integers from the
user. It returns true if the second number is higher than the first
number and the third number is larger than the second number. If
"abc" is true, the second number does not need to be larger than
the first number.
Sample Output:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
import java.util.*;
int y = in.nextInt();
int z = in.nextInt();
System.out.print("\n");
if(xyz)
54. Write a Java program that accepts three integers from the
user and returns true if two or more of them (integers) have the
same rightmost digit. The integers are non-negative.
Sample Output:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true
import java.util.*;
int x = in.nextInt();
int y = in.nextInt();
int z = in.nextInt();
System.out.print("\n");
int M = H % 60;
H = H / 60;
System.out.print("\n");
}
56. Write a Java program to find the number of values in a given
range divisible by a given value.
For example x = 5, y=20 and p =3, find the number of integers
within the range x..y and that are divisible by p i.e. { i :x ≤ i ≤ y, i
mod p = 0 }
Sample Output:
5
import java.util.*;
int x = 5;
int y = 20;
int p = 3;
System.out.println(result(x,y,p));
if (x%p == 0)
return(y/p - x/p);
int x = in.nextInt();
System.out.println(result(x));
int ctr = 0;
ctr+=2;
} else if (i*i==num) {
ctr++;
return ctr;
while(lineScan.hasNext()) {
upper_case_line +=
Character.toUpperCase(word.charAt(0)) + word.substring(1) + " ";
System.out.println(upper_case_line.trim());
line = line.toLowerCase();
System.out.println(line);
System.out.println("Penultimate word:
"+words[words.length - 2]);
import java.util.*;
public class Exercise61 {
word = word.trim();
char[] ch=word.toCharArray();
result += ch[i];
62. Write a Java program that accepts three integer values and
returns true if one is 20 or more less than the others'
subtractions.
Sample Output:
Input the first number : 15
Input the second number: 20
Input the third number : 25
false
import java.util.*;
int x = in.nextInt();
int y = in.nextInt();
int z = in.nextInt();
63. Write a Java program that accepts two integer values from
the user and returns the largest value. However if the two values
are the same, return 0 and find the smallest value if the two
values have the same remainder when divided by 6.
Sample Output:
Input the first number : 12
Input the second number: 13
Result: 13
import java.util.*;
int a = in.nextInt();
int b = in.nextInt();
System.out.println("Result: "+result(a, b));
if(x == y)
return 0;
if(x % 6 == y % 6)
return (x < y) ? x : y;
return (x > y) ? x : y;
int a = in.nextInt();
int b = in.nextInt();
System.out.println("Result: "+common_digit(a, b));
if (p<25 || q>75)
return false;
int x = p % 10;
int y = q % 10;
p /= 10;
q /= 10;
return (p == q || p == y || x == q || x == y);
int b = in.nextInt();
int divided = a / b;
System.out.println(result);
66. Write a Java program to compute the sum of the first 100
prime numbers.
Sample Output:
Sum of the first 100 prime numbers: 24133
import java.util.*;
int sum = 1;
int ctr = 0;
int n = 0;
n++;
if (n % 2 != 0) {
if (is_Prime(n)) {
sum += n;
ctr++;
if (n % i == 0) {
return false;
return true;
import java.lang.*;
System.out.println(main_string.substring(0, 7) + word +
main_string.substring(6));
String last_three_chars =
main_string.substring(main_string.length() - 3);
System.out.println(last_three_chars + last_three_chars +
last_three_chars + last_three_chars);
import java.lang.*;
System.out.println(main_string.substring(0,
main_string.length()/2));
import java.lang.*;
System.out.println( str2+str1+str2);
else
System.out.println(str1+str2+str1);
import java.lang.*;
System.out.println(str1.substring(1) + str2.substring(1));
}
72. Write a Java program to create a string taking the first three
characters from a given string. If the string length is less than 3
use "#" as substitute characters.
Test Data: Str1 = " "
Sample Output:
###
import java.lang.*;
if(len >= 3)
else if(len == 1)
System.out.println( (str1.charAt(0)+"##"));
else
System.out.println("###");
73. Write a Java program to create a string taking the first and
last characters from two given strings. If the length of each
string is 0 use "#" for missing characters.
Test Data: str1 = "Python"
str2 = " "
Sample Output:
P#
import java.lang.*;
System.out.println(result);
}
}
System.out.println((num_array[0] == 10 ||
num_array[num_array.length-1] == 10));
75. Write a Java program to test if the first and last elements of
an array of integers are the same. The array length must be
broader than or equal to 2.
Test Data: array = 50, -20, 0, 30, 40, 60, 10
Sample Output:
false
import java.lang.*;
76. Write a Java program to test if the first and last element of
two integer arrays are the same. The array length must be
greater than or equal to 2.
Test Data: array1 = 50, -20, 0, 30, 40, 60, 12
array2 = 45, 20, 10, 20, 30, 50, 11
Sample Output:
false
public class Main {
//false
//true
System.out.println(num_array1[0] == num_array2[0] ||
num_array1[num_array1.length-1] ==
num_array2[num_array2.length-1]);
else
System.out.println("Array1: "+Arrays.toString(array1));
System.out.println("Array2: "+Arrays.toString(array2));
int[] array_new = {array1[0], array2[2]};
System.out.println("New Array:
"+Arrays.toString(array_new));
System.out.println("Original Array:
"+Arrays.toString(array_nums));
if(array_nums[0] == 4 || array_nums[0] == 7)
System.out.println("True");
else
System.out.println("Original Array:
"+Arrays.toString(array_nums));
System.out.println("Rotated Array:
"+Arrays.toString(new_array_nums));
80. Write a Java program to get the largest value between the
first and last elements of an array (length 3) of integers.
Sample Output:
Original Array: [20, 30, 40]
Larger value between first and last element: 40
import java.util.Arrays;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
max_val = array_nums[2];
81. Write a Java program to swap the first and last elements of
an array (length must be at least 1) and create another array.
Sample Output:
Original Array: [20, 30, 40]
New array after swaping the first and last elements: [40, 30, 20]
import java.util.Arrays;
int x = array_nums[0];
array_nums[0] = array_nums[array_nums.length-1];
array_nums[array_nums.length-1] = x;
max_val = array_nums[array_nums.length-1];
max_val = array_nums[array_nums.length/2];
Result: 1 12 25 -8
import java.util.*;
System.out.println("\nArray1: "+Arrays.toString(left_array));
System.out.println("\nArray2: "+Arrays.toString(right_array));
System.out.println("\nResult: "+result);
84. Write a Java program to take the last three characters from
a given string. It will add the three characters at both the front
and back of the string. String length must be greater than three
and more.
Test data: "Python" will be "honPythonhon"
Sample Output:
honPythonhon
import java.util.*;
import java.io.*;
int slength = 3;
if (slength > string1.length()) {
slength = string1.length();
import java.util.*;
import java.io.*;
{
String string1 = "Hello how are you?";
System.out.println(string1.startsWith("Hello"));
import java.util.Scanner;
int n = in.nextInt();
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
else {
n = (3 * n + 1) / 2;
System.out.println("\nValue of n = "+n);
in.close();
}
}
import java.io.*;
try {
int sum = 0;
print_number(sum);
} catch (IOException e) {
e.printStackTrace();
}
public static void print_number(int n) {
String[] number =
{"zero","one","two","three","four","five","six","seven","eight","nin
e"};
if (n < 10) {
System.out.println(number[n]);
x = n / 10;
y = n - x *10;
else {
x = n / 100;
y = (n - x * 100) / 10;
z = n - x * 100 - y * 10;
}
88. Write a Java program to get the current system environment
and system properties.
import java.lang.*;
System.out.println(System.getenv());
System.out.println(System.getProperties());
}
89. Write a Java program to check whether a security manager
has already been established for the current application or not.
import java.lang.*;
System.out.println(System.getSecurityManager());
import java.lang.*;
System.out.println(System.getenv("PATH"));
System.out.println(System.getenv("TEMP"));
// gets the value of the specified environment variable
"USERNAME"
System.out.println(System.getenv("USERNAME"));
import java.lang.*;
// Sample program
int i;
System.out.println ("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
System.out.println (i);
92. Write a Java program to count the number of even and odd
elements in a given array of integers.
import java.util.*;
if(nums[i] % 2 == 0)
ctr_even++;
else
ctr_odd++;
System.out.printf("\n");
import java.util.*;
found1010 = true;
found2020 = true;
System.out.printf("\n");
import java.io.*;
int i = 0;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
if(array_nums[j] % 2 != 0) {
array_nums[i] = array_nums[j];
array_nums[j] = temp;
i++;
System.out.println("New Array:
"+Arrays.toString(array_nums));
import java.io.*;
int n= 5;
arr_string[i] = String.valueOf(i);
System.out.println("New Array:
"+Arrays.toString(arr_string));
}
96. Write a Java program to check if there is a 10 in an array of
integers with a 20 somewhere later on.
import java.util.*;
import java.io.*;
int result=0;
int x = 10;
int y = 20;
testd = true;
System.out.printf( String.valueOf(true));
result = 1 ;
if (result==0)
System.out.printf( String.valueOf(false));
System.out.printf("\n");
import java.io.*;
int result=0;
int x = 10;
System.out.printf( String.valueOf(true));
result = 1 ;
System.out.printf( String.valueOf(true));
result = 1 ;
if (result==0)
System.out.printf( String.valueOf(false));
System.out.printf("\n");
import java.io.*;
count++;
System.out.printf( String.valueOf(false));
result = 1;
if(array_nums[i] == 20)
count++;
if (result==0)
System.out.printf("\n");
import java.io.*;
int result = 0;
int x = 20;
result = 1;
if (result==0)
System.out.printf( String.valueOf(true));
else
System.out.printf( String.valueOf(false));
import java.io.*;
System.out.println("Array1: "+Arrays.toString(array_nums1));
System.out.println("Array2: "+Arrays.toString(array_nums2));
int ctr = 0;
ctr++;
System.out.printf("\n");
import java.io.*;
int[] array_nums = {10, 11, 10, 30, 45, 20, 33, 53};
int result = 0;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
int ctr1 = 0;
int ctr2 = 0;
if(array_nums[i] == 10)
ctr1++;
if(array_nums[i] == 20)
ctr2++;
System.out.printf("\n");
}
102. Write a Java program to check if a specified array of
integers contains 10 or 30.
import java.util.*;
import java.io.*;
int[] array_nums = {11, 11, 13, 31, 45, 20, 33, 53};
int result = 1;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
{
if(array_nums[i] == 10 || array_nums[i] == 30)
result =0;
if (result==1)
System.out.printf( String.valueOf(false));
else
System.out.printf(String.valueOf(true));
import java.io.*;
int[] array_nums = {11, 10, 13, 10, 45, 20, 33, 53};
int result = 0;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
int l = array_nums.length - 1;
int[] new_array;
while(array_nums[l] != 10)
l--;
new_array[i - l - 1] = array_nums[i];
System.out.println("New Array:
"+Arrays.toString(new_array));
import java.util.*;
import java.io.*;
public class Exercise104 {
int[] array_nums = {11, 15, 13, 10, 45, 20, 33, 53};
int result = 0;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
int l = 0;
int[] new_array;
while(array_nums[l] != 10)
l++;
new_array[i] = array_nums[i];
System.out.println("New Array:
"+Arrays.toString(new_array));
import java.io.*;
int[] array_nums = {11, 15, 13, 10, 45, 20, 11, 15};
System.out.println("Original Array:
"+Arrays.toString(array_nums));
int result = 0;
int l = 2;
int start = 0;
if(array_nums[start] != array_nums[end])
result = 1;
else
start++;
end++;
if (result==1)
System.out.printf(String.valueOf(false));
else
System.out.printf(String.valueOf(true));
System.out.printf("\n");
import java.io.*;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
if(array_nums.length >1)
array_nums[i - 1] = array_nums[i];
array_nums[array_nums.length - 1] = first;
System.out.println("New Array:
"+Arrays.toString(array_nums));
import java.util.*;
import java.io.*;
System.out.println("Original Array:
"+Arrays.toString(array_nums));
result =0;
if (result==1)
System.out.printf(String.valueOf(false));
else
System.out.printf(String.valueOf(true));
System.out.printf("\n");
108. Write a Java program to add all the digits of a given positive
integer until the result has a single digit.
import java.util.Scanner;
int n = in.nextInt();
if (n>0)
System.out.println("\n");
import java.util.Scanner;
int n = in.nextInt();
if (n>0)
int test = 0;
int n = in.nextInt();
if (n < 1) {
System.out.print(Boolean.toString(false));
test = 1;
test = 1;
if (test==0)
System.out.print(Boolean.toString((n &
0x55555555) != 0));
System.out.print("\n");
import java.util.Scanner;
int x, y ;
x = in.nextInt();
y = in.nextInt();
while(y != 0){
int carry = x & y;
x = x ^ y;
y = carry << 1;
System.out.print("Sum: "+x);
System.out.print("\n");
import java.util.Scanner;
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int n1 = n;
long ctr = 0;
while (n != 0)
ctr += n / 5;
n /= 5;
System.out.printf("\n");
//initialization.
array1[0]=1;
array1[1]=2;
array1[2]=3;
array1[3]=4;
System.out.println("\nArray1: "+Arrays.toString(array1));
System.out.println("\nArray2: "+Arrays.toString(array2));
array1[index--] = array1[i--];
} else {
array1[index--] = array2[j--];
while (i >= 0) {
array1[index--] = array1[i--];
while (j >= 0) {
array1[index--] = array2[j--];
System.out.println("\nMerged array:
"+Arrays.toString(array1));
}
}
import java.util.*;
import java.util.*;
char[] A = str.toCharArray();
int offset=3;
offset %= len;
System.out.println("\n"+Arrays.toString(A));
str[start] = str[end];
str[end] = temp;
start++;
end--;
int num;
int n = in.nextInt();
System.out.printf("Is %d is a palindrome number?\n",n);
System.out.println(is_Palindrome(n));
int reverse = 0;
while (n != 0) {
reverse *= 10;
reverse += n % 10;
n /= 10;
return reverse;
return (n == reverse_nums(n));
import java.util.*;
} else if (i % 5 == 0) {
System.out.printf("\n%d: buzz",i);
} else if (i % 3 == 0) {
System.out.printf("\n%d: fizz",i);
System.out.printf("\n");
import java.util.*;
int num;
int n = in.nextInt();
System.out.println(sqrt(n));
if (num == 0 || num == 1) {
return num;
int a = 0;
int b = num;
while (a <= b) {
b = mid - 1;
} else {
return mid;
}
a = mid + 1;
return a;
import java.util.Scanner;
String s = "Python";
//String t = "Py";
String t = "yt";
// String t = "ab";
System.out.printf(String.valueOf(strStr(s, t)));
return -1;
if ("".equals(target) || source.equals(target)) {
return 0;
int i = 0;
if (source.charAt(i) == target.charAt(0)) {
if (source.charAt(i + j) != target.charAt(j)) {
equal = false;
if (equal) {
return i;
}
++i;
return -1;
import java.util.Scanner;
public class Main {
int target = 7;
int lower = 0;
if (nums[mid] == target) {
index = mid;
upper = mid - 1;
} else {
lower = mid + 1;
int target = 0;
System.out.print(matrix[row][col]+" ");
if (col == 2)
System.out.println();
System.out.print(Boolean.toString(searchMatrix(matrix,
target)));
if (matrix.length == 0 || matrix[0].length == 0) {
return false;
int m = matrix.length;
int n = matrix[0].length;
int lower = 0;
int higher = m * n - 1;
if (val == target) {
return true;
lower = mid + 1;
} else {
higher = mid - 1;
return false;
int data;
Node next_node;
Node(int d) {
data = d;
next_node = null;
next_node = current_node.next_node;
current_node.next_node = prev_node;
prev_node = current_node;
current_node = next_node;
node = prev_node;
return node;
node = node.next_node;
list.head.next_node.next_node.next_node = new
Node(80);
list.printList(head);
head = list.reverse(head);
System.out.println("");
list.printList(head);
System.out.print(max_SubArray(nums));
}
if (nums.length < 1) {
return 0;
int max_Begin = 0;
int max_End = 0;
int begin = 0;
int end = 0;
int sum = 0;
sum += nums[end];
if (sum < 0) {
sum = 0;
begin = end + 1;
} else {
max = sum;
max_Begin = begin;
max_End = end;
end++;
}
return max;
import java.util.*;
nums.add(1);
nums.add(-3);
nums.add(4);
System.out.print(min_SubArray(nums));
nums1[0] = nums.get(0);
return min;
int target;
target = 5;
// target = 0;
// target = 7;
System.out.print(searchInsert(nums, target));
}
public static int searchInsert(int[] nums1, int target) {
return 0;
int start = 0;
if (nums1[mid] == target) {
return mid;
end = mid;
} else {
start = mid;
return start;
return end;
} else {
return end + 1;
int key;
key = item;
}
class BinaryTree
Node root;
BinaryTree()
root = null;
if (node == null)
return;
print_Preorder(node.left);
print_Preorder(node.right);
void print_Preorder()
{
print_Preorder(root);
// Driver method
tree.print_Preorder();
int key;
key = item;
class BinaryTree
Node root;
BinaryTree()
root = null;
if (node == null)
return;
print_Inorder(node.left);
print_Inorder(node.right);
void print_Inorder()
print_Inorder(root);
tree.print_Inorder();
}
int key;
key = item;
class BinaryTree
{
Node root;
BinaryTree()
root = null;
if (node == null)
return;
print_Postorder(node.left);
print_Postorder(node.right);
void print_Postorder()
print_Postorder(root);
tree.print_Postorder();
if(array.length % 2 == 0) {
int nums[] = {10, 20, 10, 20, 30, 40, 40, 30, 50};
int result;
System.out.println("Source Array :
"+Arrays.toString(nums));
result = getSingleNumber(nums);
return -1;
int result = 0;
result ^= nums[i];
return result;
class Node
int data;
data = item;
}
}
Node root;
if(root==null)
return 0;
return bigger+1;
class Solution {
if (nums == null) {
return 0;
}
if (nums.length <= 1) {
return nums.length;
int current_pos = 0;
int moving_pos;
if (nums[current_pos] != nums[moving_pos]) {
nums[current_pos + 1] = nums[moving_pos];
current_pos++;
return current_pos + 1;
}
}
import java.util.Arrays;
class Solution {
return 0;
int index = 1;
index++;
nums[index] = nums[i];
return index + 1;
}
}
133. Write a search Java program to find a path from top left to
bottom in the right direction which minimizes the sum of all
numbers along its path.
Note: Move either down or right at any point in time.
Sample Output: Sum of all numbers along its path: 13
int m = grid.length;
int n = grid[0].length;
if (i == 0 && j == 0) {
temp[i][j] = grid[i][j];
continue;
// Compute temp
{0,5,6},
{3,1,2}};
if (n <= 1) {
return 1;
s_case[0] = 1;
s_case[1] = 1;
return s_case[n];
int steps = 5;
class Node
int data;
Node next;
void remove_Duplicates()
{
Node current = head;
Node next_next;
if (head == null)
return;
if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null;
current.next = next_next;
else
current = current.next;
new_node.next = head;
head = new_node;
System.out.print(temp.data);
if (temp.next != null)
System.out.print("->");
temp = temp.next;
System.out.println();
l_list.push(17);
l_list.push(17);
l_list.push(16);
l_list.push(15);
l_list.push(15);
l_list.push(14);
l_list.push(13);
l_list.push(12);
l_list.push(12);
l_list.printList();
l_list.remove_Duplicates();
l_list.printList();
/**
* @return an integer
*/
public static int unique_Paths(int m, int n) {
if (m <= 0 || n <= 0) {
return 0;
return grid[0][0];
return 1;
int m = 3;
int n = 2;
int m = obstacle_Grid.length;
if (m <= 0) {
return 0;
int n = obstacle_Grid[0].length;
if (n <= 0) {
return 0;
return dp[0][0];
int[][] obstacle_Grid ={
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
};
int m = obstacle_Grid.length;
if (m <= 0) {
return 0;
int n = obstacle_Grid[0].length;
if (n <= 0) {
return 0;
dp[m][n - 1] = 1;
return dp[0][0];
int[][] obstacle_Grid ={
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
};
System.out.println("Unique paths from top-left corner
to bottom-right corner of the said grid (considering some
obstacles): "+uniquePaths_With_obstacle_Grid(obstacle_Grid));
int longest_length = 0;
longest_length = length;
list.clear();
if (length == longest_length) {
list.add(str);
}
}
return list;
System.out.println("Original dictionary :
"+Arrays.toString(dict));
139. Write a search Java program to get the index of the first and
the last number of a subarray where the sum of numbers is zero.
This is from a given array of integers.
Original Array : [1, 2, 3, -6, 5, 4]
Index of the subarray of the said array where the sum of numbers
is zero: [0, 3]
import java.util.*;
return temp;
int pre_Sum = 0;
pre_Sum += nums[i];
if (map.containsKey(pre_Sum)) {
temp.add(map.get(pre_Sum) + 1);
temp.add(i);
return temp;
map.put(pre_Sum, i);
return temp;
System.out.println("Original Array :
"+Arrays.toString(nums));
import java.util.*;
x = merge(x);
for(Interval i : x)
}
public static ArrayList<Interval> merge(ArrayList<Interval>
intervals) {
if(intervals.size() == 0 || intervals.size() == 1)
return intervals;
} else {
start = current.getStart();
end = current.getEnd();
}
result.add(new Interval(start, end));
return result;
class Interval
Interval() {
start = 0;
end = 0;
Interval(int s, int e)
start = s;
end = e;
return start;
}
public int getEnd() {
return end;
/**
* @return: a boolean
*/
Arrays.sort(chars);
if (chars[i] == chars[i-1]) {
return false;
return true;
/**
*/
return false;
} else if (str1.length() != str2.length()) {
return false;
return true;
count[str1.charAt(i)]++;
count[str2.charAt(i)]--;
if (num != 0) {
return false;
return true;
System.out.println("String-1 : "+str1);
System.out.println("String-2 : "+str2);
}
}
143. Write a search Java program to merge the two sorted lists.
Sample Output:
Merge Two Sorted ListsT:
1 2 3 7 9 13 40
head = head.next;
mlist = mlist.next;
list1 = list1.next;
} else {
mlist = mlist.next;
list2 = list2.next;
mlist = mlist.next;
list1 = list1.next;
mlist = mlist.next;
list2 = list2.next;
head = head.next;
return head;
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
/**
*/
public static int removeElement(int[] nums, int elem) {
if(length==0) return 0;
int i=0;
if(nums[j]!=elem)
nums[i]=nums[j];
i++;
if(i<length) nums[i]='\0';
return i;
int x = 6;
System.out.println("Original array:
"+Arrays.toString(nums));
}
145. Write a Java program to remove the nth element from the
end of a given list.
Sample Output:
Original node:
12345
After removing 2nd element from end:
1235
import java.util.*;
ListNode o = h;
System.out.println("Original node:");
while (o != null) {
o = o.next;
head = head.next;
ListNode p = head;
int size = 0;
while (p != null) {
size++;
p = p.next;
}
if (n == size) {
head = head.next;
} else {
ListNode t = head;
t = t.next;
index--;
t.next = t.next.next;
return head;
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
146. Write a search Java program to convert an array of sorted
items into a binary search tree. Maintain the minimal height of
the tree.
Sample Output:
2
1
4
6
5
3
traverseTree(root);
if (start == end - 1) {
} else {
node.val = arr[mid];
return node;
if (root != null) {
traverseTree(root.left);
traverseTree(root.right);
System.out.println(root.val);
class TreeNode {
this.val = val;
System.out.println(bitSwapRequired(27, 23));
int ctr = 0;
ctr += z & 1;
return ctr;
148. Write a Java program to find the index of the first unique
character in a given string. Assume that there is at least one
unique character in the string.
Sample Output:
Original String: wresource
First unique character of the above: 0
import java.util.*;
String s = "wresource";
}
public static int first_Uniq_Char(String s) {
freq[c - 'a']++;
return -1;
System.out.println(stringPermutation(str1, str2));
arr[(int) str1.charAt(i)] += 1;
arr[(int) str2.charAt(i)] -= 1;
return true;
/**
*/
return false;
return t1 == t2;
return false;
} else {
t1.left = t2;
t1.right = t3;
n1.left = n2;
n1.right = n3;
System.out.println(is_Subtree(t1, n1));
class TreeNode {
this.val = val;