0% found this document useful (0 votes)
13 views5 pages

DT - Sort Pairs Alternatively

Uploaded by

Siva Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

DT - Sort Pairs Alternatively

Uploaded by

Siva Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

7/8/2019 www.skillrack.com/faces/candidate/dailychallenge.xhtml?

k=DT

 LeaderBoard & Yesterday's Solution(/faces/candidate/leaderboarddailychallenge.xhtml?RT=DAILYTEST)

Daily Test ProgramID- 8904 Solved By 0 Users SkillRack

Time Left: 00:02:39

Sort Pairs Alternatively

The program must accept N pairs of integers as the input. The program must sort the pairs based on the position of each pair in the given input in ascending
order. If the pair is at odd position then consider the rst integer in it to sort. If the pair is in even position then consider the second integer to sort. Then the
program must print the sorted pairs as the output.
Note: If two integers are same, then they must appear in same order as in the given input.

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

Input Format:
The rst line contains N.
The next N lines contain 2 integers each separated by a space.

Output Format:
The rst N lines contain sorted integers as per the given conditions.

Example Input/Output 1:
Input:
5
54
14
32
75
26

Output:
26
32

www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT 1/5
7/8/2019 www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT

14
54
75

Explanation:
The pairs are sorted based on their positions. The values by which each pair is sorted are given below.
54-5
14-4
32-3
75-5
26-2

The values are sorted as below.


26-2
32-3
14-4
54-5
75-5

Example Input/Output 2:
Input:
8
13 14
98
6 28
24 28
23 13
19 25
57
18 30

Output:
57
6 28
98
13 14
23 13
19 25
24 28
18 30

www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT 2/5
7/8/2019 www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT

Max Execution Time Limit: 1000 millisecs

Ambiance

C ( gcc 8.x) 


1 #include<stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int n;
7 scanf("%d ",&n);
8 int a[n],b[n],c[n],d[n];
9 for(int i=0;i<n;i++){
10 scanf("%d %d\n",&a[i],&b[i]);
11 if(i%2==0){
12 c[i]=a[i];
13 d[i]=b[i];
14 }
15 else{
16 c[i]=b[i];
17 d[i]=a[i];
18 }
19 }
20 for(int i=0;i<n;i++){
21 for(int j=i+1;j<n;j++){
22 if(c[j]<c[i]){
23 int temp=c[i];
24 c[i]=c[j];
25 c[j]=temp;
26
27 temp=b[i];
www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT 3/5
7/8/2019 www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT

28 b[i]=b[j];
29 b[j]=temp;
30
31 temp=a[i];
32 a[i]=a[j];
33 a[j]=temp;
34
35 temp=d[i];
36 d[i]=d[j];
37 d[j]=temp;
38 }
39 if(c[i]==c[j]){
40 if(d[j]<d[i]){
41 int temp=d[j];
42 d[j]=d[i];
43 d[i]=temp;
44
45 temp=a[i];
46 a[i]=a[j];
47 a[j]=temp;
48
49 temp=b[i];
50 b[i]=b[j];
51 b[j]=temp;
52 }
53 }
54 }
55 }
56 for(int i=0;i<n;i++){
57 printf("%d %d*\n ",a[i],b[i]);
58 }
59
60 }

Code did not pass the execution 

www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT 4/5
7/8/2019 www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT

Input:

5
54
14
32
75
26

Expected Output:

26
32
14
54
75

Your Program Output:

2 6*
3 2*
1 4*
5 4*
7 5*

Save Run

www.skillrack.com/faces/candidate/dailychallenge.xhtml?k=DT 5/5

You might also like