
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
Find Perfect Array of Size N Whose Subarray is a Good Array in C++
Suppose we have a number n. An array B is good if the sum of its elements is divisible by the length of this array. We can say an array A with n elements is perfect, if non-empty subarray of this array A is good and elements in A is in range 1 to 100. From the number n, we have to find an array A which is perfect.
So, if the input is like n = 4, then the output will be [7, 37, 79, 49], other answers are also possible.
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < n, update (increase i by 1), do: print 1
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; void solve(int n){ for(int i=0;i<n;i++){ cout<<"1"<<", "; } } int main(){ int n = 4; solve(n); }
Input
4
Output
1, 1, 1, 1,
Advertisements