Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Consecutively Descending Integers Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Consecutively Descending Integers

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string containing digits, determine if the string can be broken into consecutively descending integers.

Approach

We will try to make a number with all the possible lengths of prefixes, and then use that number as the starting point of the descending series to check if it is possible to make a consecutively descending series starting with that number. If any of the prefixes becomes a possible valid series, then return true, else return false.

Analysis

  • Time Complexity: O(2n)
  • Space Complexity: O(2n)

Implementation

C++

vector<int> possibleList;
bool isPossible;
void solve(string c,int start,int n){
	if(start == n) {
		for(int i = 1; i < possibleList.size(); i++) {
			if(possibleList[i - 1] - 1 == possibleList[i]) {
				continue;
			}
			isPossible = false;
			return;
		}
		if(possibleList.size() != 1) {
			isPossible = true;
		}
		return;
	}

	int num = 0;
	for(int i = start; i < n; i++){
		num = num * 10 + (c[i] - '0');
		possibleList.push_back(num);
		solve(c, i + 1, n);
		if(isPossible){
			return;
		}
		possibleList.pop_back();
	}
}
bool isConsecutivelyDescending(string s) {
	possibleList.clear();
	isPossible = false;
	solve(s, 0, s.length());
	return isPossible;
}

Java

class Solution {
	ArrayList<Integer> possibleList;
	boolean isPossible;
	void solve(char c[],int start,int n){
		if(start == n) {
			for(int i = 1; i < possibleList.size(); i++) {
				if(possibleList.get(i - 1) - 1 == possibleList.get(i)) {
					continue;
				}
				isPossible = false;
				return;
			}
			if(possibleList.size() != 1) {
				isPossible = true;
			}
			return;
		}
		
		int num = 0;
		for(int i = start; i < n; i++){
			num = num * 10 + (c[i] - '0');
			possibleList.add(num);
			solve(c, i + 1, n);
			if(isPossible == true){
				return;
			}
			possibleList.remove(possibleList.size() - 1);
		}
	}
	boolean isConsecutivelyDescending(String s) {
		possibleList = new ArrayList<>();
		isPossible = false;
		solve(s.toCharArray(), 0, s.length());
		return isPossible;
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Generate Parentheses
Kth Permutation Sequence
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet