
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert to Strictly Increasing Integer Array with Minimum Changes in C++
In this tutorial, we will be discussing a program to convert to strictly increasing integer array with minimum changes.
For this we will be provided with an array. Our task is to change the elements of the array to be in strictly increasing order by minimum number of changes in the elements.
Example
#include <bits/stdc++.h> using namespace std; //calculating number of changes required int remove_min(int arr[], int n){ int LIS[n], len = 0; for (int i = 0; i < n; i++) LIS[i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j])){ LIS[i] = max(LIS[i], LIS[j] + 1); } } len = max(len, LIS[i]); } //returning the changes required return n - len; } int main(){ int arr[] = { 1, 2, 6, 5, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << remove_min(arr, n); return 0; }
Output
2
Advertisements