Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Straight Max-Min Divide and Conquer
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include<iostream> using namespace std; #define MAX 1000 int a[MAX]; struct max_min{ int max, min; }; max_min straightMaxMin(int a[], int l, int r){ max_min ans; if( l == r){ ans.max = a[l]; ans.min = a[r]; return ans; } if( l == r-1 ){ if(a[l] > a[r]){ ans.max = a[l]; ans.min = a[r]; } else{ ans.max = a[r]; ans.min = a[l]; } return ans; } else { int mid = (l+r)/2;//Divide max_min leftAns, rightAns; //Conquer leftAns = straightMaxMin(a, l, mid); rightAns = straightMaxMin(a, mid+1, r); //Combine if(leftAns.max > rightAns.max) ans.max = leftAns.max; else ans.max = rightAns.max; if(leftAns.min < rightAns.min) ans.min = leftAns.min; else ans.min = rightAns.min; return ans; } } int main() { int n; cout << "Enter the number of elements: "; cin >> n; int *a = new int[n]; for(int i = 0; i < n; i++) cin >> a[i]; max_min result = straightMaxMin(a, 0, n-1); cout << "Maximum: " << result.max << endl; cout << "Minimum: " << result.min; // for(int i = 0; i < n; i++) // cout << a[i] << " "; // cout << a[n-1]; return 0; }
g++
8 50 70 60 45 35 25 75 12
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.52 sec, absolute running time: 0.16 sec, cpu time: 0.01 sec, memory peak: 6 Mb, absolute service time: 0,87 sec
edit mode
|
history
|
discussion
Enter the number of elements: Maximum: 75 Minimum: 12