Below are the 2 questions asked in one IT company to code online -
import java.util.HashSet;
public class Palindrome {
public static void main(String[] args) {
System.out.println(findPalindrome("123"));
}
public static int findPalindrome(String number) {
int len = number.length();
int last = len-1;
int start = 0;
int max = 0;
while(start < last) {
char init = number.charAt(start);
int first = start+1;
while(first < last) {
if(number.charAt(first) == init)
break;
first++;
}
if(number.charAt(first) == init && (first-start+1 > max || last-start > max))
max = getMax(number, start, first, last, max, 0);
start++;
}
return max;
}
public static int getMax(String number, int start, int first, int last, int max, int count) {
if(count == 2)
return max;
int org = last;
if(count == 1)
last = first;
count++;
int tempLen = 0;
int tempStart = start;
HashSet<Character> chars = new HashSet<>();
while(tempStart <= last) {
if(!chars.add(number.charAt(tempStart))) {
chars.remove(number.charAt(tempStart));
tempLen+=2;
}
if(chars.isEmpty()) {
if(tempLen > max)
max = tempLen;
}
tempStart++;
}
return getMax(number, start, first, org, max, count);
}
}
public class Palindrome {
public static void main(String[] args) {
System.out.println(findPalindrome("123"));
}
public static int findPalindrome(String number) {
int len = number.length();
int last = len-1;
int start = 0;
int max = 0;
while(start < last) {
char init = number.charAt(start);
int first = start+1;
while(first < last) {
if(number.charAt(first) == init)
break;
first++;
}
if(number.charAt(first) == init && (first-start+1 > max || last-start > max))
max = getMax(number, start, first, last, max, 0);
start++;
}
return max;
}
public static int getMax(String number, int start, int first, int last, int max, int count) {
if(count == 2)
return max;
int org = last;
if(count == 1)
last = first;
count++;
int tempLen = 0;
int tempStart = start;
HashSet<Character> chars = new HashSet<>();
while(tempStart <= last) {
if(!chars.add(number.charAt(tempStart))) {
chars.remove(number.charAt(tempStart));
tempLen+=2;
}
if(chars.isEmpty()) {
if(tempLen > max)
max = tempLen;
}
tempStart++;
}
return getMax(number, start, first, org, max, count);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Matches {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
ArrayList<Integer> teams = new ArrayList<>();
while(cases > 0) {
int count = sc.nextInt();
teams.add(count);
cases--;
}
teams.stream()
.map(team -> getMatchesCount(team))
.forEach(System.out::println);
sc.close();
}
public static int getMatchesCount(int teams) {
int count = 0;
while(teams > 1) {
int div = teams/2;
int rem = teams%2;
count+=div;
teams = div+rem;
}
return count;
}
}
import java.util.Scanner;
public class Matches {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
ArrayList<Integer> teams = new ArrayList<>();
while(cases > 0) {
int count = sc.nextInt();
teams.add(count);
cases--;
}
teams.stream()
.map(team -> getMatchesCount(team))
.forEach(System.out::println);
sc.close();
}
public static int getMatchesCount(int teams) {
int count = 0;
while(teams > 1) {
int div = teams/2;
int rem = teams%2;
count+=div;
teams = div+rem;
}
return count;
}
}