New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Check if given Array is Monotonic

by admin
3 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


Given an array arr[] containing N integers, the duty is to examine whether or not the array is monotonic or not (monotonic means both the array is in growing order or in reducing order).

Examples:

Enter: arr[] = {1, 2, 2, 3}
Output: Sure
Rationalization: Right here 1 < 2 <= 2 < 3. 
The array is in growing order. Due to this fact it’s monotonic.

Enter: arr[] =  {6, 5, 4, 3}
Output: Sure
Rationalization: Right here 6 > 5 > 4 > 3. 
The array is in reducing order. So it’s monotonic.

Enter: arr[] = {1, 5, 2}
Output: No
Rationalization: Right here 1 < 5 > 2. The array is neither growing nor reducing. 
So the array just isn’t monotonic

 

Strategy: The issue will be solved by checking if the array is in growing order or in reducing order. This may be simply accomplished within the following manner:

  • If for every i in vary [0, N-2], arr[i] ≥  arr[i+1] the array is in reducing order.
  • If for every i in vary [0, N-2], arr[i] ≤ arr[i+1], the array is in growing order.

Comply with the under steps to unravel the issue:

  • Traverse the array arr[] from i = 0 to N-2 and examine if the array is growing so as
  • Traverse the array arr[] from i = 0 to N-2 and examine if the array is reducing so as
  • If neither of the above two is true, then the array just isn’t monotonic.

Under is the implementation of the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

bool examine(vector<int>& arr)

{

    int N = arr.measurement();

    bool inc = true;

    bool dec = true;

  

    

    for (int i = 0; i < N - 1; i++) {

  

        

        

        if (arr[i] > arr[i + 1]) {

            inc = false;

        }

    }

  

    

    for (int i = 0; i < N - 1; i++) {

  

        

        

        if (arr[i] < arr[i + 1]) {

            dec = false;

        }

    }

  

    

    return inc || dec;

}

  

int predominant()

{

    vector<int> arr = { 1, 2, 3, 3 };

  

    

    bool ans = examine(arr);

    if (ans)

        cout << "Sure";

    else

        cout << "No";

    return 0;

}

Time Complexity: O(N)
Auxiliary House: O(1)



Source link

Tags: arrayCheckMonotonic
Previous Post

Indulge in walking to pursue a healthy lifestyle – Noise

Next Post

The Beauty Benefits Of Iron For Skin, Hair, And Nails

Related Posts

Applications of Artificial Intelligence in Business
Softwares

Applications of Artificial Intelligence in Business

by admin
June 4, 2025
How to Add Custom Style Variations to WordPress Blocks — Speckyboy
Softwares

How to Add Custom Style Variations to WordPress Blocks — Speckyboy

by admin
June 2, 2025
Smart software replaces expensive sensors for glass wall detection with 96% accuracy
Softwares

Smart software replaces expensive sensors for glass wall detection with 96% accuracy

by admin
June 1, 2025
User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 2025
Next Post
The Beauty Benefits Of Iron For Skin, Hair, And Nails

The Beauty Benefits Of Iron For Skin, Hair, And Nails

Prince’s 1985 Syracuse Purple Rain Show Will Finally be Released

Prince's 1985 Syracuse Purple Rain Show Will Finally be Released

  • Trending
  • Comments
  • Latest
How to Build a JavaScript Search [Article]

How to Build a JavaScript Search [Article]

August 30, 2022
Product Information Management Trends (PIM)

Product Information Management Trends (PIM)

February 4, 2022
Advancement in predicting software vulnerabilities

Advancement in predicting software vulnerabilities

May 21, 2022
Neal Schon Teases Potential Return of Co-Founding Journey Member

Neal Schon Teases Potential Return of Co-Founding Journey Member

November 28, 2022
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
15 Best Movies Like Parasite

15 Best Movies Like Parasite

February 20, 2022
Every Kathryn Hahn Film Performance, Ranked

Every Kathryn Hahn Film Performance, Ranked

December 24, 2022
How to Build a DIY Spotify Music Player with Raspberry Pi Pico

How to Build a DIY Spotify Music Player with Raspberry Pi Pico

May 13, 2025
With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’

With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’

June 5, 2025
Has The Survivors Season 2 Been Canceled or Renewed?

Has The Survivors Season 2 Been Canceled or Renewed?

June 5, 2025
Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ

Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ

June 5, 2025
Rogue Traders singer Natalie Bassingthwaighte celebrates second anniversary with partner Pip Loth

Rogue Traders singer Natalie Bassingthwaighte celebrates second anniversary with partner Pip Loth

June 5, 2025
X Adds Verification Explainers To Avoid EU Fines

X Adds Verification Explainers To Avoid EU Fines

June 5, 2025
Leah Remini Addresses Rumors of a Rift with Jennifer Lopez

Leah Remini Addresses Rumors of a Rift with Jennifer Lopez

June 5, 2025
Eminem’s Publishing Company Battles Meta in $109 Million Lawsuit

Eminem’s Publishing Company Battles Meta in $109 Million Lawsuit

June 4, 2025
TESmart thunderbolt KVM switch review – great as long as you’re fine with 4k60FPS or 2K screens

TESmart thunderbolt KVM switch review – great as long as you’re fine with 4k60FPS or 2K screens

June 4, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • With Their New Single, 2ŁØT and Aloe Blacc Inspire Us to Become ‘Immortal’
  • Has The Survivors Season 2 Been Canceled or Renewed?
  • Turning Disaster Into Relief: The Purpose-Driven Growth of Insurance Claim HQ
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life