[Solved] Bob’s Challenge with Java, C++, Python

Bob’s Challenge: Stella and friends have set out on a vacation to Manali. They have booked accommodation in a resort and the resort authorities headed by Bob, organize Campfires every night as a part of their daily activities. Stella volunteered herself for an activity called the “Stick Game”.

Stella was given a total of N sticks. The length of i-th stick is Ai. Bob insists Stella choose any four sticks and make a rectangle with those sticks as its sides. Bob warns Stella not to break any of the sticks, she has to use sticks as a whole.
 
Also, Bob wants that the rectangle formed should have the maximum possible area among all the rectangles that Stella can make. Stella takes this challenge up and overcomes it. You have to help her know whether it is even possible to create a rectangle. If yes, then tell the maximum possible area of the rectangle.
 
Input Format:
The first line of the input contains a single integer N denoting the number of sticks.
The second line of each test case contains N space-separated integers A1A2, …,AN denoting the lengths of sticks.

Output Format:
Output a single line containing an integer representing the maximum possible area for rectangle or output -1, if it’s impossible to form any rectangle using the available sticks.
Refer sample input and output for formatting specifications.

Sample Input 1:
5
1 2 3 1 2

Sample Output 1:
2

Sample Input 2:
4
1 2 2 3

Sample Output 2:
-1

Solution

import java.util.*;
class Main
{
    public static int zerofun(int a[],int n)

{

    int i,j,k=2;

    for(i=0;i<n;i++)

    {

        for(j=0;j<n;j++)

        {
            //sauravhathi
            if(a[i]==a[j]){k--;}

            if(k==0){break;}

            if(j==n-1){a[i]=0;}

        }

        k=2;

    }

    k=a[0];

    for(i=0;i<n;i++){if(a[i]>k){k=a[i];}}

    j=0;

    for(i=0;j<2;i++){if(k==a[i]){a[i]=0;j++;}}

    return k;

}


    public static void main(String jr[])
    {  int n,i,k=2,l=0;

    Scanner sc = new Scanner(System.in);

    n = sc.nextInt();

    int a[] = new int[n];

    for(i=0;i<n;i++)

    {

        a[i] = sc.nextInt();

    }

    k=zerofun(a,n);

    l=zerofun(a,n);

    if(l*k == 0) {

     System.out.println("-1");

    }

    else {

     System.out.println(l*k);

    }
    
}
}
#include <bits/stdc++.h>
#define li long int
#define lli long long int
#define pmod 1000000007
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
        int n, a;
        //sauravhathi
        cin >> n;
        map<int, int, greater<int>> m;
        while (n--)
        {
            cin >> a;
            m[a]++;
        }
        //sauravhathi
        int ans = 1, c = 0;
        for (auto &p : m)
        {
            if (p.second > 1)
            {
                //sauravhathi
                ans = ans * pow(p.first, min(2 - c, p.second / 2));
                c += min(2 - c, p.second / 2);
            }
            if (c >= 2)
                break;
        }
        if (c >= 2)
        //sauravhathi
            cout << ans << endl;
        else
            cout << -1 << endl;
    return 0;
}
n = int(input())
l = list(map(int, input().split()))
r = []
for i in l:
    c = l.count(i)
    if c > 1:
        c1 = c//2
        r.extend([i]*c1)
        #sauravhathi
        while i in l:
            l.remove(i)
r.sort()
#sauravhathi
if len(r) >= 2:
    print(r[-2]*r[-1])
else:
    print("-1")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *