Question: Barclays, Recent Online Assesment Questions (22nd August 2023) | The Minimum Sum
2
Entering edit mode

ADD COMMENTlink 16 months ago PoGo 2.4k
0
Entering edit mode

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, x;
    cin >> n >> x;

    vector<long long> A(n); // Using long long to handle large numbers
    long long original_sum = 0;

    for (int i = 0; i < n; i++) {
        cin >> A[i];
        original_sum += A[i];
    }

    long long min_sum = original_sum;

    for (int i = 0; i < n; i++) {
        long long transformed_value = A[i];
        while (transformed_value % x == 0) {
            transformed_value /= x;
        }
        transformed_value *= x;

        long long current_sum = original_sum - A[i] + transformed_value;
        min_sum = min(min_sum, current_sum);
    }

    cout << min_sum << endl;
    return 0;
}
 

ADD COMMENTlink 15 months ago Ashutosh singh • 0
0
Entering edit mode

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,x;
    cin>>n>>x;
    vector<long long> v(n);
    long long origsum=0;
    for(int i=0;i<n;i++){ cin>>v[i]; origsum+=v[i];}
    
    long long minsum=origsum;
    for(int i=0;i<n;i++)
    {
        long long val=v[i];
        if(val%x==0) continue;
        while(val%x!=0)
            val= (val/x)*x;        
        minsum-=v[i];
        minsum+=val;
    }
    return minsum;
}

ADD COMMENTlink 15 months ago Pankaj Semwal • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts