Skip to main content
added 47 characters in body
Source Link
VivekN
  • 167
  • 4
import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
            a=new int[n];
               
            digit=in.readLine();
        
            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void calchef()
    {
        for(int i=0;i<m;i++)
        {  
            try
            {
                b1=0;
                b2=0;
                int index=Integer.parseInt(in.readLine());
                if(mymap.containsKey(index))
                {
                    int ans=mymap.get(index);
                    out.println(ans);
                }
                else
                {
                    for(int j=0;j<index-1;j++)
                    {
                        if(a[index-1]-a[j]>0)
                        {
                            b1+=(a[index-1]-a[j]);
                        }
                        else
                        {
                            b2+=(a[index-1]-a[j]);
                        }
                    }
                    int ans=b1-b2;
                    out.println(ans);
                    mymap.put(index,ans);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}
import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
            a=new int[n];
               
            digit=in.readLine();
        
            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void calchef()
    {
        for(int i=0;i<m;i++)
        {  
            try
            {
                b1=0;
                b2=0;
                int index=Integer.parseInt(in.readLine());
                if(mymap.containsKey(index))
                {
                    int ans=mymap.get(index);
                    out.println(ans);
                }
                else
                {
                    for(int j=0;j<index-1;j++)
                    {
                        if(a[index-1]-a[j]>0)
                        {
                            b1+=(a[index-1]-a[j]);
                        }
                        else
                        {
                            b2+=(a[index-1]-a[j]);
                        }
                    }
                    int ans=b1-b2;
                    out.println(ans);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}
import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
            a=new int[n];
               
            digit=in.readLine();
        
            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void calchef()
    {
        for(int i=0;i<m;i++)
        {  
            try
            {
                b1=0;
                b2=0;
                int index=Integer.parseInt(in.readLine());
                if(mymap.containsKey(index))
                {
                    int ans=mymap.get(index);
                    out.println(ans);
                }
                else
                {
                    for(int j=0;j<index-1;j++)
                    {
                        if(a[index-1]-a[j]>0)
                        {
                            b1+=(a[index-1]-a[j]);
                        }
                        else
                        {
                            b2+=(a[index-1]-a[j]);
                        }
                    }
                    int ans=b1-b2;
                    out.println(ans);
                    mymap.put(index,ans);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}
linked to CodeChef challenge
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

The questionchallenge is to reconstruct an n-digit number using the following information:

On each step, you choose an index xx from 1 to nn. For all indices yy (yy < xx), you calculate the difference byby = axax - ayay.

You then calculate BB1 - sum of all by which are greater than 0 and BB2 - sum of all by which are less than 0.

The answer for this step is BB1 - BB2.

Input

The first line contains two integers n and m, denoting the number of digits and number of steps. The second line contains n digits (without spaces) aa1, aa2, ..., aann. Each of the next m lines contains a single integer x denoting the index for the current step.
  

Output

For each of mm steps print single number in a line - answer of the step.

Can someone please help me optimize this code or provide a better, less time consuming-consuming solution?

The question is:

On each step, you choose an index x from 1 to n. For all indices y (y < x), you calculate the difference by = ax - ay.

You then calculate B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0.

The answer for this step is B1 - B2.

Input

The first line contains two integers n and m, denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of the next m lines contains a single integer x denoting the index for the current step.
 Output

For each of m steps print single number in a line - answer of the step.

Can someone please help me optimize this code or provide a better less time consuming solution?

The challenge is to reconstruct an n-digit number using the following information:

On each step, you choose an index x from 1 to n. For all indices y (y < x), you calculate the difference by = ax - ay.

You then calculate B1 sum of all by which are greater than 0 and B2 sum of all by which are less than 0.

The answer for this step is B1 - B2.

Input

The first line contains two integers n and m, denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of the next m lines contains a single integer x denoting the index for the current step. 

Output

For each of m steps print single number in a line - answer of the step.

Can someone please help me optimize this code or provide a better, less time-consuming solution?

Attempted to improve title; fixed indentation (tabs to spaces)
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

optimize my java program Calculate difference of indices

The question is:

On each step, you choose an index x from 1 to n. For all indices y (y < x), you calculate the difference by = ax - ay.

You then calculate B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0.

The answer for this step is B1 - B2.

Input

The first line contains two integers n and m, denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of the next m lines contains a single integer x denoting the index for the current step.
Output

For each of m steps print single number in a line - answer of the step.

Can someone plsplease help me optimize this code or provide a better less time consuming solution for my given code

The question is u hav On each step u choose an index x from 1 to n. For all indices y (y < x) u calculate the difference by = ax - ay. Then u calculate B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0. The answer for this step is B1 - B2.

Input

The first line contains two integers n, m denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of next m lines contains single integer x denoting the index for current step.

Output

For each of m steps print single number in a line - answer of the step.

Here is my code in java?

import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
                    a=new int[n];
               
            digit=in.readLine();
        
            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
           }
        catch(Exception e)
    }
    catch(Exception e){
    {
        e.printStackTrace();
        }
    }
void calchef()
{
   void forcalchef(int i=0;i<m;i++)
    {
         for(int i=0;i<m;i++)
      
   {   
            try
                {
                    b1=0;
                    b2=0;
                    int index=Integer.parseInt(in.readLine());
                    if(mymap.containsKey(index))
                    {
                        int ans=mymap.get(index);
                        out.println(ans);
                    }
                    else
                    {
                        for(int j=0;j<index-1;j++)
                    {
    {
                    if(a[index-1]-a[j]>0)
                            {
                                   b1+=(a[index-1]-a[j]);
                                  }
                            }
            else
                            {
                                b2+=(a[index-1]-a[j]);
                                
                            }
                        }
                         int ans=b1-b2;
                         out.println(ans);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
               
    
    }
            
                }
    
}
public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}

}

optimize my java program

Can someone pls help me optimize this code or provide a better less time consuming solution for my given code

The question is u hav On each step u choose an index x from 1 to n. For all indices y (y < x) u calculate the difference by = ax - ay. Then u calculate B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0. The answer for this step is B1 - B2.

Input

The first line contains two integers n, m denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of next m lines contains single integer x denoting the index for current step.

Output

For each of m steps print single number in a line - answer of the step.

Here is my code in java

import java.io.*;
import java.util.*;
public class Chefs
{
//BufferedReader in;
DataInputStream in;
PrintWriter out;
HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
int a[],n,m,b1,b2;
public Chefs()
{
    //in=new BufferedReader(new InputStreamReader(System.in));
    in=new DataInputStream(System.in);
    out=new PrintWriter(System.out,true);
    readInput();
    calchef();
}
void readInput()
{
    try
    {
        String digit=in.readLine();
        String split[]=digit.split("\\s+");
        n=Integer.parseInt(split[0]);
        m=Integer.parseInt(split[1]);
                    a=new int[n];
               
        digit=in.readLine();
        
        for(int i=0;i<digit.length();i++)
        a[i]=digit.charAt(i);
                    
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
void calchef()
{
    for(int i=0;i<m;i++)
    {
                
                 try
                {
                    b1=0;
                    b2=0;
                    int index=Integer.parseInt(in.readLine());
                    if(mymap.containsKey(index))
                    {
                        int ans=mymap.get(index);
                        out.println(ans);
                    }
                    else
                    {
                        for(int j=0;j<index-1;j++)
                        {
            if(a[index-1]-a[j]>0)
                            {
                                   b1+=(a[index-1]-a[j]);
                                  
                            }
            else
                            {
                                b2+=(a[index-1]-a[j]);
                                
                            }
                        }
                         int ans=b1-b2;
                         out.println(ans);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
               
    
    }
            
                
    
}
public static void main(String[] args)
{
    //long startTime = System.currentTimeMillis();
    Chefs c=new Chefs();
    //long endTime   = System.currentTimeMillis();
    //long totalTime = endTime - startTime;
    //System.out.println(totalTime);
}

}

Calculate difference of indices

The question is:

On each step, you choose an index x from 1 to n. For all indices y (y < x), you calculate the difference by = ax - ay.

You then calculate B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0.

The answer for this step is B1 - B2.

Input

The first line contains two integers n and m, denoting the number of digits and number of steps. The second line contains n digits (without spaces) a1, a2, ..., an. Each of the next m lines contains a single integer x denoting the index for the current step.
Output

For each of m steps print single number in a line - answer of the step.

Can someone please help me optimize this code or provide a better less time consuming solution?

import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
            a=new int[n];
               
            digit=in.readLine();
        
            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void calchef()
    {
        for(int i=0;i<m;i++)
        {   
            try
            {
                b1=0;
                b2=0;
                int index=Integer.parseInt(in.readLine());
                if(mymap.containsKey(index))
                {
                    int ans=mymap.get(index);
                    out.println(ans);
                }
                else
                {
                    for(int j=0;j<index-1;j++)
                    {
                        if(a[index-1]-a[j]>0)
                        {
                            b1+=(a[index-1]-a[j]);
                        }
                        else
                        {
                            b2+=(a[index-1]-a[j]);
                        }
                    }
                    int ans=b1-b2;
                    out.println(ans);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}
Source Link
VivekN
  • 167
  • 4
Loading