MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / solve

Function solve

Codeforces_problems/Cubes Sorting1420A/Solution.cpp:16–42  ·  view source on GitHub ↗

The main clue of this question is the equation (n*(n-1))/2 -1. When we sort an array by swapping elements, the Worst case happens when the array is strictly decreasing, because the whole array need to be reversed. To reverse an array of n elements the number of operations needed is (n*(n-1))/2. So if the array is not strictly decreasing, then it is possible possible to sort in less than (n*(n-1

Source from the content-addressed store, hash-verified

14 decreasing or not.
15 */
16void solve()
17{
18 int n,flag=0;
19 cin>>n;
20 int arr[n];
21 for(int i=0;i<n;i++)
22 {
23 cin>>arr[i];
24 }
25 for(int j=n-1;j>0;j--)
26 {
27 if(arr[j]>=arr[j-1])// checking every element whether it is greater than or equal to its previous element. If yes, it is not strictly decreasing.
28 {
29 flag=1;
30 break;
31 }
32 }
33 if(flag==1)
34 {
35 cout<<"YES\n";
36 }
37 else
38 {
39 cout<<"NO\n";
40 }
41
42 }
43
44int main(){
45 int t;

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected