Please check my solution

My solution is getting really complex and inefficient also 5 test cases are failing please have a look at my solution and suggest changes

Hey @bhavik911
try for this
8
86922395
correct output : 4 (2 2 3 5 ye 4 CB Number)
your code gives : 2

Not able to find solution can you give more insight please

  • Put loop on string that will give substring of every length.
  • create a function that will return true if the passed number is a CB number otherwise return false.
  • To put a check if the digit is already a part of the any other CB number, create an boolean array say, valid which store which digits till now has been a part of any other CB number.
  • Take a counter and increment if a CB number is found.
  • At the end print the count.
    pehel 1 length ke substring pe work kro then 2 length … 3 length …
    because find the maximum number of CB numbers that can be formed from the given string.
package Strings;

import java.util.Scanner;

public class FindingCBNumbersRightApproach {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str = scanner.next();
        System.out.println(substring(str, n));

    }

    public static int substring(String str,int n){
        boolean[] check = new boolean[n];
        int count =0;
        for(int i =1;i<=n;i++){
            for(int j =0;j<=n-i;j++){
                String s1 =str.substring(j,j+i);
                if(check_cb(Long.parseLong(s1))){
                    boolean flag = true;
                    for(int k =j;k<j+i;k++){
                        if (check[k]){
                            flag =false;
                            break;
                        }
                    }
                    if(flag){
                        count++;
                        check[j] =true;
                    }
                }
            }
        }
        return count;
    }


    public static boolean check_cb(long check){
        int[] cb = {2,3,5,7,11,13,17,19,23,29};
        for(int i =0;i<cb.length;i++){
            if(check%cb[i]==0 && check!=cb[i] || check==1 || check==0){
                return false;
            }
        }
        return true;
    }
}

test case 1 and 3 are failing

Please mentions what might be the problem

14
23574414416111
correct output : 8
but your code gives : 9
correct code :
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String str = scanner.next();
System.out.println(substring(str, n));

}

public static int substring(String str, int n) {
	boolean[] check = new boolean[n];
	int count = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= n - i; j++) {
			String s1 = str.substring(j, j + i);

if (check_cb(Long.parseLong(s1)) && visited(check, j, j + i - 1)) {
// Mark kr raha hu
for (int k = j; k < j + i; k++) {
check[k] = true;
}

				count++;

			}
		}
	}
	return count;
}

public static boolean visited(boolean ans[], int i, int j) {
	for (; i <= j; i++) {
		if (ans[i]) {
			return false;
		}
	}
	return true;

}

public static boolean check_cb(long check) {
	int[] cb = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
	for (int i = 0; i < cb.length; i++) {
		if (check % cb[i] == 0 && check != cb[i] || check == 1 || check == 0) {
			return false;
		}
	}
	return true;
}

}

mark bhi kro element ko ye consider hogya .taki next time consider nhi ho .

I think it is marked in my solution, I have just not created another function for that.

package Strings;

import java.util.Scanner;

public class FindingCBNumbersRightApproach {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str = scanner.next();
        System.out.println(substring(str, n));

    }

    public static int substring(String str,int n){
        boolean[] check = new boolean[n];
        int count =0;
        for(int i =1;i<=n;i++){
            for(int j =0;j<=n-i;j++){
                String s1 =str.substring(j,j+i);
                if(check_cb(Long.parseLong(s1))){
                    boolean flag = true;
                    for(int k =j;k<j+i;k++){
                        if (check[k]){
                            flag =false;
                            break;
                        }
                    }
                    if(flag){
                        count++;
                        for(int l = j;l<j+i;l++){
                            check[l] =true;
                        }

                    }
                }
            }
        }
        return count;
    }


    public static boolean check_cb(long check){
        int[] cb = {2,3,5,7,11,13,17,19,23,29};
        for(int i =0;i<cb.length;i++){
            if(check%cb[i]==0 && check!=cb[i] || check==1 || check==0){
                return false;
            }
        }
        return true;
    }
}

This was the final submission

@bhavik911
Mast H
just submit it

1 Like

Please mark your doubts as resolved in your course’s. and rate full