Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems >
        • Problem10
        • Problem300
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases >
        • Java8 >
          • Performance
          • NasHorn
          • WordCount
          • Thoughts
        • Java9 >
          • ServiceLoaders
          • Lambdas
          • List Of Objects
          • Code Snippets
        • Java14 >
          • Teeing
          • Pattern
          • Semaphores
        • Java17 >
          • Switches
          • FunctionalStreams
          • Predicate
          • Consumer_Supplier
          • Collectors in Java
        • Java21 >
          • Un-named Class
          • Virtual Threads
          • Structured Concurrency
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • Strings >
          • Reverse the String
          • Reverse the String in n/2 complexity
          • StringEditor
          • Reversing String
          • String Puzzle
          • Knuth Morris Pratt
          • Unique characters
          • Top N most occurring characters
          • Longest Common Subsequence
          • Longest Common Substring
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • 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
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • Singleton >
          • Singletons
        • Strategy
  • Frameworks
    • Apache Velocity
    • React Library >
      • Tutorial
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Custom Beans
        • 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
      • Kubernetes
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
    • Dynamic loading of agents
  • 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
Above solution iterates the array twice, once left to right to find the index of maximum number in the left & then right to left to find the index of minimum number & then it compares if the current number is greater than maximum number in left & smaller than the minimum number in the right.
But below traverses through the array once & keeps the record of the number which is greater than all in its left & smaller than all in its right.
Picture
Powered by Create your own unique website with customizable templates.