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
/*
 * Given an unsorted array of size N.
 * Find the first element in array such that all of its left elements
 * are smaller and all right elements to it are greater than it.
 */
public class Median {
 public static void main(String[] args) {
//    int[] A = {8,5,6,9,10,11,4,12,13,14}; //Ans : 12
//    int[] A = {4,2,5,7}; //Ans : 5
//    int[] A = {11,9,12}; //Ans : -1
//    int[] A = {4,3,2,7,8,9}; //Ans : 7
//  int[] A = {1,2,3,4,5,6,7,8,9}; //Ans : 2
//    int[] A = {1,4,3,2,5,6,7,8,9}; //Ans : 5
  int[] A = {1,4,3,2,10,6,7,8,9}; //Ans : -1
  int len = A.length;
  int max = Integer.MIN_VALUE;
  int min = Integer.MAX_VALUE;
  int maxI = -1;
  int minI = -1;
  boolean found = false;
  for(int i = 0; i < len; i++) {
   int value = A[i];
   if(max < value) {
    max = value;
    maxI = i;
    found = true;
    continue;
   }
   if(min > value) {
    min = value;
    minI = i;
    found = true;
   }
   if(max > value && maxI < i)
    found = false;
  }
  if(maxI > minI && len-1 > minI+1 && minI+1 > 0 && found)
   System.out.println(A[minI+1]);
  else if(minI == -1 && found) {
   if(A[0] < A[1])
    System.out.println(A[1]);
  } else
   System.out.println(-1);
 }
}
 Bit simpler way - Traverse left to right to mark the index of largest number till the current index. Then traverse right to left to check if the current number is minimum & also check maxNum[curIndex] == -1
Picture
Powered by Create your own unique website with customizable templates.