Showing posts with label Adhoc Array Problems. Show all posts
Showing posts with label Adhoc Array Problems. Show all posts

Tuesday, October 25, 2016

Array Product

Problem source gfg

http://stackoverflow.com/questions/2680548/given-an-array-of-numbers-return-array-of-products-of-all-other-numbers-no-div

Given an array of numbers, return array of products of all other numbers (no division)


solution:

#include <iostream>
using namespace std;

void fill(int *a,int size)
{
    for(int i=0;i<size;i++)
    {
        a[i] = 1;
    }
}
int* createProduct(int* a,int size)
{
  
  
    int temp = 1;
    int* products = new int[size];
    fill(products,size);
    for(int i=0;i<size;i++)
    {
        products[i] = temp;
        temp *= a[i];
    }
    temp = 1;
    for(int i=size-2;i>=0;i--)
    {
        
        temp *= a[i+1];
        products[i] *= temp;
    }
    return products;
    
}
void disp(int* a,int size)
{
       for(int i=0;i<size;i++)
    {
        cout<<a[i]<<"\t";
    }
}
int main(int argc, const char * argv[]) {
    // insert code here...
    int a[] = {6,7,8,4,5};
    
    int size = sizeof(a)/sizeof(*a);
    //cout<<sizeof(*a);
    int* res = createProduct(a,size);
    disp(res,size);
    return 0;
}

Friday, August 26, 2016

GeeksForGeeks Print Martix in Spiral Order


Print a given matrix in spiral form

Given a 2D array, print it in spiral form. See the following examples.
Input:
        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 


Input:
        1   2   3   4  5   6
        7   8   9  10  11  12
        13  14  15 16  17  18
Output: 
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11

Below is the clear link for explanation and pseudo code implementation


https://www.youtube.com/watch?v=siKFOI8PNKM&feature=youtu.be

Solution in Java

public class Solution {
// DO NOT MODIFY THE LIST
public ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> a) {

                //Initialization of 4 pointers top bottom left and right
int top = 0,bottom = a.size()-1,left = 0,right = a.get(0).size()-1;
                // below variable will control the direction
int direction = 0;
ArrayList<Integer> res = new ArrayList<Integer>();
               // the while loop will execute until the top meets bottom and left meets right
while(bottom>=top && right>=left)
{
   if(direction==0)
   {
       for(int i =left;i<=right;i++)
       {
         
           res.add(a.get(top).get(i));
       }
     
       top++;
   }
   else if(direction==1)
   {
       for(int i =top;i<=bottom;i++)
       {
           res.add(a.get(i).get(right));
       }
     
       right--;
   }
   else if(direction==2)
   {
       for(int i=right;i>=left;i--)
       {
           res.add(a.get(bottom).get(i));
       }
     
       bottom--;
   }
   else if(direction==3)
   {
       for(int i =bottom;i>=top;i--)
       {
           res.add(a.get(i).get(left));
       }
     
       left++;
   }
                   //this helps to put back the direction to 0 so that we are again pointed to the top row of the
                   //matrix
   direction = (direction+1)%4;
}
   

return res;
}
}