Add

Java Array Coding 4 Problems for Interviews

 


1.Stock span problem

The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days.

The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.

For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.

Example 1:

Input:

N = 7, price[] = [100 80 60 70 60 75 85]

Output:

1 1 1 2 1 4 6

Explanation:

Traversing the given input span for 100

will be 1, 80 is smaller than 100 so the

span is 1, 60 is smaller than 80 so the

span is 1, 70 is greater than 60 so the

span is 2 and so on. Hence the output will

be 1 1 1 2 1 4 6.

Example 2:

Input:

N = 6, price[] = [10 4 5 90 120 80]

Output:

1 1 2 4 5 1

Explanation:

Traversing the given input span for 10

will be 1, 4 is smaller than 10 so the

span will be 1, 5 is greater than 4 so

the span will be 2 and so on. Hence, the

output will be 1 1 2 4 5 1.

// This Code run for all test case but

// Time Limit Exceeded

// anybody suggest else method.

vector <int> calculateSpan(int price[], int n)

   {

      // Your code here

       vector<int> s;

      s.push_back(1);

      for(int i=1; i<n; i++)

      {

          int jump=1;

          int pos=i;

          for(int j=i; j>0; j--) // j=75 60

          {

              if(price[pos]>=price[j-1]) // 75>60 60>

              jump++; // 1 2

              else

               break;

          }

          s.push_back(jump);

      }

      return s;

   }

2.Triplet Sum in Array

Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X.

Example 1:

Input:

n = 6, X = 13

arr[] = [1 4 45 6 10 8]

Output:

1

Explanation:

The triplet {1, 4, 8} in

the array sums up to 13.

Example 2:

Input:

n = 5, X = 10

arr[] = [1 2 4 3 6]

Output:

1

Explanation:

The triplet {1, 3, 6} in

the array sums up to 10.

C++ Solution || Using two pointer technique

Execution Time -: 0.1

class Solution{

    public:

    //Function to find if there exists a triplet in the

    //array A[] which sums up to X.

    bool find3Numbers(int A[], int n, int X)

    {

        //Your Code Here

        sort(A,A+n);

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

            int l=0,r=n-1;

            while(l<r){

                if(i==l)

                    l++;

                else if(i==r)

                    r--;

                else{

                    int sum = A[i]+A[l]+A[r];

                    if(sum==X)

                        return true;

                    else if(sum<X)

                        l++;

                    else if(sum>X)

                        r--;

                }

            }

        }

        return false;

    }

 

};

bool search(int k,int a[], int n)

   {

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

       {

           if(a[i]==k)

             return true;

       }

       return false;

   }

   int missingNumber(int arr[], int n)

   {

       // Your code here

      int l;

      l=arr[0];

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

      {

          if(l<arr[i])

          l=arr[i];

      }

            int k=1;

     while(k<=l)

      {

          if(search(k,arr,n)==true)

          k++;

                else    

          return k;

      }

   }

Time Limit Exceeded .

3.Row with max 1s

Given a boolean 2D array of n x m dimensions where each row is sorted. Find the 0-based index of the first row that has the maximum number of 1's.

Example 1:

Input:

N = 4 , M = 4

Arr[][] = {{0, 1, 1, 1},

           {0, 0, 1, 1},

           {1, 1, 1, 1},

           {0, 0, 0, 0}}

Output: 2

Explanation: Row 2 contains 4 1's (0-based

indexing).

Example 2:

Input:

N = 2, M = 2

Arr[][] = {{0, 0}, {1, 1}}

Output: 1

Explanation: Row 1 contains 2 1's (0-based

indexing).

int rowWithMax1s(vector<vector<int> > arr, int n, int m)

{

    int ans = 0;

    int j = m-1;

    int ind  = -1;

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

    {

        if(arr[i][j] == 1)

        {

            for (j;j>=0;j--)

            {

                if (arr[i][j] == 1)

                continue;

                else

                break;

            }

            if ((m-(j+1)) > ans)

            {

             ind = i;  

            ans = max(ans, (m-(j+1)));

            }

        }

        if (j == -1)

        break;

    }

    if (ans == 0)

    return -1;

    return ind;

}

4.Spirally traversing a matrix

Given a matrix of size r*c. Traverse the matrix in spiral form.

Example 1:

Input:

r = 4, c = 4

matrix[][] = {{1, 2, 3, 4},

           {5, 6, 7, 8},

           {9, 10, 11, 12},

           {13, 14, 15,16}}

Output:

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Explanation:

Example 2:

Input:

r = 3, c = 4 

matrix[][] = {{1, 2, 3, 4},

           {5, 6, 7, 8},

           {9, 10, 11, 12}}

Output:

1 2 3 4 8 12 11 10 9 5 6 7

Explanation:

Applying same technique as shown above,

output for the 2nd testcase will be

1 2 3 4 8 12 11 10 9 5 6 7.

import java.util.*;

public class sipraltraversal {

   public static void main(String[] args) {

       //code

       int minr=0;

       int minc=0;

       int maxr= arr.length-1;

       int maxc=arr[0].length-1;

       int totalelements=n*m;

       int count=0;

       //count<totalelements we use this for chceking the last box . in few cases last box is not with 4 walls. if we do not use this we may print duplicate elements .

       while(count<totalelements)

       {

           //left wall

           // col fix (minc)

           // row change min to max

           for (int i = minr,j=minc; i <=maxr && count<totalelements; i++) {

               System.out.println(arr[i][j]);

               count++;

           }

           minc++;// for avoiding corners

           //bottom wall

           //row fix (maxr)

           //col change (min to max)

           for(int i=maxr,j=minc;j<=maxc && count<totalelements;j++)

           {

               System.out.println(arr[i][j]);

               count++;

           }

           maxr--;//for avoiding corners

           //right wall

           //col fix(maxc)

           //row change (max to min)

           for(int i=maxr,j=maxc;i>=minr && count<totalelements;i--)

           {

               System.out.println(arr[i][j]);

               count++;

           }

           maxc--;//for avoiding corners

           //top wall

           //row fix(minr)

           //col change (max to min)

           for(int i=minr,j=maxc;j>=minc && count<totalelements;j--)

           {

               System.out.println(arr[i][j]);

               count++;

           }

           minr++;//for avoiding corners

       }

   }

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.