Minimum sum of Quotients after performing the given operation
Last Updated :
02 Dec, 2023
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: , where 0 <= i <= N-1 and k is the index of the element whose value we have changed.
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 = 4
Input: 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:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
long min_notes( int N, int A[])
{
long sum = 0, ans = 1000000000;
int pref[N];
int suff[N];
for ( int i = 0; i < N; i++) {
sum += A[i];
}
if (N == 1) {
return 1;
}
pref[0] = A[0];
for ( int i = 1; i < N; i++) {
pref[i] = gcd(pref[i - 1], A[i]);
}
suff[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suff[i] = gcd(suff[i + 1], A[i]);
}
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;
}
return ans + 1;
}
int main()
{
int N = 3;
int A[] = { 2, 3, 4 };
cout << min_notes(N, A) << endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 3 ;
int A[] = { 2 , 3 , 4 };
System.out.println(min_notes(N, A));
}
public static long min_notes( int N, int [] A)
{
long sum = 0 , ans = 1000000000 ;
int pref[] = new int [N];
int suff[] = new int [N];
for ( int i = 0 ; i < N; i++) {
sum += A[i];
}
if (N == 1 ) {
return 1 ;
}
pref[ 0 ] = A[ 0 ];
for ( int i = 1 ; i < N; i++) {
pref[i] = gcd(pref[i - 1 ], A[i]);
}
suff[N - 1 ] = A[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--) {
suff[i] = gcd(suff[i + 1 ], A[i]);
}
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;
}
return ans + 1 ;
}
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
}
|
Python3
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))
|
C#
using System;
class MainClass
{
public static void Main ( string [] args)
{
int N = 3;
int [] A = { 2, 3, 4 };
Console.WriteLine(MinNotes(N, A));
}
public static long MinNotes( int N, int [] A)
{
long sum = 0, ans = 1000000000;
int [] pref = new int [N];
int [] suff = new int [N];
for ( int i = 0; i < N; i++) {
sum += A[i];
}
if (N == 1) {
return 1;
}
pref[0] = A[0];
for ( int i = 1; i < N; i++) {
pref[i] = GCD(pref[i - 1], A[i]);
}
suff[N - 1] = A[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suff[i] = GCD(suff[i + 1], A[i]);
}
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;
}
return ans + 1;
}
static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
}
|
Javascript
function main() {
let N = 3;
let A = [2, 3, 4];
console.log(minNotes(N, A));
}
function minNotes(N, A) {
let sum = 0;
let ans = 1000000000;
let pref = new Array(N);
let suff = new Array(N);
for (let i = 0; i < N; i++) {
sum += A[i];
}
if (N === 1) {
return 1;
}
pref[0] = A[0];
for (let i = 1; i < N; i++) {
pref[i] = gcd(pref[i - 1], A[i]);
}
suff[N - 1] = A[N - 1];
for (let i = N - 2; i >= 0; i--) {
suff[i] = gcd(suff[i + 1], A[i]);
}
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;
}
}
return ans + 1;
}
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
main();
|
Time Complexity: O(N*log(max)), where max is the largest element of array A[].
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...