Given an array A[] of length N, we can change the value of exactly one element of the array to any integer greater than 0, such that the sum of quotients of all the elements when divided by the new element is minimum. In other words, we have to find:
Note: The new element should perfectly divide all the elements of the array, without leaving any remainder.
Examples:
Input: N = 3, A[] = {2, 3, 4}
Output: 4
Explanation: We will alter the element A[1] = 3 to A[1] = 2, then A[] = {2, 2, 4}. Now, the sum of quotients will be:
(A[0] / A[1]) + (A[1] / A[1]) + (A[2] / A[1]) = 1 + 1 + 2 = 4Input: N = 3, A[] = {8, 8, 8}
Output: 3
Explanation: We can alter A[0] = 8 to A[0] = 8. Now, the sum of quotients will be:
(A[0] / A[0]) + (A[1] / A[0]) + (A[2] / A[0]) = 1 + 1 + 1 = 3
Approach: The problem can be solved using the following approach:
The problem is related to Number Theory and can be solved using calculating the GCD of all the elements of array A[] and using the prefix[], suffix[] arrays to store GCD of prefix and suffix subarrays.
Steps were taken to solve the problem:
- Create a variable let’s say sum to store the sum of all elements.
- Create prefix and suffix arrays of length N let say pref[] and suff[] respectively.
- Run a loop and calculate the sum of all elements and store it into sum.
- If length of A[] is 1, then return 1.
- Create a variable ans to store the minimum number of operations required.
- Initialize pref[0] and suff[N-1] = 0
- Run a loop for i = 1 and i < N and initialize pref[i] = gcd(pref[i-1], A[i-1])
- Run a loop for i = N-2 and i >=0 and initialize suff[i] = gcd(suff[i+1], A[i+1])
-
Run a loop for i = 0 to i < N:
- Calculate gcd of pref[i-1] and suff[i+1] and store it in g
- val = (sum–A[i])/g
- If (val < ans), then ans = val
- return ans+1
Code to implement the approach:
#include <iostream> using namespace std;
// Function to calculate the GCD of two numbers int gcd( int a, int b)
{ if (b == 0)
return a;
return gcd(b, a % b);
} // Function to calculate the minimum number of notes // required long min_notes( int N, int A[])
{ long sum = 0, ans = 1000000000;
// Prefix and Suffix arrays of length N
int pref[N];
int suff[N];
// Loop for calculating sum
for ( int i = 0; i < N; i++) {
sum += A[i];
}
// Size of the array is 1
if (N == 1) {
return 1;
}
// Initializing the prefix GCD array
pref[0] = A[0];
for ( int i = 1; i < N; i++) {
pref[i] = gcd(pref[i - 1], A[i]);
}
// Initializing the suffix GCD array
suff[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suff[i] = gcd(suff[i + 1], A[i]);
}
// Checking all indices for our answer
for ( int i = 0; i < N; i++) {
int g = 0;
if (i == 0) {
g = suff[i + 1];
}
else if (i == N - 1) {
g = pref[i - 1];
}
else {
g = gcd(pref[i - 1], suff[i + 1]);
}
long val = (sum - A[i]) / g;
if (val < ans)
ans = val;
}
// Printing the minimum number of notes required
return ans + 1;
} int main()
{ int N = 3;
int A[] = { 2, 3, 4 };
// Function call
cout << min_notes(N, A) << endl;
return 0;
} |
// Java code to implement the approach import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Inputs
int N = 3 ;
int A[] = { 2 , 3 , 4 };
// Function call
System.out.println(min_notes(N, A));
}
// Method to print the minimum
// number of notes required
public static long min_notes( int N, int [] A)
{
// Variable to store the sum
long sum = 0 , ans = 1000000000 ;
// Prefix and Suffix arrays
// of lenth N
int pref[] = new int [N];
int suff[] = new int [N];
// Loop for calculating sum
for ( int i = 0 ; i < N; i++) {
sum += A[i];
}
// Size of array is 1
if (N == 1 ) {
return 1 ;
}
// Initializing the prefix gcd array
pref[ 0 ] = A[ 0 ];
for ( int i = 1 ; i < N; i++) {
pref[i] = gcd(pref[i - 1 ], A[i]);
}
// Initializing the suffix gcd array
suff[N - 1 ] = A[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--) {
suff[i] = gcd(suff[i + 1 ], A[i]);
}
// Checking all indices
// for our answer
for ( int i = 0 ; i < N; i++) {
int g = 0 ;
if (i == 0 ) {
g = suff[i + 1 ];
}
else if (i == N - 1 ) {
g = pref[i - 1 ];
}
else {
g = gcd(pref[i - 1 ], suff[i + 1 ]);
}
long val = (sum - A[i]) / g;
if (val < ans)
ans = val;
}
// Printing the minimum number
// of notes required
return ans + 1 ;
}
// Method to return GCD of
// two numbers
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
} |
# Python Implementation def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def min_notes(N, A):
sum = 0
ans = 1000000000
pref = [ 0 ] * N
suff = [ 0 ] * N
for i in range (N):
sum + = A[i]
if N = = 1 :
return 1
pref[ 0 ] = A[ 0 ]
for i in range ( 1 , N):
pref[i] = gcd(pref[i - 1 ], A[i])
suff[N - 1 ] = A[N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
suff[i] = gcd(suff[i + 1 ], A[i])
for i in range (N):
g = 0
if i = = 0 :
g = suff[i + 1 ]
elif i = = N - 1 :
g = pref[i - 1 ]
else :
g = gcd(pref[i - 1 ], suff[i + 1 ])
val = ( sum - A[i]) / / g
if val < ans:
ans = val
return ans + 1
N = 3
A = [ 2 , 3 , 4 ]
print (min_notes(N, A))
# This code is contributed by Sakshi |
// C# code to implement the approach using System;
class MainClass
{ // Driver Function
public static void Main ( string [] args)
{
// Inputs
int N = 3;
int [] A = { 2, 3, 4 };
// Function call
Console.WriteLine(MinNotes(N, A));
}
// Method to print the minimum
// number of notes required
public static long MinNotes( int N, int [] A)
{
// Variable to store the sum
long sum = 0, ans = 1000000000;
// Prefix and Suffix arrays
// of length N
int [] pref = new int [N];
int [] suff = new int [N];
// Loop for calculating sum
for ( int i = 0; i < N; i++) {
sum += A[i];
}
// Size of array is 1
if (N == 1) {
return 1;
}
// Initializing the prefix gcd array
pref[0] = A[0];
for ( int i = 1; i < N; i++) {
pref[i] = GCD(pref[i - 1], A[i]);
}
// Initializing the suffix gcd array
suff[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suff[i] = GCD(suff[i + 1], A[i]);
}
// Checking all indices
// for our answer
for ( int i = 0; i < N; i++) {
int g = 0;
if (i == 0) {
g = suff[i + 1];
}
else if (i == N - 1) {
g = pref[i - 1];
}
else {
g = GCD(pref[i - 1], suff[i + 1]);
}
long val = (sum - A[i]) / g;
if (val < ans)
ans = val;
}
// Printing the minimum number
// of notes required
return ans + 1;
}
// Method to return GCD of
// two numbers
static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
} // this code is contributed by uttamdp_10 |
function main() {
// Inputs
let N = 3;
let A = [2, 3, 4];
// Function call
console.log(minNotes(N, A));
} function minNotes(N, A) {
// Variable to store the sum
let sum = 0;
let ans = 1000000000;
// Prefix and Suffix arrays of length N
let pref = new Array(N);
let suff = new Array(N);
// Loop for calculating sum
for (let i = 0; i < N; i++) {
sum += A[i];
}
// Size of array is 1
if (N === 1) {
return 1;
}
// Initializing the prefix gcd array
pref[0] = A[0];
for (let i = 1; i < N; i++) {
pref[i] = gcd(pref[i - 1], A[i]);
}
// Initializing the suffix gcd array
suff[N - 1] = A[N - 1];
for (let i = N - 2; i >= 0; i--) {
suff[i] = gcd(suff[i + 1], A[i]);
}
// Checking all indices for our answer
for (let i = 0; i < N; i++) {
let g = 0;
if (i === 0) {
g = suff[i + 1];
} else if (i === N - 1) {
g = pref[i - 1];
} else {
g = gcd(pref[i - 1], suff[i + 1]);
}
let val = (sum - A[i]) / g;
if (val < ans) {
ans = val;
}
}
// Returning the minimum number of notes required
return ans + 1;
} // Method to return GCD of two numbers function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
} // Calling the main function main(); |
Output
4
Time Complexity: O(N*log(max)), where max is the largest element of array A[].
Auxiliary Space: O(N)