Home > Backend Development > C++ > body text

C++ program: find array after removing maximum value C++ program to find array after removing maximum value

WBOY
Release: 2023-09-04 20:01:02
forward
598 people have browsed it

C++程序:在删除最大值后找到数组

C++程序用于在删除最大值后找到数组

Suppose we have an array A containing n elements and another value k. We want to perform k operations. An operation has the form:

  • Assume d is the maximum value of the array

  • For each index i from 1 to n, add A Replace [i] with d - A[i]

We need to find the final sequence.

Question Category

An array in a data structure is a finite collection of elements of a specific type. Arrays are used as Store elements of the same type in consecutive memory locations. An array is allocated A specific name and referred to by that name in various programming languages To access an element of an array, an index is required. We use the term "name[i]" to Access a specific element at position 'i' in array 'name'. various data structures For example, stacks, queues, heaps, and priority queues can all be implemented using arrays. Operates on Arrays include insert, delete, update, traverse, search, and sort operations. access Links below are for further reading.

https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm

So, if the input to our problem is A = [5, -1, 4, 2, 0]; k = 19., then the output will be [0, 6, 1, 3, 5] because d is 5.

Steps

To solve this problem, we will follow the following steps:

n := size of A
m := -inf
t := -inf
for initialize i := 0, when i < n, update (increase i by 1), do:
   m := maximum of m and A[i]
for initialize i := 0, when i < n, update (increase i by 1), do:
   A[i] := m - A[i]
   t := maximum of t and A[i]
if k mod 2 is same as 1, then:
   for initialize i := 0, when i < n, update (increase i by 1), do:
      print A[i]
   Otherwise
      for initialize i := 0, when i < n, update (increase i by 1), do:
         A[i] := t - A[i]
         print A[i]
Copy after login

Example

Let us look at the following implementation for better understanding Understand −

#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> A, int k){
   int n = A.size();
   int m = -999;
   int t = -999;
   for (int i = 0; i < n; i++)
      m = max(m, A[i]);
   for (int i = 0; i < n; i++)
      A[i] = m - A[i], t = max(t, A[i]);
   if (k % 2 == 1)
      for (int i = 0; i < n; i++)
         cout << A[i] << ", ";
   else
      for (int i = 0; i < n; i++)
         A[i] = t - A[i], cout << A[i] << ", ";
}
int main(){
   vector<int> A = { 5, -1, 4, 2, 0 };
   int k = 19;
   solve(A, k);
}
Copy after login

Input

{ 5, -1, 4, 2, 0 }, 19
Copy after login

Output

0, 6, 1, 3, 5,
Copy after login

The above is the detailed content of C++ program: find array after removing maximum value C++ program to find array after removing maximum value. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template