Skip to content

Commit edc51cb

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Java into node-find-fix
2 parents e538158 + b21444d commit edc51cb

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* @author Afrizal Fikri (https://github.com/icalF)
6+
*
7+
*/
8+
public class LongestIncreasingSubsequence {
9+
public static void main(String[] args) throws Exception {
10+
11+
Scanner sc = new Scanner(System.in);
12+
int n = sc.nextInt();
13+
14+
int ar[] = new int[n];
15+
for (int i = 0; i < n; i++) {
16+
ar[i] = sc.nextInt();
17+
}
18+
19+
System.out.println(LIS(ar));
20+
}
21+
22+
private static int upperBound(int[] ar, int l, int r, int key) {
23+
while (l < r-1) {
24+
int m = (l + r) / 2;
25+
if (ar[m] >= key)
26+
r = m;
27+
else
28+
l = m;
29+
}
30+
31+
return r;
32+
}
33+
34+
public static int LIS(int[] array) {
35+
int N = array.length;
36+
if (N == 0)
37+
return 0;
38+
39+
int[] tail = new int[N];
40+
int length = 1; // always points empty slot in tail
41+
42+
tail[0] = array[0];
43+
for (int i = 1; i < N; i++) {
44+
45+
// new smallest value
46+
if (array[i] < tail[0])
47+
tail[0] = array[i];
48+
49+
// array[i] extends largest subsequence
50+
else if (array[i] > tail[length-1])
51+
tail[length++] = array[i];
52+
53+
// array[i] will become end candidate of an existing subsequence or
54+
// Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i]
55+
// (and also, array[i] would have already appeared in one of LIS, identify the location and replace it)
56+
else
57+
tail[upperBound(tail, -1, length-1, array[i])] = array[i];
58+
}
59+
60+
return length;
61+
}
62+
}

Misc/LowestBasePalindrome.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import java.util.InputMismatchException;
2+
import java.util.Scanner;
3+
4+
/**
5+
* Class for finding the lowest base in which a given integer is a palindrome.
6+
* Includes auxiliary methods for converting between bases and reversing strings.
7+
*
8+
* NOTE: There is potential for error, see note at line 63.
9+
*
10+
* @author RollandMichael
11+
* @version 2017.09.28
12+
*
13+
*/
14+
public class LowestBasePalindrome {
15+
16+
public static void main(String[] args) {
17+
Scanner in = new Scanner(System.in);
18+
int n=0;
19+
while (true) {
20+
try {
21+
System.out.print("Enter number: ");
22+
n = in.nextInt();
23+
break;
24+
} catch (InputMismatchException e) {
25+
System.out.println("Invalid input!");
26+
in.next();
27+
}
28+
}
29+
System.out.println(n+" is a palindrome in base "+lowestBasePalindrome(n));
30+
System.out.println(base2base(Integer.toString(n),10, lowestBasePalindrome(n)));
31+
}
32+
33+
/**
34+
* Given a number in base 10, returns the lowest base in which the
35+
* number is represented by a palindrome (read the same left-to-right
36+
* and right-to-left).
37+
* @param num A number in base 10.
38+
* @return The lowest base in which num is a palindrome.
39+
*/
40+
public static int lowestBasePalindrome(int num) {
41+
int base, num2=num;
42+
int digit;
43+
char digitC;
44+
boolean foundBase=false;
45+
String newNum = "";
46+
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
47+
48+
while (!foundBase) {
49+
// Try from bases 2 to num (any number n in base n is 1)
50+
for (base=2; base<num2; base++) {
51+
newNum="";
52+
while(num>0) {
53+
// Obtain the first digit of n in the current base,
54+
// which is equivalent to the integer remainder of (n/base).
55+
// The next digit is obtained by dividing n by the base and
56+
// continuing the process of getting the remainder. This is done
57+
// until n is <=0 and the number in the new base is obtained.
58+
digit = (num % base);
59+
num/=base;
60+
// If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character
61+
// form is just its value in ASCII.
62+
63+
// NOTE: This may cause problems, as the capital letters are ASCII values
64+
// 65-90. It may cause false positives when one digit is, for instance 10 and assigned
65+
// 'A' from the character array and the other is 65 and also assigned 'A'.
66+
67+
// Regardless, the character is added to the representation of n
68+
// in the current base.
69+
if (digit>=digits.length()) {
70+
digitC=(char)(digit);
71+
newNum+=digitC;
72+
continue;
73+
}
74+
newNum+=digits.charAt(digit);
75+
}
76+
// Num is assigned back its original value for the next iteration.
77+
num=num2;
78+
// Auxiliary method reverses the number.
79+
String reverse = reverse(newNum);
80+
// If the number is read the same as its reverse, then it is a palindrome.
81+
// The current base is returned.
82+
if (reverse.equals(newNum)) {
83+
foundBase=true;
84+
return base;
85+
}
86+
}
87+
}
88+
// If all else fails, n is always a palindrome in base n-1. ("11")
89+
return num-1;
90+
}
91+
92+
private static String reverse(String str) {
93+
String reverse = "";
94+
for(int i=str.length()-1; i>=0; i--) {
95+
reverse += str.charAt(i);
96+
}
97+
return reverse;
98+
}
99+
100+
private static String base2base(String n, int b1, int b2) {
101+
// Declare variables: decimal value of n,
102+
// character of base b1, character of base b2,
103+
// and the string that will be returned.
104+
int decimalValue = 0, charB2;
105+
char charB1;
106+
String output="";
107+
// Go through every character of n
108+
for (int i=0; i<n.length(); i++) {
109+
// store the character in charB1
110+
charB1 = n.charAt(i);
111+
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
112+
if (charB1 >= 'A' && charB1 <= 'Z')
113+
charB2 = 10 + (charB1 - 'A');
114+
// Else, store the integer value in charB2
115+
else
116+
charB2 = charB1 - '0';
117+
// Convert the digit to decimal and add it to the
118+
// decimalValue of n
119+
decimalValue = decimalValue * b1 + charB2;
120+
}
121+
122+
// Converting the decimal value to base b2:
123+
// A number is converted from decimal to another base
124+
// by continuously dividing by the base and recording
125+
// the remainder until the quotient is zero. The number in the
126+
// new base is the remainders, with the last remainder
127+
// being the left-most digit.
128+
129+
// While the quotient is NOT zero:
130+
while (decimalValue != 0) {
131+
// If the remainder is a digit < 10, simply add it to
132+
// the left side of the new number.
133+
if (decimalValue % b2 < 10)
134+
output = Integer.toString(decimalValue % b2) + output;
135+
// If the remainder is >= 10, add a character with the
136+
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
137+
else
138+
output = (char)((decimalValue % b2)+55) + output;
139+
// Divide by the new base again
140+
decimalValue /= b2;
141+
}
142+
return output;
143+
}
144+
}

0 commit comments

Comments
 (0)