Skip to main content

Java 8 coding challenge : Jarvis and Seven Segments

All over the world, peoples are working on energy solution. It would be a tough time for our next generation to survive if we don’t think about solution. Tony stark is working on a new project and wants to display his project using “seven segment display - concept”. Tony Stark gave Jarvis a task to find a number from his Favorite list of number for which the energy consumption is lowest.
(Assuming that for a digit to represent Tony stark is using 7 bulbs and only those bulbs light up which are required to represent a number and rest other would be completely off.)
Help Jarvis and conserve energy.
enter image description here
Input:
First line will contain the number of test cases and for every test case first line will contain length of favorite list and the second line for a test case will contain n numbers
Output:
For every test case print the answer. If there exist more than 1 numbers for which same number of bulbs are required than output the number which occurs first in the Favorite list.
Constraints:
Test cases< 10
A[i] < 10^6
Size of list < 10^5
SAMPLE INPUT
 
 
1
5
1 2 3 4 5
SAMPLE OUTPUT
 
 
1
 
Explanation
Number 1 needs only two bulbs to represent.

Code:

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
 
 
 
 
public class Solution {
 
 
 public static void main(String args[]) throws IOException{
 InputReader in=new InputReader(System.in);
 PrintWriter out=new PrintWriter(System.out);
 int T=in.readInt();
 
 while(T-->0){
 long N=in.readLong();
 long min=222222222;
 long minnum=0;
 while(N-->0){
 long num=in.readCount();
 long count=in.getcount();
 if(count<min){
 min=count;
 minnum=num;
 }
 }
 out.println(minnum);
 out.flush();
 
 }
 
 out.close();
 
 }
}
 
class InputReader{
 InputStream in;
 int[] segment={6,2,5,5,4,5,6,3,7,6};
 long count=0;
 InputReader(InputStream in){
 this.in=in;
 }
 
 public long getcount(){
 return count;
 }
 private int read() throws IOException{
 return in.read();
 }
 
 
 
 public char readChar() throws IOException{
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 return (char)n;
 }
 
 public int readInt() throws IOException{
 int number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 public long readCount() throws IOException{
 long number=0;
 count=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 count+=segment[integer];
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 
 public long readLong() throws IOException{
 long number=0;
 int n=read();
 while(isWhiteSpace(n)){
 n=read();
 }
 while(!isWhiteSpace(n)){
 int integer=n-'0';
 number*=10;
 number+=integer;
 n=read();
 }
 return number;
 }
 
 private boolean isWhiteSpace(int n){
 if(n=='\n'||n=='\r'||n=='\t'||n==' '||n==-1){
 return true;
 }else{
 return false;
 }
 
 }
 }

Comments

Popular posts from this blog

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. The key features of Terraform are: Infrastructure as Code : Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and...

Salt stack issues

The function “state.apply” is running as PID Restart salt-minion with command:  service salt-minion restart No matching sls found for ‘init’ in env ‘base’ Add top.sls file in the directory where your main sls file is present. Create the file as follows: 1 2 3 base: 'web*' : - apache If the sls is present in a subdirectory elasticsearch/init.sls then write the top.sls as: 1 2 3 base: '*' : - elasticsearch.init How to execute saltstack-formulas create file  /srv/pillar/top.sls  with content: base : ' * ' : - salt create file  /srv/pillar/salt.sls  with content: salt : master : worker_threads : 2 fileserver_backend : - roots - git gitfs_remotes : - git://github.com/saltstack-formulas/epel-formula.git - git://github.com/saltstack-formulas/git-formula.git - git://github.com/saltstack-formulas/nano-formula.git - git://github.com/saltstack-f...

Helm: Installation and Configuration

PREREQUISITES You must have Kubernetes installed. We recommend version 1.4.1 or later. You should also have a local configured copy of  kubectl . Helm will figure out where to install Tiller by reading your Kubernetes configuration file (usually  $HOME/.kube/config ). This is the same file that  kubectl  uses. To find out which cluster Tiller would install to, you can run  kubectl config current-context or  kubectl cluster-info . $ kubectl config current-context my-cluster INSTALL HELM Download a binary release of the Helm client. You can use tools like  homebrew , or look at  the official releases page . For more details, or for other options, see  the installation guide . INITIALIZE HELM AND INSTALL TILLER Once you have Helm ready, you can initialize the local CLI and also install Tiller into your Kubernetes cluster in one step: $ helm init This will install Tiller into the Kubernetes cluster you saw with  kubectl config current-context . TIP:  Want to install into a different cl...