Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
To find the minimum start & end point of a n*n matrix, i.e. distance between (0, 0)  & (n-1, n-1). Value at each block represents the cost.


public class Paths {

 public static void main(String[] args) {
  int[] input2 = {5,7,4,6,2,3,5,1,3,1,9,7,1,6,8,8};
  int[] input1 = {4,4};
  System.out.println("Minimum cost : " + no_of_path(input1, input2));
 }

 public static int no_of_path(int[] input1,int[] input2)
 {
  int[][] arr = new int[input1[0]][input1[1]];
  int k = 0;

  for(int i = 0; i < input1[0]; i++){
   for(int j = 0; j < input1[1]; j++){
    arr[i][j] = input2[k];
    k++;
   }
  }

  int cost = paths(arr, 0, 0, input1[0], input1[1]);
  return cost;
 }

 public static int paths(int[][] arr, int i, int j, int row, int col){
  int c = 0;
  int d = 0;
  int r = 0;
  if(i == row-1 && j == col-1)
   return arr[i][j];
  if(i < row && j+1 < col){
   c = paths(arr, i, j+1, row, col);
   c = c + arr[i][j];
  }
  if(i+1 < row && j+1 < col){
   d = paths(arr, i+1, j+1, row, col);
   d = d + arr[i][j];
  }
  if(i+1 < row && j < col){
   r = paths(arr, i+1, j, row, col);
   r = r + arr[i][j];
  }

  if(c ==0 || d == 0 || r == 0)
   return c+d+r;
  else if(c <= d && c <=r)
   return c;
  else if(d <= c && d <=r)
   return d;
  else if(r <= c && r <=d)
   return r;
  else
   return 0;
 }
}

Powered by Create your own unique website with customizable templates.