optimsed apporach

//For finding buying and seeling the stock: 

class Main {

    public static void main(String[] args) {

        int[] price = {8, 2, 1, 5, 4, 6};

        int min = Integer.MAX_VALUE;

        int max = 0;

        for(int i=0;i<price.length;i++){

            if(price[i]<min){

                min=price[i];

            }

            else if(price[i]-min>max){

                max=price[i]-min;

            }

        }

        System.out.println("Buy:"+min);

                System.out.println("sell:"+max);

    }

}

//finiding leader in an array:

// Online Java Compiler

// Use this editor to write, compile and run your Java code online


class Main {

    public static void main(String[] args) {

       int [] arr = {16, 17, 4, 3, 5, 2};

       int max_right=arr[arr.length-1];

       System.out.println(max_right);

       for(int i=arr.length-2;i>=0;i--){

           if(arr[i]>max_right){

               max_right=arr[i];

                System.out.println(max_right);

           }

       }

    }

}

Sliding Window First Negative - Dry Run

class Main {

    public static void main(String[] args) {

        int[] arr = {12, -1, -7, 8, -15, 30, 16, 28};

        int k = 3;

        int n = arr.length;


        int negIndex = 0; // Pointer to track first negative in window


        for (int i = 0; i <= n - k; i++) {

            // Move negIndex to stay within the window

            while (negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)) {

                negIndex++;

            }


            // Print result for current window

            if (negIndex < i + k) {

                System.out.print(arr[negIndex] + " ");

            } else {

                System.out.print("0 ");

            }

        }

    }

}


Array: {12, -1, -7, 8, -15, 30, 16, 28}
k = 3 (window size)
Initial negIndex = 0


Window 1: i = 0 (elements: 12, -1, -7)

  • Window range: [0, 2]
  • Current negIndex: 0
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: negIndex(0) < 0 + 3(3) && (0 < 0 || arr[0](12) >= 0)
  • Result: 0 < 3 && (false || true)true && truetrue
  • Action: Increment negIndex: negIndex = 1
  • While loop again: 1 < 3 && (1 < 0 || arr[1](-1) >= 0)
  • Result: 1 < 3 && (false || false)true && falsefalse
  • Exit while loop: negIndex = 1
  • Check if negIndex < i + k: 1 < 3true
  • Output: arr[1] = -1

Window 2: i = 1 (elements: -1, -7, 8)

  • Window range: [1, 3]
  • Current negIndex: 1
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: 1 < 1 + 3(4) && (1 < 1 || arr[1](-1) >= 0)
  • Result: 1 < 4 && (false || false)true && falsefalse
  • Exit while loop: negIndex remains 1
  • Check if negIndex < i + k: 1 < 4true
  • Output: arr[1] = -1

Window 3: i = 2 (elements: -7, 8, -15)

  • Window range: [2, 4]
  • Current negIndex: 1
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: 1 < 2 + 3(5) && (1 < 2 || arr[1](-1) >= 0)
  • Result: 1 < 5 && (true || false)true && truetrue
  • Action: Increment negIndex: negIndex = 2
  • While loop again: 2 < 5 && (2 < 2 || arr[2](-7) >= 0)
  • Result: 2 < 5 && (false || false)true && falsefalse
  • Exit while loop: negIndex = 2
  • Check if negIndex < i + k: 2 < 5true
  • Output: arr[2] = -7

Window 4: i = 3 (elements: 8, -15, 30)

  • Window range: [3, 5]
  • Current negIndex: 2
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: 2 < 3 + 3(6) && (2 < 3 || arr[2](-7) >= 0)
  • Result: 2 < 6 && (true || false)true && truetrue
  • Action: Increment negIndex: negIndex = 3
  • While loop again: 3 < 6 && (3 < 3 || arr[3](8) >= 0)
  • Result: 3 < 6 && (false || true)true && truetrue
  • Action: Increment negIndex: negIndex = 4
  • While loop again: 4 < 6 && (4 < 3 || arr[4](-15) >= 0)
  • Result: 4 < 6 && (false || false)true && falsefalse
  • Exit while loop: negIndex = 4
  • Check if negIndex < i + k: 4 < 6true
  • Output: arr[4] = -15

Window 5: i = 4 (elements: -15, 30, 16)

  • Window range: [4, 6]
  • Current negIndex: 4
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: 4 < 4 + 3(7) && (4 < 4 || arr[4](-15) >= 0)
  • Result: 4 < 7 && (false || false)true && falsefalse
  • Exit while loop: negIndex remains 4
  • Check if negIndex < i + k: 4 < 7true
  • Output: arr[4] = -15

Window 6: i = 5 (elements: 30, 16, 28)

  • Window range: [5, 7]
  • Current negIndex: 4
  • While loop condition: negIndex < i + k && (negIndex < i || arr[negIndex] >= 0)
  • Check: 4 < 5 + 3(8) && (4 < 5 || arr[4](-15) >= 0)
  • Result: 4 < 8 && (true || false)true && truetrue
  • Action: Increment negIndex: negIndex = 5
  • While loop continues: 5 < 8 && (5 < 5 || arr[5](30) >= 0)
  • Result: 5 < 8 && (false || true)true && truetrue
  • Action: Increment negIndex: negIndex = 6
  • While loop continues: 6 < 8 && (6 < 5 || arr[6](16) >= 0)
  • Result: 6 < 8 && (false || true)true && truetrue
  • Action: Increment negIndex: negIndex = 7
  • While loop continues: 7 < 8 && (7 < 5 || arr[7](28) >= 0)
  • Result: 7 < 8 && (false || true)true && truetrue
  • Action: Increment negIndex: negIndex = 8
  • While loop continues: 8 < 8false
  • Exit while loop: negIndex = 8
  • Check if negIndex < i + k: 8 < 8false
  • Output: 0 (no negative number in this window)

Final Output

-1 -1 -7 -15 -15 0

How negIndex Works

  1. Efficiency: Instead of scanning each window from scratch, negIndex remembers where we found the last negative number
  2. Forward Movement: negIndex only moves forward, never backward
  3. Window Validation: The condition negIndex < i ensures the negative number is actually within the current window
  4. Skip Non-negatives: The condition arr[negIndex] >= 0 helps skip positive numbers to find the first negative

Comments

Popular posts from this blog

what i learn

creative codes