Sort Pairs – Inside & Outside Parentheses

The program must accept N pairs of integers as the input. One of the two integers in each pair is enclosed within a pair of parentheses. The program must sort the integers that present inside the parentheses in ascending order and sort the integers that present outside the parentheses in descending order. Finally, the program must print N revised pairs as the output.

Boundary Condition(s):
2 <= N <= 100
1 <= Each integer value <= 10^5

Input Format:
The first line contains N.
The second line contains N pairs of integers separated by a space.

Output Format:
The first line contains the revised N pairs of integers separated by a space.

Example Input/Output 1:
Input:
4
12(52) (25)50 (35)10 44(60)

Output:
50(25) (35)44 (52)12 10(60)

Explanation:
Here N = 4.
The 4 integers that present inside the parentheses are 522535 and 60.
The 4 integers that present outside the parentheses are 125010 and 44.
After sorting those integers in the pairs based on the given conditions, the pairs become
50(25) (35)44 (52)12 10(60)

Example Input/Output 2:
Input:
3
626(564) (343)752 (179)99

Output:
752(179) (343)626 (564)99

n=int(input())
l=input().split()
m=[]
a=[]
b=[]
for i in l:
    k=i.split('(')
    if k[0]=='':
        m.append(0)
        k=k[1].split(')')
        a.append(int(k[1]))
        b.append(int(k[0]))
    else:
        m.append(1)
        a.append(int(k[0]))
        k=k[1].split(')')
        b.append(int(k[0]))
a.sort(reverse=True)
b.sort()
for i in range(n):
    if m[i]==0:
        print('(',b[i],')',a[i],sep="",end=" ")
    else:
        print(a[i],'(',b[i],')',sep="",end=" ")
import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] s = new String[n];
        for (int i = 0; i < n; i++) {
            s[i] = sc.next();

        }
        ArrayList < Integer > l1 = new ArrayList < > (n);
        ArrayList < Integer > l2 = new ArrayList < > (n);
        for (int i = 0; i < n; i++) {
            String s1, s2;
            int e = s[i].indexOf(')');
            if (e == s[i].length() - 1) {
                s1 = s[i].substring(s[i].indexOf('(') + 1, e);
                s2 = s[i].substring(0, s[i].indexOf('('));

            } else {
                s1 = s[i].substring(s[i].indexOf('(') + 1, e);
                s2 = s[i].substring(e + 1);
            }

            int t1 = Integer.parseInt(s1), t2 = Integer.parseInt(s2);
            l1.add(t1);
            l2.add(t2);

        }
        Collections.sort(l1);
        Collections.sort(l2);

        for (int i = 0; i < n; i++) {
            int t = s[i].indexOf(')');
            if (t == s[i].length() - 1) {
                System.out.print(l2.get(n - 1 - i) + "(" + l1.get(i) + ") ");
            } else {
                System.out.print("(" + l1.get(i) + ")" + l2.get(n - 1 - i) + " ");
            }
        }
    }
}
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n; scanf("%dn",&n);
    char s[n][1001]; int a[1001],b[1001],x=0,y=0;
    for(int i=0;i<n;i++) scanf("%s ",s[i]);
    for(int i=0;i<n;i++){
        int num=0,num1=0;
        for(int j=0;j<strlen(s[i]);j++){
            if(s[i][j]=='('){
                for(int k=j+1;k<strlen(s[i]);k++,j++){
                     if(s[i][k]!=')'){num1=num1*10+(s[i][k]-'0');}
                     else{ b[y++]=num1;break;}
                }
            }
            else if(s[i][j]!=')'){
                num=num*10+(s[i][j]-'0');
            }
        }
        a[x++]=num;

    }
    for(int i=0;i<x;i++){
        for(int j=i+1;j<x;j++){
            if(a[i]<a[j]){ int t=a[i]; a[i]=a[j]; a[j]=t;}
            if(b[i]>b[j]){ int t=b[i]; b[i]=b[j]; b[j]=t;}
        }
    }
    int count=0;
    for(int i=0;i<n;i++){
        int flag1=0,flag=0;
        for(int j=0;j<strlen(s[i]);j++){
            if(s[i][j]=='('){
                if(flag1!=1){ printf("(%d)",b[count]);flag1=1;}
            }
            else if(s[i][j]!=')'){
                if(flag!=1){printf("%d",a[count]);flag=1;}
            }

        }
        printf(" ");
        count++;
    }
}
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char ** argv) {
    int a;
    cin >> a;
    int brac[1001], emp[10001], ce[1001], b1 = 0, b2 = 0, c = 0;
    while (a > 0) {
        string s;
        cin >> s;
        if (s[0] == '(' || (s[0] == ' ' && s[1] == '(')) {
            ce[c++] = 1;
        } else {
            ce[c++] = 0;
        }
        int num = 0, num2 = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s[i] == '(') {
                i++;
                while (s[i] != ')') {
                    num = num * 10 + (s[i] - '0');
                    i++;
                }
            }
            if (isdigit(s[i])) {
                num2 = num2 * 10 + (s[i] - '0');
            }
        }
        brac[b1++] = num;
        emp[b2++] = num2;
        a = a - 1;
    }
    sort(brac, brac + b1);
    sort(emp, emp + b2, greater < int > ());
    for (int i = 0; i < c; i++) {
        if (ce[i] == 1) {
            cout << "(" << brac[i] << ")" << emp[i] << " ";
        } else {
            cout << emp[i] << "(" << brac[i] << ")" << " ";
        }
    }
}
Previous Article

String - Non Repeated Characters

Next Article

Function mergeEveryTwoIntegers

Write a Comment

Leave a Comment

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