0

Question:Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. After modifying, height should be a non-negative integer. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower.

My code is passing the basic test cases,so i guess my logic is working. But upon submitting IndexOutOfBoundException is coming. Please look at the code and point the problem. https://practice.geeksforgeeks.org/problems/minimize-the-heights/0

class Pair{
    private int index;
    private int value;
    Pair(int index, int value){
        this.index=index;
        this.value=value;
    }
   public int getIndex(){
       return this.index;
   }
   
   public int getValue(){
       return this.value;
   }
}

class Solution {
    int getMinDiff(int[] a, int n, int k) {
      ArrayList<Pair> possibleHeights =new ArrayList<Pair>();
      int [] visited = new int[n];
    //   for(int i=0;i<n;i++){
    //     System.out.println(a[i]-k);
    //   } 
      for(int i=0;i<n;i++){
          if((a[i]-k)>=0)
          possibleHeights.add(new Pair(i,a[i]-k));
          possibleHeights.add(new Pair(i,a[i]+k));
          visited[i]=0;
      } 
      
    //   for(int i=0;i<possibleHeights.size();i++){
    //       System.out.println(possibleHeights.get(i).getIndex()+"--"+possibleHeights.get(i).getValue());
    //   }
    //     System.out.println("---------");
      
      Collections.sort(possibleHeights,new Comparator<Pair>(){
          public int compare(Pair p1,Pair p2){
              return p1.getValue()-p2.getValue();
          }
      });
    //   for(int i=0;i<possibleHeights.size();i++){
    //       System.out.println(possibleHeights.get(i).getIndex()+"--"+possibleHeights.get(i).getValue());
    //   }
      
      int left=0,right=0,ele=0,size=possibleHeights.size();
      while(ele<n&&right<size&&left<size){
          if(visited[possibleHeights.get(right).getIndex()]==0)
          ele++;
          visited[possibleHeights.get(right).getIndex()]++;
          right++;
      }
      int ans = possibleHeights.get(right-1).getValue()- possibleHeights.get(left).getValue();
      while(right<size&&left<size){
          if(possibleHeights.get(left).getIndex()==1)
          ele--;
          visited[possibleHeights.get(left).getIndex()]--;
          left++;
            while(ele<n&&right<size){
          if(visited[possibleHeights.get(right).getIndex()]==0)
          ele++;
          visited[possibleHeights.get(right).getIndex()]++;
          right++;
      }
      int temp=possibleHeights.get(right-1).getValue()- possibleHeights.get(left).getValue();
        if(ele==n)      
        ans=ans<temp?ans:temp;
        else 
        break;
      }
      return ans;
    }
}
  • 3
    Have you stepped through the code with a debugger? We can't actually run this code because it doesn't have a main method, so we don't know how it's being called, with what input etc. – Andy Turner May 17 '21 at 08:34
  • well the main class is already provided , you can check the question here and put my code in it. https://practice.geeksforgeeks.org/problems/minimize-the-heights/0 – Divyansh Purohit May 17 '21 at 10:42

0 Answers0