• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Maximum difference between two elements s.t larger element appears after the smaller number

April 15, 2015 by Dhaval Dave

Find maximum profit if you can buy & sell share just once.

Given an array of integers, find out the difference between any two elements such that larger element appears after the smaller number.

Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9).
This difference can be the largest profit if shares are allowed to buy and sell once.

so in simple words 
we need to find A[i] and A[j] such that A[i] < A[j] and i< j

Think what can be done to achieve that.

[2, 3, 10, 6, 4, 8, 1] in this wee nedd to find 2 and 10. 
[2, 3, 10, 1, 4, 8, 11] thn in this it should be 1 and 11.
so as we read array from index 0 to N, we can find max_number later.
but we need to keep track of minimum number.

We can keep track of 
1) minimum number , min_num found so far
2) maximum difference, max_dif  found so far.

now if for next element in array , current diff ( A [ j ] - min_num ) is larger than existing then update max diff. 
and if A[ j ] is smaller then min_num update it.

code 

#include<stdio.h>
 
int maxDiff(int a[], int length)
{
 int max_diff = a[1] - a[0];
 int min_element = a[0];
 for( int i = 1 ; i < length ; i++ )
 { 
 if(a[i] - min_num > max_diff) 
 max_diff = a[i] - min_num;
 if(a[i] < min_num)
 min_num = a[i]; 
 }
 return max_diff;
} 

int main()
{
 int a[] = {1, 2, 6, 80, 100};
 int len = sizeof(a)/sizeof(a[0]);
 printf("Maximum difference is %d", maxDiff(a, len));
 return 0;
}

 

 

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Interview Experience, Microsoft Interview Questions, problem Tagged With: Array

Reader Interactions

Trackbacks

  1. Stock Buy Sell to Maximize Profit | GoHired says:
    April 18, 2015 at 7:52 pm

    […] you can just buy and sell once thenn? -> you can think for Max and Min such that index_Max > index_Min. so this code is extension of Max and Min such that index_Max > […]

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Common Ancestor in a Binary Tree or Binary Search Tree

Printing intermediate Integers between one element & next element of array

BlueStone E-commerce Interview Experience

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Find the smallest window in a string containing all characters of another string

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Generic Object Oriented Stack with Template

Interfaces in C++ (Abstract Classes in C++)

Get K Max and Delete K Max in stream of incoming integers

Find min element in Sorted Rotated Array (With Duplicates)

Generate largest number arranging a no. of given non negative integer numbers

Circular Linked List

Longest Increasing Subsequence

Generate next palindrome number

Closed Parentheses checker

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Python List

LeetCode: Binary Tree Maximum Path Sum

Convert number to words java

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Leetcode: Edit Distance

Minimum insertions to form a palindrome

TicTacToe Game As Asked in Flipkart

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Fibonacci Hashing & Fastest Hashtable

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Count Possible Decodings of a given Digit Sequence

Doubly linked list

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Python Dictionaries

Copyright © 2025 · Genesis Framework · WordPress · Log in