Libin Thambi
BAN USERProgrammer
posted wrong version of code
public class MissingNumber {
public static void main(String[] args) {
String str = "101113";
long [] strArray = new long[str.length()/2];
String numS = "";
int num = 0;
int nextNum = 0;
long seq = 0;
for (int i = 0; i < str.length()/2; i++) {
numS = ""+str.charAt(i);
nextNum = Integer.valueOf(numS);
if(num!=0){
nextNum = num * 10 + nextNum;
}
num = nextNum;
strArray[i]= nextNum;
}
for (int i = strArray.length-1; i >= 0 && strArray[i]!=0; i--) {
if(checkSequence(strArray[i],str)){
seq = strArray[i];
break;
}
}
for(int i = 0; true ; i++){
if(!str.contains(Long.toString(seq+i))){
System.out.println("The missing number is ::::: "+(seq+i));
break;
}
}
}
static boolean checkSequence(long l,String str){
//System.out.println(l);
long indexCheck = 0;
int count = 0;
for(int i = 0; i<3 ; i++){
if(count!=2 && str.contains(Long.toString(l+i))){
if(i!=0&&str.indexOf(Long.toString(indexCheck))!=0){
if(str.indexOf(Long.toString(l+i)) > str.indexOf(Long.toString(indexCheck))&&str.indexOf(Long.toString(l+i))%str.indexOf(Long.toString(indexCheck))!=0){
count++;
if(count==2){
return false;
}
}else if(str.indexOf(Long.toString(indexCheck)) > str.indexOf(Long.toString(l+i))&&str.indexOf(Long.toString(indexCheck))%str.indexOf(Long.toString(l+i))!=0){
count++;
if(count==2){
return false;
}
}
}
if(str.indexOf(Long.toString(l+i))>0){
indexCheck = l+i;
}
continue;
}
else if (count < 1){
count++;
}else{
return false;
}
}
return true;
}
}
public class MissingNumber {
public static void main(String[] args) {
String str = "9899100101103104105";
long [] strArray = new long[str.length()/2];
String numS = "";
int num = 0;
int nextNum = 0;
long seq = 0;
for (int i = 0; i < str.length()/2; i++) {
numS = ""+str.charAt(i);
nextNum = Integer.valueOf(numS);
if(num!=0){
nextNum = num * 10 + nextNum;
}
num = nextNum;
strArray[i]= nextNum;
}
for (int i = strArray.length-1; i >= 0 && strArray[i]!=0; i--) {
if(checkSequence(strArray[i],str)){
seq = strArray[i];
break;
}
}
for(int i = 0; true ; i++){
if(!str.contains(Long.toString(seq+i))){
System.out.println("The missing number is ::::: "+(seq+i));
break;
}
}
}
static boolean checkSequence(long l,String str){
int count = 0;
for(int i = 0; i<5 ; i++){
if(count!=2 && str.contains(Long.toString(l+i))){
continue;
}
else if (count < 1){
count++;
}else{
return false;
}
}
return true;
}
}
Algorithm:
1. Split the string to long array to find the sequence.
2. For example, String s = "123" should converted into {1,12,123}
3. Using long array, start from the end and check for the missing number to find the correct sequence.
4. After finding the sequence find the missing number
PFB the updated code. Check and let me know if the solution is not valid for any input.
public class MyLearning {
public static void main(String[] args) {
boolean flag = true;
String a = "686";
int j =0;
int c[] = new int[a.length()];
for (int i = a.length()-1; i >=0; i--) {
c[i]= a.charAt(i)-48;
if(i< a.length() - 1 && c[i] == 9 && flag){
continue;
}else if(i == a.length() - 1 && flag && c[i] == 0){
for( j = a.length() - 2 ;j >= 0 ;j--){
if(c[j]!=0&&j!=0){
c[a.length() - 1]= 1;
c[a.length() - 2]=c[j]-1 ;
c[j]=0;
flag = false;
continue;
}else if(j==0){
flag = false;
}
}
}
else if (i < a.length() - 1 && flag ){
c[i]= c[i]+1;
int temp = c[i+1];
c[i+1]= c[a.length()-1]-1;
for( j = a.length() -1 ;j > i+1 ;j--){
c[j]= c[j-1];
}
if(j != a.length() -1)
c[j+1] = temp;
flag = false;
}
}
String b = "";
for (int i = 0; i < c.length; i++) {
b = b+c[i];
}
if (b.equalsIgnoreCase(a)) {
System.out.println("doesn't exist");
}else{
System.out.println(b);
}
}
}
PFB the solution in java. It works fine for all inputs.
package test;
public class MyLearning {
public static void main(String[] args) {
boolean flag = true;
String a = "999999999999212111199999";
int c[] = new int[a.length()];
for (int i = a.length()-1; i >=0; i--) {
c[i]= a.charAt(i)-48;
if(i< a.length() - 1 && c[i] == 9 && flag){
continue;
}else if (i< a.length() - 1 && flag){
c[i+1] = c[i+1] - 1 ;
c[i] = c[i] + 1;
flag = false;
}
}
String b = "";
for (int i = 0; i < c.length; i++) {
b = b+c[i];
}
if (b.equalsIgnoreCase(a)) {
System.out.println("doesn't exist");
}else{
System.out.println(b);
}
}
}
Printing even numbers less than 101 in red colour and printing odd numbers less than 101 in black colour - console output
- Libin Thambi October 06, 2013