Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to check if a vector is all increasing or decreasing and the gap between numbers is 1, 2 or 3 in C++

2 Answers

0 votes
#include <iostream>
#include <vector>
#include <cmath>
 
bool isArraySortedAndValidGap(const std::vector<int>& vec) {
    if (vec.size() < 2) return true; 
     
    int size = vec.size();
 
    // Check ascending order
    bool isAscending = true;
    for (size_t i = 1; i < size; ++i) {
        if (vec[i] < vec[i-1] || std::abs(vec[i] - vec[i-1]) < 1 || std::abs(vec[i] - vec[i-1]) > 3) {
            isAscending = false;
            break;
        }
    }
     
    // Check descending order
    bool isDescending = true;
    if (!isAscending) {
        for (size_t i = 1; i < size; ++i) {
            if (vec[i] > vec[i-1] || std::abs(vec[i] - vec[i-1]) < 1 || std::abs(vec[i] - vec[i-1]) > 3) {
                isDescending = false;
                break;
            }
        }
    }
     
    return isAscending || isDescending;
}
 
int main() {
    std::vector<int> vec1 = {1, 2, 3, 5, 8, 11, 14, 15}; 
     
    if (isArraySortedAndValidGap(vec1)) {
        std::cout << "vector is sorted and valid gaps" << std::endl;
    } else {
        std::cout << "vector is not sorted or gaps are invalid" << std::endl;
    }
     
    std::vector<int> vec2 = {15, 14, 11, 8, 5, 3, 2, 1}; 
     
    if (isArraySortedAndValidGap(vec2)) {
        std::cout << "vector is sorted and valid gaps" << std::endl;
    } else {
        std::cout << "vector is not sorted or gaps are invalid" << std::endl;
    }
}
 
 
     
/*
run:
      
vector is sorted and valid gaps
vector is sorted and valid gaps
    
*/

 



answered Jan 11, 2025 by avibootz
edited Jan 12, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>

bool isArraySortedAndValidGap(const std::vector<int>& arr) {
    if (arr.size() < 2) return true; 

    bool increasing = arr[1] > arr[0];
    for (size_t i = 1; i < arr.size(); ++i) {
        int diff = arr[i] - arr[i - 1];
        if (diff != 1 && diff != 2 && diff != 3 && diff != -1 && diff != -2 && diff != -3) {
            return false;
        }
        if ((increasing && diff <= 0) || (!increasing && diff >= 0)) {
            return false;
        }
    }
    return true;
}

int main() {
    std::vector<int> vec1 = {1, 2, 3, 5, 8, 11, 14, 15}; 
    
    if (isArraySortedAndValidGap(vec1)) {
        std::cout << "vector is sorted and valid gaps" << std::endl;
    } else {
        std::cout << "vector is not sorted or gaps are invalid" << std::endl;
    }
    
    std::vector<int> vec2 = {15, 14, 11, 8, 5, 3, 2, 1}; 
    
    if (isArraySortedAndValidGap(vec2)) {
        std::cout << "vector is sorted and valid gaps" << std::endl;
    } else {
        std::cout << "vector is not sorted or gaps are invalid" << std::endl;
    }
}


    
/*
run:
     
vector is sorted and valid gaps
vector is sorted and valid gaps
   
*/

 



answered Jan 12, 2025 by avibootz
...