Problem
You are given an array containing integers. You can apply the following operation on the array:
- Choose an index , split into two positive integers such that , remove the element from the array and append the elements to the array.
Find the minimum number of operations required to make all integers of the array equal.
Input format
- The first line contains denoting the number of test cases. The description of test cases is as follows:
- For each test case:
- The first line contains a single integer denoting the size of array .
- The second line contains integers - denoting the elements of .
Output format
For each test case, print the minimum number of operations required to make all integers of the array equal.
Constraints
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
Test case 1: Choose i = 2 and split A[2] = 4 into X = 2, Y = 2 making A = [2, ,2, 2]. Now all elements of the array are equal.
Test case 2:
- Choose i = 2 and split A[2] = 2 into X = 1, Y = 1 making A = [1, 3, 1, 1].
- Choose i = 2 and split A[2] = 3 into X = 2, Y = 1 making A = [1, 1, 1, 2, 1].
- Choose i = 4 and split A[4] = 2 into X = 1, Y = 1 making A = [1, 1, 1, 1, 1, 1]. Now all elements of the array are equal.
Code(C++):-
#include <iostream>
using namespace std;
template <typename T> inline void inp (T &n) {
n = 0;
int ch = getchar_unlocked (), sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1; // only if also negative integer
ch = getchar_unlocked ();
}
while (ch >= '0' && ch <= '9')
n = (n << 3) + (n << 1) + ch - '0', ch = getchar_unlocked ();
n *= sign; // only if also negative integer
}
inline int inp () {
int n = 0;
int ch = getchar_unlocked (), sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1; // only if also negative integer
ch = getchar_unlocked ();
}
while (ch >= '0' && ch <= '9')
n = (n << 3) + (n << 1) + ch - '0', ch = getchar_unlocked ();
n *= sign; // only if also negative integer
return n;
}
template <typename T> inline void puti (T n, char lc) {
if (0 == n) {
putchar_unlocked ('0');
if (lc) putchar_unlocked (lc);
return;
}
bool sign = false;
if (n < 0) sign = true, n = -n;
char s[20]; int rdi =-1;
while (n) {
s [++rdi] = '0' + n % 10;
n /= 10;
}
if (sign) putchar_unlocked ('-');
while (rdi >= 0) putchar_unlocked (s [rdi--]);
if (lc) putchar_unlocked (lc);
}
inline void puts (string s) {
for (char c : s) putchar_unlocked (c);
}
inline void inps (string &i) {
i = "";
char c = getchar_unlocked ();
while (c < 32 or c > 127) c = getchar_unlocked ();
while (c >= 32 and c <= 127) i += c, c = getchar_unlocked ();
}
inline string inps () {
string i = "";
char c = getchar_unlocked ();
while (c < 32 or c > 127) c = getchar_unlocked ();
while (c >= 32 and c <= 127) i += c, c = getchar_unlocked ();
return i;
}
inline void inpc (char &c) {
c = getchar_unlocked ();
if (c == EOF) return;
while (c < 32 or c > 127) c = getchar_unlocked ();
}
inline char inpc () {
char c = getchar_unlocked ();
if (c == EOF) return c;
while (c < 32 or c > 127) c = getchar_unlocked ();
return c;
}
int main (){
int n, t, i, mn, rest;
long long ops;
bool nok;
inp (t);
while (t--) {
inp (n);
int a [n];
for (i = 0; i < n; i++) inp (a [i]);
mn = a [0];
nok = true;
while (nok) {
ops = 0;
nok = false;
for (auto v : a) {
ops += v / mn - 1;
rest = v % mn;
if (rest) {
mn = rest;
nok = true;
break;
}
}
}
puti (ops, '\n');
}
}
Code(JAVA):-
import java.util.*;
import java.io.*;
public class TestClass {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out)));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken());
int gcd = arr[0];
for (int x : arr) gcd = gcd(gcd, x);
long ans = 0;
for (int x : arr) ans += (x - gcd) / gcd;
sb.append(ans).append("\n");
}
pw.println(sb.toString().trim());
pw.close();
br.close();
}
public static int gcd(int x, int y) {
if (x == 0) return y;
return gcd(y % x, x);
}
}
Recommended Post :-
HCL Coding Questions:-
Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
Must check this:-
Companies interview:-
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments