Комментарии:
Hi, I followed the link you sent me in the Hackerrank discussion section. Great video!
I always like better optimized code that is legible.
While I'm talking about optimization even though I'm very much a noob, would it not save a calculation to use "else if" in your loop as by the time you have finished the comparison
if ( arr[i] < min ), the second comparison (arr[i] > max) becomes an impossibility, or is this change just very insignificant in the scheme of things?
Lastly, good luck with your channel. I hope to see more "thought-process" hacker rank tutorials like this that don't just dive into solving the problem but explaining.
I might recommend you add some narration if its not too taxing as I think that would greatly increase the quality of the videos.
typeScript solution
function miniMaxSum(arr: number[]): void {
const orderValues:number[] = arr.sort()
const minValue:number = orderValues[0];
const maxValue:number = orderValues[orderValues.length - 1];
let sumValues:number = arr.reduce((fristValue,secondValue)=>fristValue + secondValue, 0)
console.log((sumValues - maxValue), (sumValues-minValue));
}
gracias que pro.
Ответитьgood explaination
ОтветитьThis requires O(nlogn),we can do it O(n)
Ответитьwhy you subtract min and max element from sum logic is not clear
ОтветитьWhy do we have to declare min, max, sum with long type? I really dont get it!
Ответитьimport java.util.*;
import java.util.Arrays.*;
class Solution{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n =sc.nextInt();
int[] a =new int[n];
for (int i = 0; i <n; i++) {
a[i] =sc.nextInt();
}
Arrays.sort(a);
int max = 0,min =0;
for (int i = 0; i <n-1; i++) {
min = min +a[i];
}
for (int i = 1; i <n; i++) {
max = max +a[i];
}
System.out.println(min);
System.out.println(max);
}
}
thanks, i learnt how to think from you
ОтветитьThis is what i did (Note: this is not at all an optimal solution )
static void miniMaxSum(int[] arr) {
long max=0,min=0,sum=0; int count=0;
for(int j=0;j<arr.length;j++)
{
for(int i=0;i<arr.length;i++)
{
if(i!=count)
sum+=arr[i];
}
if(j==0) min=sum;
count++;
if(sum>max)max=sum;
if(sum<min)min=sum;
sum=0;
}
System.out.print(min+" "+max);
}
tank you, i am so bad in the problems :,v
ОтветитьNice one, i need to change my way of coding , Thanks
ОтветитьThank you so much
Ответитьhelpful video
Ответитьis it good approach or not?
Ответитьgreat
thnx buddy
nice logic. (no need to initialize min, max, sum to zero)
ОтветитьReally Useful Trick!
ОтветитьThis method does the same job, but still, 11/15 test cases failed :(
static void miniMaxSum(int[] arr) {
int[] sum = new int[arr.length];
for(int i = 0; i < arr.length; i++){
int current = arr[i];
sum[i] = Arrays.stream(arr).filter(s -> s != current).sum();
}
int max = Arrays.stream(sum).max().orElse(0);
int min = Arrays.stream(sum).min().orElse(0);
System.out.print(min+" "+max);
}
Sir may I know what IDE are you using
ОтветитьThis Tut Solved My Problem !! Thanks!
ОтветитьHi , I just followed your link you sent me in Hackerrank . Thank You for this solution :)
Ответитьgud solution ..complexity decreased, great work
ОтветитьHello Coding Lover,
This is the updated version of mini-max sum hackerrank problem tutorial with subtitle with better sound.