PROBLEM LINK:
Author: Rahil Parikh Kashvi Mody
Tester: Rohit Tawde
Editorialist: Rohit Tawde
DIFFICULTY:
Simple.
PREREQUISITES:
None.
PROBLEM:
Given Y coordinates of N points on a 2D plane, tell if you can set their X coordinates (without changing the order) such that all points fall on a straight line.
EXPLANATION:
The points lie on the same line if the list of Y coordinates is strictly increasing/is strictly decreasing/contains same elements.
TIME COMPLEXITY:
O(N).
SOLUTIONS:
Setter's Solution
for test_cases in range(int(input())):
list1 = []
n = int(input())
list1 = list(map(int, input().split(" ")))[:n]
strictly_increasing = all(i < j for i, j in zip(list1, list1[1:]))
strictly_decreasing = all(i > j for i, j in zip(list1, list1[1:]))
constant_array = all(i == j for i, j in zip(list1, list1[1:]))
if(strictly_increasing == True):
print("YES")
elif(strictly_decreasing == True):
print("YES")
elif(constant_array == True):
print("YES")
else:
print("NO")
Tester's Solution
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int main() {
fast_io;
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
vector<ll>a(n);
for(int i =0; i <n;i++){
cin>>a[i];
}
//check strictly increasing
bool flag =0;
for(int i =1;i<n;i++){
if(a[i-1]>=a[i]){
flag=1;
}
}
if(flag==0){
cout<<"YES"<<endl;
continue;
}
//check strictly decreasing
flag =0;
for(int i =1;i<n;i++){
if(a[i-1]<=a[i]){
flag=1;
}
}
if(flag==0){
cout<<"YES"<<endl;
continue;
}
//check if all elements are same.
flag =0;
for(int i =1;i<n;i++){
if(a[i-1]!=a[i]){
flag=1;
}
}
if(flag==0){
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
}
return 0;
}