Header Ad

Codechef Max minus Min problem solution

In this Codechef Max minus Min problem solution Chef is given 33 integers A, B,A,B, and CC such that A \lt B \lt CA<B<C.

Chef needs to find the value of max(A, B, C) - min(A, B, C)max(A,B,C)−min(A,B,C).

Here max(A, B, C)max(A,B,C) denotes the maximum value among A, B, CA,B,C while min(A, B, C)min(A,B,C) denotes the minimum value among A, B, CA,B,C.

Codechef Max minus Min problem solution


Problem solution in Python programming.

# cook your dish here
t=int(input())
while t :
    lst=list(map(int,input().split()))
    print(max(lst)-min(lst))
    t=t-1

Problem solution in Java programming.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		int T=sc.nextInt();
		for (int Z=1;Z<=T;Z++){
		    int A=sc.nextInt();
		    int B=sc.nextInt();
		    int C=sc.nextInt();
		    int array[]=new int[3];
		    array[0]=A;
		    array[1]=B;
		    array[2]=C;
		    for (int i=0;i<array.length;i++){
		        for(int j=i+1;j<array.length;j++){
		            if(array[i]>array[j]){
		                int temp=array[i];
		                array[i]=array[j];
		                array[j]=temp;
		            }
		        }
		    }
		  System.out.println(array[array.length-1]-array[0]);
		}
	}
}

Problem solution in C++ programming.

#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
	{
	    int x,y,z;
	    cin>>x>>y>>z;
	    int min;
	    int max;
	    if(x>y && x>z)
	    {
	        max=x;
	    }
	    else if(y>x && y>z)
	    {
	        max=y;
	    }
	    else
	    {
	        max=z;
	    }
	    if(x<y && x<z)
	    {
	        min=x;
	    }
	    else if(y<x && y<z)
	    {
	        min=y;
	    }
	    else
	    {
	        min=z;
	    }
	    cout<<max-min<<endl;
	}
	return 0;
}

Problem solution in C programming.

#include <stdio.h>

int main(void) {
	// your code goes here
	int t;
	scanf("%d",&t);
	while(t--)
	{
	    int x,y,z;
	    scanf("%d %d %d",&x,&y,&z);
	    printf("%d\n",z-x);
	}
	return 0;
}

Post a Comment

0 Comments