Skip to content

Commit 7845db0

Browse files
committed
added questions
1 parent 3d74db9 commit 7845db0

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Aahad gives an array of integers and asks Harshit to find which three elements form a triangle (non-degenerate). The task seems easy to Harshit.
3+
So, Aahad adds some conditions to this task -
4+
1. Find the triangle with maximum perimeter
5+
2. If there are two or more combinations with same value of maximum perimeter, then find the one with the longest side.
6+
3.If there are more than one combinations which satisfy all the above conditions the find with maximum longest minimum side.
7+
Input Format
8+
The First line contains no of elements of array: N
9+
Each T lines contains N space-separated integers: A [i]
10+
Output Format
11+
The output contains three space-separated elements that denote the length of the sides of triangle. If no such triangle is possible, then print -1.
12+
Constraints
13+
1 =< N <= 10^5
14+
1 <= A[i] <= 10^9
15+
Time Limit: 1 second
16+
Sample Input1:
17+
5
18+
1 1 1 3 3
19+
Sample Output1:
20+
1 3 3
21+
Sample Input2:
22+
3
23+
2 2 4
24+
Sample Output3:
25+
-1
26+
Explaination
27+
In the First Sample case, the elements that form a triangle with maximum perimeter is 1,3,3.
28+
In the Second Sample case, the elements that can form a triangle are degenerate, so, we printed -1.
29+
*/
30+
#include<bits/stdc++.h>
31+
using namespace std;
32+
int main() {
33+
// Write your code here
34+
int n;
35+
cin>>n;
36+
int arr[n];
37+
for(int i=0;i<n;i++){
38+
cin>>arr[i];
39+
}
40+
sort(arr,arr+n);
41+
reverse(arr,arr+n);
42+
bool flag = false;
43+
int index = 0;
44+
for(int i=0;i<n-2;i++){
45+
if(arr[i+2]+arr[i+1]>arr[i]){
46+
flag = true;
47+
index = i;
48+
break;
49+
}
50+
}
51+
if(flag){
52+
cout<<arr[index+2]<<" "<<arr[index+1]<<" "<<arr[index]<<endl;
53+
}
54+
else{
55+
cout << -1;
56+
}
57+
}

greedy_problems/weighted_job_scheduling.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
You are given N jobs where every job is represented as:
3+
1.Start Time
4+
2.Finish Time
5+
3.Profit Associated
6+
Find the maximum profit subset of jobs such that no two jobs in the subset overlap.
7+
Input
8+
The first line of input contains one integer denoting N.
9+
Next N lines contains three space separated integers denoting the start time, finish time and the profit associated with the ith job.
10+
Output
11+
Output one integer, the maximum profit that can be achieved.
12+
Constraints
13+
1 ≤ N ≤ 10^6
14+
1 ≤ ai, di, p ≤ 10^6
15+
Sample Input
16+
4
17+
3 10 20
18+
1 2 50
19+
6 19 100
20+
2 100 200
21+
Sample Output
22+
250
23+
*/
124
#include <iostream>
225
#include <algorithm>
326
using namespace std;

0 commit comments

Comments
 (0)