problems related to arrays


class Main {

    public static void main(String[] args) {

       int [] arr={1,2,-1,-2};

       int [] temp_neg=new int [arr.length];

       int [] temp_pos=new int [arr.length];

       int posindex=0;

       int negindex=0;

       //storing positive and negative array values

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

           if(arr[i]<0){

               temp_neg[negindex++]=arr[i];

           }

           else{

               temp_pos[posindex++]=arr[i];

           }

       }

       int [] combine=new int[temp_neg.length+temp_pos.length];

       int i=0,j=0,k=0;

       //iterating alternatively

       while(i<negindex && j<posindex){

           combine[k++]=temp_neg[i++];

           combine[k++]=temp_pos[j++];

       }

       //if any reaminig elements int the array by using this we can be able to print

       while(i<negindex){

           combine[k++]=temp_neg[i++];

       }

       while(j<posindex){

           combine[k++]=temp_pos[j++];

       }

       

       

       for(int ele:combine){

           System.out.println(ele);

       }

    }

}

//subarrays

// 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={10,20,30,40,50};

        int n=arr.length;

        int ln=n*(n-1)/2;

        int k1=30;

        int [] temp=new int [arr.length];

        int index=0,sum=0;

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

            for(int j=i;j<n;j++){

                for(int k=i;k<=j;k++){

                   

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

                }

                        System.out.println();

            }

        }

        System.out.println(ln);

    }

}

//kandae's algorithm maximum subarrays sum:

// 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={4,-1,2,1};

       int cur_sum=arr[0];

      // int max_sum=arr[0];

       for(int i=1;i<arr.length;i++){

           if(cur_sum+arr[i]>arr[i]){

               cur_sum=cur_sum+arr[i];

           }

           else{

               cur_sum=arr[i];

           }

       }

       System.out.println(cur_sum);

    }

}

//for finding minimum sum of sub 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={10,20,30};

       int cur_sum=arr[0];

       int min_sum=arr[0];

       for(int i=1;i<arr.length;i++){

           if(cur_sum+arr[i]<arr[i]){

               cur_sum=cur_sum+arr[i];

           }

           else{

               cur_sum=arr[i];

           }

           if(cur_sum<min_sum){

               min_sum=cur_sum;

           }

       }

       System.out.println(min_sum);

    }

}

// finding the sub array which has binary digits  max length

// 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={0,1,0,1};

       int max=Integer.MIN_VALUE;

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

           if(arr[i]==0){

               arr[i]=-1;

           }

       }

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

           int sum=0;

           for(int j=i;j<arr.length;j++){

               sum+=arr[j];

               if(sum==0){

                   int length=j-i+1;

                   if(length>max){

                       max=length;

                   }

               }

           }


       }

       System.out.println(max);

    }

}

//finding incresing contigous subarray:

// 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={-1,1,-1,1};

        int cur_len=1;

        int max_len=1;

        for(int i=1;i<arr.length;i++){

            if(arr[i]>arr[i-1]){

                cur_len++;

            }

            else{

                cur_len=1;

            }

             if(cur_len>max_len){

                max_len=cur_len;

            }

            

        }

        System.out.println(max_len);

    }

}

//sliding window first neagative:

class Main {

    public static void main(String[] args) {

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

        int N = arr.length;

        int K = 3;


        for (int i = 0; i <= N - K; i++) {

            boolean found = false;

            // Check each element in this window

            for (int j = i; j < i + K; j++) {

                if (arr[j] < 0) {

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

                    found = true;

                    break; // stop after first negative

                }

            }

            if (!found) {

                System.out.print("0 ");

            }

        }

    }

}

//finding sum==target of the subarray:
// 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 = {1, 2, 3};
       int  target = 3;
       for(int i=0;i<arr.length;i++){
            int sum=0;
           for(int j=i;j<arr.length;j++){
               sum+=arr[i];
               if(sum==target){
                   for(int k=i;k<=j;k++){
                   System.out.print(arr[k]+" ");
               }
               }
             
           }
           System.out.println();
       }

    }
}
//finding min len of all subarray's:
// 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 = {2, 3, 1, 2, 4, 3};
        int target = 7;
        int n=arr.length;
        int minlen=Integer.MAX_VALUE;
        int sum=0,start=0;
        for(int i=0;i<n;i++){
         sum+=arr[i];
         while(sum>=target){
             int len=i-start+1;
             if(len<minlen){
                 minlen=len;
             }
             sum-=arr[start];
             start++;
         }
        }
        System.out.println((minlen==Integer.MAX_VALUE)?0:minlen);
    }
}
//Naive approach for maximum element in the subarray with value k:
// 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 = {1, 3, -1, -3, 5, 3, 6, 7}; 
     int   k = 3;
     int n=arr.length;
     for(int i=0;i<=n-k;i++){
         int max=Integer.MIN_VALUE;
         for(int j=i;j<i+k;j++){
            if(arr[j]>max){
                max=arr[j];
            } 
         }
         System.out.println(max);
     }

    }
}
//finding leaders 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=arr[arr.length-1];
       int [] temp=new int [arr.length];
       int count=0;
       temp[count++]=max;
       for(int i=arr.length-2;i>=0;i--){
           if(arr[i]>max){
               max=arr[i];
               temp[count++]=max;
           }
       }
       for(int i=count-1;i>=0;i--){
           System.out.print(temp[i]+" ");
       }
       
    }
}
//finding each matches of one
// 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={0,1,1,0,1,1,1};
       int count=0;
       for(int i=1;i<arr.length;i++){
            if (arr[i]==0) continue;
           if(arr[i]==arr[i-1]){
               count++;
           }
            
       }
       System.out.println(count);
    }
}

Comments

Popular posts from this blog

optimsed apporach

what i learn

creative codes