August Circuits '21 Non-decreasing arrays Solution
You are given an array consisting of positive integers. Your task is to find an array of length satisfying the following conditions:
- for all
- , for all
- is divisible by for all
- is minimum
You are given test cases.
Input format
- The first line contains a single integer denoting the number of test cases.
- The first line of each test case contains a single integer denoting the length of the array.
- The second line of each test case contains space-separated integers denoting the integer array .
Output format
For each test case (in a separate line), print space-separated integers denoting . If there are multiple answers, you can print any of them. It is guaranteed that under the given constraints at least 1 exists.
Constraints
Self explanatory.
Solution In C++
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int res=a[0];
for(long long int i=0;i<n;i++){
if(res%a[i]!=0){
res=ceil(res/(1.0*a[i]))*a[i];
}
cout<<res<<" ";
}
cout<<endl;
}
}
Solution In C
#include <stdio.h>
int main(){
int t;
scanf("%d",&t);
while(t--){
long long int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
long long int res=a[0];
for(long long int i=0;i<n;i++){
if(res%a[i]!=0){
res=ceil(res/(1.0*a[i]))*a[i];
}
printf("%d ",res);
}
printf("\n");
}
}
Comments
Post a Comment