Chef and Proportion Codechef Solution: Chef recently learned about ratios and proportions. He wrote some positive integers a, b, c, d on a paper. Chef wants to know whether he can shuffle these numbers so as to make some proportion? Formally, four numbers x, y, z, w are said to make a proportion if ratio of x : y is same as that of z : w.
Input
Only line of the input contains four space separated positive integers – a, b, c, d.
Output
Print “Possible” if it is possible to shuffle a, b, c, d to make proportion, otherwise “Impossible” (without quotes).
Constraints
- 1 ≤ a, b, c, d ≤ 1000
Sample Input 1
1 2 4 2
Sample Output 1
Possible
Explanation
By swapping 4 and the second 2, we get 1 2 2 4. Note that 1 2 2 4 make proportion as 1 : 2 = 2 : 4. Hence answer is “Possible”
Chef and Proportion – CodeChef Solution in JAVA
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) throws java.lang.Exception { try { Scanner sc = new Scanner(System.in); int []a=new int[4]; for(int i=0;i<4;i++){ a[i]=sc.nextInt(); } Arrays.sort(a); if(a[0]*a[3]==a[1]*a[2]) System.out.println("Possible"); else System.out.println("Impossible"); } catch(Exception e) { return; } } }
Chef and Proportion – CodeChef Solution in CPP
#include <iostream> #include<bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { // your code goes here FastIO; int a ,b ,c ,d ; cin >> a >> b >> c >> d; bool flag = (a*b==c*d || a*c==b*d || a*d==b*c ); if(flag) cout <<"Possible"<<endl; else cout <<"Impossible" <<endl; return 0; }
Chef and Proportion -CodeChef Solution in Python
from sys import stdin, stdout rl = lambda : stdin.readline() wl = lambda s: stdout.write(s + "\n") a, b, c, d = map(int, rl().split()) if (a / b == c / d or b / a == c / d or a / c == b / d or c / a == b / d): wl("Possible") else: wl("Impossible")
Disclaimer: The above Problem(Chef and Proportion ) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.