DISCSHOP help

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new
                 InputStreamReader(System.in));
    }

    int nextInt() {
        try{
            return Integer.parseInt(next());
        }catch(NullPointerException e){
            return 0;
        }
    }

    long nextLong() {
        return Long.parseLong(next());
    }
    
    int[] arry(int l) {
        try {
                st = new StringTokenizer(br.readLine());
        } catch (IOException  e) {
                e.printStackTrace();
        }
        int[] arr=new int[l];
        for (int i = 0; i < l; ++i) {
            arr[i] = Integer.parseInt(st.nextToken());
            
        }
        return arr;
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException  e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

}
public static void main (String[] args) throws java.lang.Exception
{
	FastReader fr=new FastReader();
	int t=fr.nextInt();
	while(t!=0){
	    String p=fr.next();
	    int m=Integer.parseInt(p);
	    for(int i=0;i<p.length();i++){
	        StringBuilder sb=new StringBuilder(p);
	        int tp=Integer.parseInt(sb.deleteCharAt(i).toString());
	        if(tp<m&&tp!=0){m=tp;}
	    }
	    System.out.println(m);
	    t--;
	}
}

}

This is my first code on code chef.
Worked fine with custom inputs. but the code is note accepted.

any suggestions?

Your code’s readability is poor. The Problem is fairly basic. My Approach (everyone’s will be the same):

  • Convert Given Integer to String.
  • Now, Initialise your answer to Maximum possible value, say around 10^9 + 10.
  • Write two nested loops, one to select the digit to remove, the other to find answer for that loop.
  • At every iteration, rewrite the answer with minimum value among answer and current value.

My solution:

import java.io.*;
import java.util.*;
class Main {
    static int solve(int n) {
        int ans = hell;
        String num = String.valueOf(n);
        for(int i=0;i<num.length();i++) {
            String temp = "";
            for(int j=0;j<num.length();j++) {
                if(j!=i)
                    temp += num.charAt(j);
            }
            ans = min(ans, Integer.parseInt(temp));
        }
        return ans;
    }
    public static void main(String[] args) {
        scan = new FastReader();
        int t = 0;
        if(t==0)
            t = scan.nextInt();
        for(int test = 0;test<t;test++) {
            // System.out.print("Case #"+(test+1)+": ");
            int n = scan.nextInt();
            System.out.println(solve(n));
        }
    }
    static final int mod = (int)(1e9+7);
    static final long inf = (long)(1e18);
    static final int hell = (int)(1e9+9);
    static FastReader scan;
    static int min(int a, int b) {
        return a<b?a:b;
    }
    static int max(int a, int b) {
        return a>b?a:b;
    }
    static long min(long a, long b) {
        return a<b?a:b;
    }
    static long max(long a, long b) {
        return a>b?a:b;
    }
}
class FastReader {
    BufferedReader br;
    StringTokenizer st;
    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    String next() {
        while(st==null || !st.hasMoreElements()) {
            try {
                st=new StringTokenizer(br.readLine());
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }
    int nextInt() {
        return Integer.parseInt(next());
    }
    float nextFloat() {
        return Float.parseFloat(next());
    }
    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    String nextLine() {
        String str="";
        try{
            str=br.readLine();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        return str;
    }
}

@suman_18733097
Then will my code be accepted if I initialize the variable m to the maximum possible value. I’m asking this because I don’t know in what test case my code can go wrong.

Why do you check tp!=0, it isn’t needed, I guess.

Try this test case:

1
10

My solution gives 0 and your solution gives 1 as the answer. I think 0 is the answer for this test case, since it was not mentioned Explicitly, that answer should not be 0 :wink:.

No, not needed.

int m = Integer.parseInt(p)

will also work.

oh, now I get it, then this should have made the problem, just thought that 0 is not accepted :sweat_smile:
Thanks a lot @suman_18733097