Monday, 15 July 2019

largest subarray of 0's and 1's

/*You are required to complete this method*/
int largeLen(int arr[], int n) 
{
int sum = 0; 
int maxsize = -1, startindex;
      for (int i = 0; i < n-1; i++)                // Pick a starting point as i 

sum = (arr[i] == 0)? -1 : 1; 
                for (int j = i+1; j < n; j++)   // Consider all subarrays                                                                             starting from i
                   
(arr[j] == 0)? (sum += -1): (sum += 1); 

// If this is a 0 sum subarray, then 
// compare it with maximum size subarray 
// calculated so far 
              if (sum == 0 && maxsize < j-i+1) 


maxsize = j - i +1; 
startindex = i; 

    } 

if (maxsize == -1)  maxsize=0;
return maxsize; 
}

Saturday, 13 July 2019

equilibrium point in array

#include<stdio.h>
int main()
{  
int t,a[100000],n,i,j,sum=0,leftsum=0;
 scanf("%d",&t);  //test cases
 while(t--)
  {
    scanf("%d",&n);    //array size
     for(i=0;i<=n-1;++i)
         {
             scanf("%d",&a[i]); //array scan
         }
    for(i=0;i<=n-1;++i)
         {
             sum=sum+a[i];     //sum of all elements in array
         }
    for(i=0;i<=n-1;++i)
        {
         sum=sum-a[i];            //obtaining right sum
          if(leftsum==sum)      //comparing left and right
              {
                break;
              }

         leftsum=leftsum+a[i]; //updating leftsum at each  iteration               
           if(i!=n)printf("%d",i+1);// equilibrium point
           else  printf("-1\n");  //no equilibrium point
      }
return 0;
}

Friday, 12 July 2019

find the first missing positive in unsorted array

#include <stdio.h> #include<stdlib.h> int fmp(int [] ,int); //function for first missing positive int main() { int a[100000],n,i,j,temp; scanf("%d",&n); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); //array scan } j=0; for(i=0;i<=n-1;i++) //loop for separating +ve and -ve { if(a[i]<=0 ) { temp=a[i]; a[i]=a[j]; a[j]=temp; j++; } } int val=fmp(a+j,n-j); // calling fmp printf("%d",val); return 0; }

int fmp( int a[],int n) // fmp definiton { int i,size=n; // Mark a[i] as visited by making a[a[i] - 1] negative. // Note that 1 is subtracted because index start // from 0 and positive numbers start from 1 for(i = 0; i < size; i++) { if((a[i]) - 1 < size && a[(a[i]) - 1] > 0) a[ (a[i]) - 1] = -a[(a[i]) - 1]; } // Return the first index value at which is positive for(i = 0; i < size; i++) if (a[i] > 0) return i+1; // 1 is added because indexes start from 0
//any +ve more than size-1. return size+1; //no integer more than size-1 }
Time complexity:O(n)     space complexity:O(1)

Thursday, 11 July 2019

find leaders in array simple approach

#include<stdio.h>
#include<limits.h>
//the concept is to traverse array from right and comparing each element with
//it's max of left subpart and store the leader elements in other array.
//complexity:O(n)
int main()
 {
int t;
scanf("%d",&t);
while(t--)
{
    int n,max=INT_MIN;
    scanf("%d",&n);
    int a[n],b[n],count=0;
    for(int i=0;i<n;i++)  scanf("%d",&a[i]);
    for(int i=n-1;i>=0;i--)
     {
                   if(a[i]>=max)
          {
            max=a[i];
            b[count]=a[i];
             count++;
             }
              }
    for(int i=count-1;i>=0;i--) printf("%d ",b[i]);
    printf("\n");
    
   }

return 0;
}

C program to find all leaders in array using stack

#include<stdio.h>

int stack[10000],choice,n,top,x,i;//global variables declared
void push(int x);    //push declaration
void pop(void);    //pop declaration

void push(int x)
{
    if(top>=9999)
    {
        printf("\n\tSTACK is over flow");
         
    }
    else
    {
        
        top++;
        stack[top]=x;
    }
}

void pop()
{
    if(top<=-1)
    {
        printf("\n\t Stack is under flow");
    }
    else
    {
        printf("\t %d",stack[top]);
        top--;
    }
}


int main()
{
int t,n,a[100000],i,j,k,max,b[100000];
scanf("%d",&t);//number of test cases
for(k=0;k<=t-1;k++)
{
    scanf("%d",&n);
    for(i=0;i<=n-1;i++)//array scan
    {
        scanf("%d",&a[i]);
    }
max=a[n-1];
push(a[n-1]);
for(i=n-2;i>=0;i--) //scanning entire array from right side to      find leader
{
    if(max<a[i])
    {
        max=a[i];
        push(max); //pushing element onto stack
    }
    
}
for(i=top-1;i>=0;i--)   //popping all the elements (leaders)
{
pop();
}

printf("\n");     

}
}