0% found this document useful (0 votes)
79 views5 pages

Equal Stack and Down To Zero Problem of Hacckerank

The document describes two tasks related to stacks of cylinders and number reduction queries. For task 1, the goal is to remove cylinders from stacks to make their heights equal, and the output is the maximum equal height. For task 2, the goal is to reduce input numbers to 0 using operations of dividing a number by its largest factor or decreasing by 1, and the output is the minimum moves. Code solutions are provided for both tasks in Java using concepts like vectors, queues, and BFS.

Uploaded by

arjun singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views5 pages

Equal Stack and Down To Zero Problem of Hacckerank

The document describes two tasks related to stacks of cylinders and number reduction queries. For task 1, the goal is to remove cylinders from stacks to make their heights equal, and the output is the maximum equal height. For task 2, the goal is to reduce input numbers to 0 using operations of dividing a number by its largest factor or decreasing by 1, and the output is the minimum moves. Code solutions are provided for both tasks in Java using concepts like vectors, queues, and BFS.

Uploaded by

arjun singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Task 1 - Equal Stacks

You have three stacks of cylinders where each cylinder has the same diameter,
but they may vary in height. You can change the height of a stack by removing
and discarding its topmost cylinder any number of times.
Find the maximum possible height of the stacks such that all of the stacks are
exactly the same height. This means you must remove zero or more cylinders
from the top of zero or more of the three stacks until they are all the same height,
then return the height.

Sample Input -
534
32111
432
1141
Sample Output -
5

Code -
//JAVA, 20BCS7431
import java.util.Scanner;
import java.util.Vector;
class Solution{
public static int sum(Vector<Integer>
vec) { int s=0;
for(Integer i:vec)
s+=i;
return s;
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
Vector <Integer> vec=new Vector<Integer>();
for(int i=0;i<a;i++)
vec.add(sc.nextInt());
Vector <Integer> st=new Vector<Integer>();
for(int i=0;i<b;i++)
st.add(sc.nextInt());

Vector <Integer> arr=new Vector<Integer>();


for(int i=0;i<c;i++)
arr.add(sc.nextInt());

int sum1=sum(vec);
int sum2=sum(st);
int sum3=sum(arr);
int min=0;
while(sum1>=0 && sum2>=0 && sum3>=0){
if(sum1>sum2 || sum1>sum3) {
sum1-=vec.elementAt(0);
vec.removeElementAt(0);
}

if(sum2>sum3 || sum2>sum1) {
sum2-=st.elementAt(0);
st.removeElementAt(0);
}

if(sum3>sum1 || sum3>sum2) {
sum3-=arr.elementAt(0);
arr.removeElementAt(0);
}

else if(sum1==sum2 && sum2==sum3){


min=sum1;
break;
}
}
System.out.println(min);
sc.close();
}
}
Output -

Task 2 - Down to Zero.


You are given Q queries. Each query consists of a single number N. You can
perform any of the 2 operations on N in each move:
1: If we take 2 integers a and b where N=a x b (a!=1, b!=1) , then we can change
N=max(a, b).
2: Decrease the value of N by 1.
Determine the minimum number of moves required to reduce the value of N to 0.
Sample Input -
2
3
4
Sample Output -
3
3
Code -
//JAVA, 20BCS7431
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Solution{
public int count(int n) {
Queue<Integer> num=new LinkedList<>();
int store[]= new int[1000000];
num.offer(n);
while(!num.isEmpty()) {
int x=num.poll();
if(x==0)
break;
if(store[x-1]==0){
num.offer(x-1);
store[x-1]=store[x]+1;
}
for(int i=(int) Math.sqrt(x);i>=2;i--) {
if(x%i==0 && store[x/i]==0) {
num.offer(x/i); store[x/i]=store[x]
+1;
}
}
}
return store[0];
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int q=sc.nextInt();
while(q-->0) {
int n=sc.nextInt();
Solution obj=new Solution();
int count=obj.count(n);
System.out.println(count);
}
sc.close();
}
}
Output -

Evaluation Grid (To be created as per the SOP and Assessment guidelines
by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1
2
3
4

You might also like