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,914 questions

51,847 answers

573 users

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

2 Answers

0 votes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class Main {
    public static boolean isListSortedAndValidGap(List<Integer> list) {
        if (list.size() < 2) return true; 
         
        int size = list.size();
 
        // Check ascending order
        boolean isAscending = true;
        for (int i = 1; i < size; ++i) {
            if (list.get(i) < list.get(i - 1) || Math.abs(list.get(i) - list.get(i - 1)) < 1 || Math.abs(list.get(i) - list.get(i - 1)) > 3) {
                isAscending = false;
                break;
            }
        }
         
        // Check descending order
        boolean isDescending = true;
        if (!isAscending) {
            for (int i = 1; i < size; ++i) {
                if (list.get(i) > list.get(i - 1) || Math.abs(list.get(i) - list.get(i - 1)) < 1 || Math.abs(list.get(i) - list.get(i - 1)) > 3) {
                    isDescending = false;
                    break;
                }
            }
        }
         
        return isAscending || isDescending;
    }
 
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1, 2, 3, 5, 8, 11, 14, 15);
 
        if (isListSortedAndValidGap(list1)) {
            System.out.println("List is sorted and valid gaps");
        } else {
            System.out.println("List is not sorted or gaps are invalid");
        }
         
        List<Integer> list2 = Arrays.asList(15, 14, 11, 8, 5, 3, 2, 1);
 
        if (isListSortedAndValidGap(list2)) {
            System.out.println("List is sorted and valid gaps");
        } else {
            System.out.println("List is not sorted or gaps are invalid");
        }
    }
}
 
 
 
/*
run:
  
List is sorted and valid gaps
List is sorted and valid gaps
  
*/

 



answered Jan 12, 2025 by avibootz
edited Jan 12, 2025 by avibootz
0 votes
import java.util.List;
 
public class Main {
 
    public static boolean isListSortedAndValidGap(List<Integer> list) {
        if (list == null || list.size() < 2) {
            return false; 
        }
 
        boolean isIncreasing = list.get(1) > list.get(0);
        for (int i = 1; i < list.size(); i++) {
            int gap = Math.abs(list.get(i) - list.get(i - 1));
            if (gap < 1 || gap > 3) {
                return false; // Gap is not within the allowed range
            }
            if (isIncreasing && list.get(i) <= list.get(i - 1)) {
                return false; // List is not strictly increasing
            }
            if (!isIncreasing && list.get(i) >= list.get(i - 1)) {
                return false; // List is not strictly decreasing
            }
        }
        
        return true;
    }
 
    public static void main(String[] args) {
        List<Integer> list1 = List.of(1, 2, 3, 5, 8, 11, 14, 15);
        List<Integer> list2 = List.of(15, 14, 11, 8, 5, 3, 2, 1);
        List<Integer> list3 = List.of(15, 14, 11, 8, 5, 300, 2, 1);
        
        System.out.println(isListSortedAndValidGap(list1));
        System.out.println(isListSortedAndValidGap(list2));
        System.out.println(isListSortedAndValidGap(list3));
    }
}
 
 
 
/*
run:
  
true
true
false
  
*/

 



answered Jan 12, 2025 by avibootz
edited Jan 12, 2025 by avibootz
...