The program must accept N integers as the input, where each integer contains a pipe symbol. The program must find the sum of the given N integers as S1. Then the program must split each integer in two parts based on the pipe symbol. Then the program must sort the resulting N*2 integers and concatenate every two integers. Then the program must find the sum of N resulting integers as S2. Finally, the program must print the values of S1 and S2 as the output.
Boundary Condition(s):
1 <= N <= 100
10 <= Each integer value <= 10^6
Input Format:
The first line contains N.
The second line contains N integers separated by a space.
Output Format:
The first line contains the values of S1 and S2 separated by a space.
Example Input/Output 1:
Input:
4
12|3 98|54 23|02 8|76
Output:
13155 10887
Explanation:
Here N = 4.
S1 = 123 + 9854 + 2302 + 876 = 13155.
After dividing each integer two parts, the integers become
12 3 98 54 23 2 8 76
After sorting the 8 integers, the integers become
2 3 8 12 23 54 76 98
After merging every two integers, the integers become
23 812 2354 7698
S2 = 23 + 812 + 2354 + 7698 = 10887.
Example Input/Output 2:
Input:
6
3|38 57|00 1|1 5|36 389|9 74|52
Output:
17936 83357
n=int(input())
a=input().split()
b=[]
s=0
for i in a:
t=i.split('|')
t1=t[0]+t[1]
t=[int(_) for _ in t]
s+=int(t1)
b+=t
b=sorted(b)
print(s,end=" ")
c=[]
for i in range(0,2*n,2):
c.append(int(str(b[i])+str(b[i+1])))
print(sum(c))
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] arr = new int[N*2];
int sum1 = 0, sum2 = 0, k = 0;
for(int i=0;i<N;i++){
String s = sc.next();
int l = s.indexOf("|");
String n1 = s.substring(0,l);
String n2 = s.substring(l+1);
sum1 += Integer.parseInt(n1+""+n2);
arr[k++] = Integer.parseInt(n1);
arr[k++] = Integer.parseInt(n2);
}
Arrays.sort(arr);
for(int i=0;i<N*2;i+=2){
sum2 += Integer.parseInt(arr[i]+""+arr[i+1]);
}
System.out.println(sum1+" "+sum2);
}
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,s1=0,s2=0;
scanf("%d",&n);
int arr[n*2];
char temp[101],a[101],b[101];
for(int i=0;i<n;i++)
{
scanf("%[^|]| %s",a,b);
arr[i*2]=atoi(a);
arr[(i*2)+ 1]=atoi(b);
sprintf(temp,"%s%s",a,b);
s1+=atoi(temp);
}
for(int i=0;i<(n*2);i++)
{
for(int j=i+ 1;j<(n*2);j++)
{
if(arr[i]>arr[j])
{
s2=arr[i]; arr[i]=arr[j]; arr[j]=s2;
}
}
}
s2=0;
for(int i=0;i<n;i++)
{
sprintf(temp,"%d%d",arr[(i*2)],arr[(i*2)+ 1]);
s2+=atoi(temp);
}
printf("%d %d",s1,s2);
}
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int n,sum=0;
cin>>n;
int arr[n*2],ai=0,num,a;
string s;
while(n--){
cin>>s;
num=0,a=0;
for(int i=0;i<s.length();i++){
if(s[i]=='|'){
arr[ai++]=a;
a=0;
}else{
num=(num*10)+(s[i]-48);
a=(a*10)+(s[i]-48);
}
}
arr[ai++]=a;
sum+=num;
}
cout<<sum<<" ";
sort(arr,arr+ai);
sum=0;
for(int i=0;i<ai;i+=2){
sum+=stoi(to_string(arr[i])+to_string(arr[i+1]));
}
cout<<sum;
}
Leave a Reply