Given a string **str** of size **N** containing two types of character only that are **“I”** or **“D”**. The task is to generate an array **arr[0, 1, . . N]** of size **N + 1 **sattisfing the following conditions:

- If str[i] == “I” then arr[i] < arr[i+1]
- If str[i] == “D” then arr[i] > arr[i+1]

**Examples:**

Input:str = “IDID”Output:0 4 1 3 2Explaination:

str[0] == “I” therfore arr[0] < arr[1]

str[1] == “D” therfore arr[1] > arr[2]

str[2] == “I” therfore arr[2] < arr[3]

str[3] == “D” therfore arr[3] > arr[4]Input:str = “III”Output:0 1 2 3

**Approach:**

- Initialize variable
**START**as**0**and**END**as**N**. - Iterate over the entire array.
- If
**str[i]**==**“I”**then assign**START**at**arr[i]**and Increment the**START**. - If
**str[i]**==**“D”**then assign**END**at**arr[i]**and Decrement the**END**. - Now the last element of the array isn’t assigned so assign
**START**at**A[N]**.

Below is the implementation of the approach.

## C++

`// C++ program to Generate an increasing ` `// and decreasing array ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that returns generated array ` `int` `* DiStirngMatch(string Str) ` `{ ` ` ` `int` `N = Str.length(); ` ` ` ` ` `// Dynamically allocate array ` ` ` `int` `* arr = ` `new` `int` `[N]; ` ` ` ` ` `// START=0, END=N ` ` ` `int` `START = 0, END = N; ` ` ` ` ` `// iterate over array ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// if Str[i]=='I' assign arr[i] ` ` ` `// as START and increment START ` ` ` `if` `(Str[i] == ` `'I'` `) ` ` ` `arr[i] = START++; ` ` ` ` ` `// if str[i]=='D' assign arr[i] ` ` ` `// as END and decrement END ` ` ` `if` `(Str[i] == ` `'D'` `) ` ` ` `arr[i] = END--; ` ` ` `} ` ` ` ` ` `// assign A[N] as START ` ` ` `arr[N] = START; ` ` ` ` ` `// return starting ` ` ` `// address of array A ` ` ` `return` `arr; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `string Str = ` `"IDID"` `; ` ` ` `int` `N = Str.length(); ` ` ` `int` `* ptr = DiStirngMatch(Str); ` ` ` `for` `(` `int` `i = 0; i <= N; i++) ` ` ` `cout << ptr[i] << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to generate an increasing ` `// and decreasing array ` `class` `GFG{ ` ` ` `// Function that returns generated array ` `static` `int` `[]DiStirngMatch(String Str) ` `{ ` ` ` `int` `N = Str.length(); ` ` ` ` ` `// Dynamically allocate array ` ` ` `int` `[]arr = ` `new` `int` `[N + ` `1` `]; ` ` ` ` ` `// START=0, END=N ` ` ` `int` `START = ` `0` `, END = N; ` ` ` ` ` `// Iterate over array ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` ` ` `// if Str[i]=='I' assign arr[i] ` ` ` `// as START and increment START ` ` ` `if` `(Str.charAt(i) == ` `'I'` `) ` ` ` `arr[i] = START++; ` ` ` ` ` `// if str[i]=='D' assign arr[i] ` ` ` `// as END and decrement END ` ` ` `if` `(Str.charAt(i) == ` `'D'` `) ` ` ` `arr[i] = END--; ` ` ` `} ` ` ` ` ` `// Assign A[N] as START ` ` ` `arr[N] = START; ` ` ` ` ` `// Return starting ` ` ` `// address of array A ` ` ` `return` `arr; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String Str = ` `"IDID"` `; ` ` ` `int` `N = Str.length(); ` ` ` `int` `[] ptr = DiStirngMatch(Str); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i <= N; i++) ` ` ` `System.out.print(ptr[i] + ` `" "` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to generate an ` `# increasing and decreasing array ` ` ` `# Function that returns generated array ` `def` `DiStirngMatch(` `Str` `): ` ` ` ` ` `N ` `=` `len` `(` `Str` `) ` ` ` ` ` `# Dynamically allocate array ` ` ` `arr ` `=` `(N ` `+` `1` `) ` `*` `[` `0` `] ` ` ` ` ` `# START, END= 0 ,N ` ` ` `START, END ` `=` `0` `, N ` ` ` ` ` `# Iterate over array ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `# If Str[i]=='I' assign arr[i] ` ` ` `# as START and increment START ` ` ` `if` `(` `Str` `[i] ` `=` `=` `'I'` `): ` ` ` `arr[i] ` `=` `START ` ` ` `START ` `+` `=` `1` ` ` ` ` `# If str[i]=='D' assign arr[i] ` ` ` `# as END and decrement END ` ` ` `if` `(` `Str` `[i] ` `=` `=` `'D'` `): ` ` ` `arr[i] ` `=` `END ` ` ` `END ` `-` `=` `1` ` ` ` ` `# Assign A[N] as START ` ` ` `arr[N] ` `=` `START ` ` ` ` ` `# Return starting ` ` ` `# address of array A ` ` ` `return` `arr ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `Str` `=` `"IDID"` ` ` `N ` `=` `len` `(` `Str` `) ` ` ` `ptr ` `=` `DiStirngMatch(` `Str` `) ` ` ` ` ` `for` `i ` `in` `range` `(N ` `+` `1` `): ` ` ` `print` `(ptr[i], end ` `=` `" "` `) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to generate an increasing ` `// and decreasing array ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that returns generated array ` `static` `int` `[]DiStirngMatch(String Str) ` `{ ` ` ` `int` `N = Str.Length; ` ` ` ` ` `// Dynamically allocate array ` ` ` `int` `[]arr = ` `new` `int` `[N + 1]; ` ` ` ` ` `// START=0, END=N ` ` ` `int` `START = 0, END = N; ` ` ` ` ` `// Iterate over array ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` ` ` `// if Str[i]=='I' assign arr[i] ` ` ` `// as START and increment START ` ` ` `if` `(Str[i] == ` `'I'` `) ` ` ` `arr[i] = START++; ` ` ` ` ` `// if str[i]=='D' assign arr[i] ` ` ` `// as END and decrement END ` ` ` `if` `(Str[i] == ` `'D'` `) ` ` ` `arr[i] = END--; ` ` ` `} ` ` ` ` ` `// Assign A[N] as START ` ` ` `arr[N] = START; ` ` ` ` ` `// Return starting ` ` ` `// address of array A ` ` ` `return` `arr; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String Str = ` `"IDID"` `; ` ` ` `int` `N = Str.Length; ` ` ` `int` `[] ptr = DiStirngMatch(Str); ` ` ` ` ` `for` `(` `int` `i = 0; i <= N; i++) ` ` ` `Console.Write(ptr[i] + ` `" "` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

0 4 1 3 2

**Time Complexity:** O (N)**Auxiliary Space:** O (N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Check if it is possible to make array increasing or decreasing by rotating the array
- Find the maximum element in an array which is first increasing and then decreasing
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Split the array elements into strictly increasing and decreasing sequence
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Divide array into increasing and decreasing subsequence without changing the order
- Check if N and M can be made equal by increasing N by A and decreasing M by B
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Sum of array elements that is first continuously increasing then decreasing
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Check if an array is increasing or decreasing
- Sort an Increasing-Decreasing Array
- Check if Array forms an increasing-decreasing sequence or vice versa
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Count permutations that are first decreasing then increasing.
- Generate all possible sorted arrays from alternate elements of two given sorted arrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.