0

when i run my code it is showing always 0 as output. this code is about binary search. and i am using buffer reader for input.

import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{ 
public static void main (String[] args) throws java.lang.Exception
{  
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   Main mainclass= new Main();
    int t =Integer.parseInt(in.readLine()); 

    for(int i = 0;i<t;i++)
    {   StringTokenizer tokenizer = new StringTokenizer(in.readLine());
        int N = Integer.parseInt(tokenizer.nextToken());
        int C = Integer.parseInt(tokenizer.nextToken());

    int[] arr= new int[N];
           for(int x=0;x<N;x++)
             {  arr[i] =Integer.parseInt(in.readLine());} 

        int res=  mainclass.bs(N,C,arr);
     System.out.println(res);
    }

}

it is bs() method inside Main class. I am actually solving problem on aggresive cows of SPOJ. here is the link: https://www.spoj.com/problems/AGGRCOW/

public  int bs(int N,int C,int[] arr)
{   Arrays.sort(arr);
    int left = 0;int right = arr[N-1]-arr[0];int mid =  arr[N-1]-arr[0];
     int check = 0; int max= -1;
        while(left<right){  

            int temp= checker(mid,arr);
            if(temp>=C)
        {    if(max<mid)
              max=mid;
            left = mid+1;
            mid = (left+right)/2;  }
            else 
            {
                right= mid;
                mid=(left+right)/2;
            }

        }
   return max;

}
public  int checker(int mid,int[] arr)
{    int N = arr.length;int f=0;int cows=1;
    for(int i=0;i<N-1;i++)
    {
        if((arr[i+1]-arr[f])>=mid)
        {
            f=i+1;
            cows++;
        }
    }
    return cows;
}

}
  • you are not showing your entire code. If you do, there is no bs method in Main, so your code won't compile. If you are just starting to learn coding, don't use StringTokenizer, it became deprecated about a decade ago. use the split method of Stringinstead – Stultuske Jan 27 '20 at 06:35
  • where is "bs" method in Main class? please share complete code that may help. – Avvappa Hegadyal Jan 27 '20 at 06:41
  • bs method is inside main class – Mukesh Yadav Jan 27 '20 at 07:07
  • it shows correct output while using console input, it is showing always 0 as output. So, if it always shows correct output, what is this question about? – Stultuske Jan 27 '20 at 07:08
  • it is the problem: https://www.spoj.com/problems/AGGRCOW/ – Mukesh Yadav Jan 27 '20 at 07:11
  • please visit this link to see my code output: https://ide.geeksforgeeks.org/gIBM2ybYI9 – Mukesh Yadav Jan 27 '20 at 07:16
  • what is the input you provide, how do you provide it, have you debugged your code? – Stultuske Jan 27 '20 at 07:18
  • I provided input by initialization like int x=3; in the program.please visit this link to see how i provide input : https://ide.geeksforgeeks.org/gIBM2ybYI9 – Mukesh Yadav Jan 27 '20 at 07:26
  • No, I mean when you enter it manually. what input do you provide, how do you enter it, what output do you expect, what output do you get? Did you debug your code? – Stultuske Jan 27 '20 at 07:27
  • Also, the code you post there is not the same code as you've posted here. What code are you running? – Stultuske Jan 27 '20 at 07:29
  • Your problem might be related to this (at least the problem I see in that other code, not in the code you've posted here) https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Stultuske Jan 27 '20 at 07:30
  • i got it what you are asking. i am getting right output when i provide input be this way: ide.geeksforgeeks.org/gIBM2ybYI9 – Mukesh Yadav Jan 27 '20 at 07:32
  • i actually when replace the above input method to console input method then the output is not showing correct output (i mean always 0 as output ) – Mukesh Yadav Jan 27 '20 at 07:35

1 Answers1

-1

Located your problem, it's with filling the initial array.

I changed your code here myself a bit, to use Scanner and split (also removed some irrelevant code), but the issue is the same:

import java.util.Arrays;
import java.util.Scanner;
class Main
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int t =scan.nextInt(); scan.nextLine();

        for(int i = 0;i<t;i++)
        {
            String[] input = scan.nextLine().split(" ");
            int N = Integer.parseInt(input[0]);
            int C = Integer.parseInt(input[1]);

            int[] arr= new int[N];
            for(int x=0;x<N;x++){
                arr[i] = scan.nextInt();
                scan.nextLine();
            }

            int res=  bs(N,C,arr);
            System.out.println("RES = " + res);
        }

    }

    public static int bs(int N,int C,int[] arr) {
        Arrays.sort(arr);
        int left = 0;int right = arr[N-1]-arr[0];int mid =  arr[N-1]-arr[0];
        int max= -1;
        while(left<right){
            int temp= checker(mid,arr);
            if(temp>=C){
                if(max<mid)
                    max=mid;
                left = mid+1;
                mid = (left+right)/2;  }
            else {
                right= mid;
                mid=(left+right)/2;
            }
        }
        return max;
    }

    public static int checker(int mid,int[] arr){
        int f=0;int cows=1;
        for(int i=0;i<arr.length-1;i++) {
            if((arr[i+1]-arr[f])>=mid) {
                f=i+1;
                cows++;
            }
        }
        return cows;
    }
}

Your problem lies here:

for(int x=0;x<N;x++){
                arr[i] = scan.nextInt();
                scan.nextLine();
            }

The value of i never changes, so you put all those ints on the same index of your array. Change that to this:

for(int x=0;x<N;x++){
                arr[x] = scan.nextInt();
                scan.nextLine();
            }
Stultuske
  • 8,465
  • 1
  • 19
  • 30