Thursday 26 October 2017

CodeChef : Video Game Problem Code: ZCO14001 Zonal Computing Olympiad 2014, 30 Nov 2013

You are playing a video game in which several stacks of boxes are lined up on the floor, with a crane on top to rearrange the boxes, as shown in the picture below.








The crane supports the following commands:

• Move one position left (does nothing if already at the leftmost position)

• Move one position right (does nothing if already at the rightmost position)

• Pick up a box from the current stack (does nothing if the crane already has a box)

• Drop a box on the current stack (does nothing if the crane doesn't already have a box)

Further, there is a limit H on the number of boxes on each stack. If a 'drop' command would result in a stack having more than H boxes, the crane ignores this drop command. If the current stack has no boxes, a 'pick up' command is ignored.

You are given the initial number of boxes in each stack and the sequence of operations performed by the crane. You have to compute the final number of boxes in each stack.

For example, suppose the initial configuration of the game is as shown in the figure above, with 7 stacks and H=4. Then, after the following sequence of instructions,

1. Pick up box
2. Move right
3. Move right
4. Move right
5. Move right
6. Drop box
7. Move left
8. Pick up box
9. Move left
10. Drop box


the number of boxes in each stack from left to right would be 2,1,3,1,4,0,1.

Input format

• Line 1 : The width of the game (the number of stacks of boxes), N, followed by the max height H of each stack.

• Line 2 : N integers, the initial number of boxes in each stack, from left to right. Each number is ≤ H.

• Line 3 : A sequence of integers, each encoding a command to the crane.
        The commands are encoded as follows:
        1 : Move left
        2 : Move right
        3 : Pick up box
        4 : Drop box
        0 : Quit

• The command Quit (0) appears exactly once, and is the last command.
• The initial position of the crane is above the leftmost stack, with the crane not holding any box.

Output format

A single line with N integers, the number of boxes in each stack, from left to right.

Sample input 1

7 4
3 1 2 1 4 0 1
3 2 2 2 2 4 1 3 1 4 0

Sample output 1

2 1 3 1 4 0 1

Sample input 2

3 5
2 5 2
3 2 4 2 2 2 1 4 1 1 1 1 0

Sample output 2

1 5 2 

Test data


There is only one subtask worth 100 marks. In all inputs:
• The number of commands is between 1 and 105, inclusive.
• 1 ≤ N ≤ 105
• 1 ≤ H ≤ 108.

Live evaluation data

There are 18 test inputs on the server during the exam.



SOLUTION IN JAVA:  

import java.util.*;

class Crane {
public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    int N=sc.nextInt();
    int H=sc.nextInt();
    int [] ar=new int[N];
   
    //ArrayList al=new ArrayList();
    if(1<=N&&N<=100000&&1<=H&&H<=100000000){
    for(int i=0;i<N;i++){
        int g=sc.nextInt();
        if(g<=H)
            ar[i]=g;
    }
    sc.nextLine();
    //Scanner sc1=new Scanner(System.in);
    String s=sc.nextLine();
   
    StringTokenizer sk=new StringTokenizer(s," ");
    //System.out.println(s);
    int l=0,count=0;
    int nst=sk.countTokens();
    int cnst=0;
    if(1<=nst&&nst<=100000){
    while(sk.hasMoreTokens()){
        if(cnst==0){
            l=0;
             count=0;
        }
        cnst++;
    int s1=Integer.parseInt(sk.nextToken());
    //System.out.println(s1);
        if(s1==0&&cnst==nst){
            int count1=0;
            for(int i:ar){
                System.out.print(i);
            if(count1<N-1){
                System.out.print(" ");
            }
            count1++;
            }}
        if(s1==0&&cnst!=nst){
            break;
        }
         if(s1==1){
             //System.out.println("s1==1 block is running");
            if(l>0){
            l-=1;}}
         if(s1==2){
             if(l<ar.length-1)
            l+=1;}
         if(s1==3){
            // System.out.println("s1==3 block is running");
            if(ar[l]>0&&count==0){
               
            ar[l]=ar[l]-1;count=1;
            //System.out.println("s1==3 block is running");
            }}
         if(s1==4&&count==1&&ar[l]<H){
            ar[l]=ar[l]+1;
         count=0;}
       
    }}}
    //sc.close();
}
}
 

 

 

Tuesday 17 October 2017

codeChef :Rupsa and the Game Problem Code: RGAME

Princess Rupsa saw one of her friends playing a special game. The game goes as follows:
  • N+1 numbers occur sequentially (one at a time) from A0 to AN.
  • You must write the numbers on a sheet of paper, such that A0 is written first. The other numbers are written according to an inductive rule — after Ai-1 numbers have been written in a row, then Ai can be written at either end of the row. That is, you first write A0, and then A1 can be written on its left or right to make A0A1 or A1A0, and so on.
  • Ai must be written before writing Aj, for every i < j.
  • For a move in which you write a number Ai (i>0), your points increase by the product of Ai and its neighbour. (Note that for any move it will have only one neighbour as you write the number at an end).
  • Total score of a game is the score you attain after placing all the N + 1 numbers.
Princess Rupsa wants to find out the sum of scores obtained by all possible different gameplays. Two gameplays are different, if after writing down all N + 1 numbers, when we read from left to right, there exists some position i, at which the gameplays have aj and ak written at the ith position such that j ≠ k. But since she has recently found her true love, a frog Prince, and is in a hurry to meet him, you must help her solve the problem as fast as possible. Since the answer can be very large, print the answer modulo 109 + 7.

Input

  • The first line of the input contains an integer T denoting the number of test cases.
  • The first line of each test case contains a single integer N.
  • The second line contains N + 1 space-separated integers denoting A0 to AN.

Output

  • For each test case, output a single line containing an integer denoting the answer.

Constraints

  • 1T10
  • 1N105
  • 1Ai109

Sub tasks

  • Subtask #1: 1 ≤ N ≤ 10 (10 points)
  • Subtask #2: 1 ≤ N ≤ 1000 (20 points)
  • Subtask #3: Original Constraints (70 points)

Example

Input:
2
1
1 2
2
1 2 1

Output:
4
14

Explanation

There are 2 possible gameplays. A0A1 which gives score of 2 and A1A0 which also gives score of 2. So the answer is 2 + 2 = 4 




SOLUTION IN JAVA:   

 import java.util.*;
public class Rupsa {
public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    long mod=1000000007L;
    int t=sc.nextInt();int[] art=new int[t];
    for(int i=0;i<t;i++){
        int N=sc.nextInt();
        int []ar=new int[N+1];long l,m=0,n=0,p=1;
    //int jl=2,kl=1;
  
        for(int j=0;j<N+1;j++){
          
            ar[j]=sc.nextInt();
        }
        long o=ar[0];
            for(int j=1;j<N+1;j++){
                long k=2;
                m*=2;
            k=(k*o*ar[j])%mod;
            m=(m+k)%mod;
            o=(o+p*ar[j])%mod;
                p=(p*2)%mod;
              
          
        }
        int    m1=(int)m%1000000007;
        art[i]=m1;
    }
    for(int h:art){
        System.out.println(h);
    }
}
}

 

Friday 13 October 2017

CodeChef October Chellange 2017 : Chef and a great voluntary Program.

Chef likes to eat apples and bananas a lot. Today, he is doing a voluntary service by providing these fruits to needy people in a lunger (a gathering of needy people). The people form a queue to collect fruits from Chef. Chef distributes each person one of the fruits either an apple or a banana.
You are given information about the way Chef distributed fruits to the people by a string s, whose each character is either 'a' or 'b' denoting whether the corresponding person was distributed an apple or a banana.
After the event Chef wants to analyze what he could have done to make the event memorable. The event will be memorable if everyone at the end is happy. Whenever a person in the queue get an apple or a banana then he will observe the most recent K fruits distributed (including fruits he previously took, but not counting the fruit he just got) and if all of them were same as the fruit he just got, then he becomes disgruntled.
The value of K if the person is getting an apple is x, and similarly y for banana.
To make all the people happy, Chef can change the order of distributing the fruits (apple and bananas) and also can give some people extra kiwi fruits. Since the kiwis are costly, Chef would want to minimize the number of kiwis he needed to distribute. Note that a person leaves the queue as soon as Chef distributes him either an apple or a banana. So Chef should provide a person some kiwis before providing an apple or a banana, otherwise Chef won't be able to provide him kiwi as they would have already left the queue.

Help Chef in finding a way of distributing the fruits to the people that make everyone happy and requires Chef to buy as few kiwis as possible.

Input

The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains the string s.
The second line contains two space separated integers x, y.

Output

For each test case, output a single string corresponding to the way in which the fruits should be distributed to the people. Apple fruit is denoted by 'a', banana by 'b', and kiwi by '*'. If there are more than one possible solutions, you can print any. Note that the number of apples in the output should be equal to the number of apples in the initial distributions of fruits denoted by string s. Same goes for bananas.

Constraints

  • 1 ≤ T ≤ 50
  • 1 ≤ |s| ≤ 105
  • 1 ≤ x, y ≤ |s|

Subtasks

  • Subtask #1 (20 points): x = y = 1
  • Subtask #2 (30 points): 1 ≤ |s| ≤ 103
  • Subtask #3 (50 points): Original constraints

Example

Input
6
ab
1 1
aab
1 1
aabb
1 2
aaaaab
2 1
aaaa
1 3
aaaaa
2 2
Output
ab
aba
abba
aa*abaa
a*a*a*a
aa*aa*a

Explanation

Example 1. The way of distributing fruits already makes everyone happy.
Example 2. The second person will be disgruntled as he receives apple same as the person ahead of him. One possible way of having good impression is to distribute in the order aba.
Example 4. It's impossible for the Chef to make everyone happy without buying extra kiwi fruits. Chef will buy one extra kiwi fruit. Now, he distributes aa*abaa, i.e. apple to first and second, kiwi and apple to third, banana to fourth, and apples to fifth and sixth persons. This makes everyone happy. Notice that third person is happy because when he got an apple he checked the last 2 fruits taken which are the apple that the second person and the kiwi that he previously took, and they are not all apples so he is happy.
Example 6. at least two kiwis are needed here, if we gave the first one to third person and second one to fifth person then no one will be disgruntled, for example when the third person get an apple he will check the last two fruits distributed, they are one kiwi given to the same person previously and one apple given the second person so not all of them are apples, so it's fine with him.



SOLUTION:  IN  JAVA.



import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
public class ChefVoluntary1 {


    public static void main(String []args) throws IOException {
      
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr,50);
byte t=Byte.valueOf(br.readLine());
int k=0;

byte i;

String []sf=new String[t];
for(i=0;i<t;i++){
  
int j=0;
String []s2=new String[2];
    BufferedReader br1=new BufferedReader(isr,16*100000);  
  
    String s3=br1.readLine();
  
  
    BufferedReader br2=new BufferedReader(isr,6);
  
StringTokenizer st=new StringTokenizer(br2.readLine()," ");
while(st.hasMoreTokens()){
    s2[j]=st.nextToken();
    j++;
}
    int  a=Integer.parseInt(s2[0]);
 int  b=Integer.parseInt(s2[1]);
  
  

int count1=0,count2=0;

for(int m=s3.length()-1;m>=0;m--){
    if(s3.charAt(m)=='a'){
        count1++;
    }
    if(s3.charAt(m)=='b'){
        count2++;
    }
}

for(int m=s3.length()-1;m>=0;m--){
  
    if(m>-1&&s3.charAt(m)=='a'){
        int count=0;
        for(int l=1;l<=a;l++){
            if((m-l)>-1){
            if(s3.charAt(m-l)==s3.charAt(m)){
                count++;
              
            }}}
            if(count==a){
                int kl=0;
                    if((s3.indexOf('b',0))!=-1&&k!=1){
                        StringBuilder sb=new StringBuilder(s3);
                        char l=s3.charAt(m);char r=s3.charAt(s3.lastIndexOf('b'));
                        sb.setCharAt(m,r);
                        int all=s3.lastIndexOf('b');
                        sb.setCharAt((s3.lastIndexOf('b')),l);
                        s3=sb.toString();
                      
           if(    bb(s3.substring(m,s3.length()),a,b)==false){
                            sb.setCharAt(m,s3.charAt(all));
                            sb.setCharAt(all,s3.charAt(m));
                            s3=sb.toString();
                            k=1;
                            s3=s3.substring(0, m)+"*"+s3.substring(m,s3.length());
                            count=0;
                            m=m-Math.min(a,b)+1;
                        }
                count=0;
  
                }
                    else{
                        s3=s3.substring(0, m)+"*"+s3.substring(m,s3.length());
                    count=0;
                    m=m-Math.min(a,b)+1;
                    }
            count=0;
    }
          
    }
      
  
    if(m>-1&&s3.charAt(m)=='b') {

        int count=0;
        for(int l=1;l<=b;l++){
            if((m-l)>-1&&s3.charAt(m-l)!='a'){
            if(s3.charAt(m-l)==s3.charAt(m)){
                count++;
          
            }
            else
            {
                count=0;
            }
          
            }}
            if(count==b){
                int kl=0;
                if((s3.indexOf('a',0))!=-1&&kl!=1){
                        StringBuilder sb=new StringBuilder(s3);
                        char l=s3.charAt(m);
                        int all=s3.lastIndexOf('a');
                        char r=s3.charAt(all);
                        sb.setCharAt(m,r);
                        sb.setCharAt(s3.lastIndexOf('a'),l);
                        s3=sb.toString();
                        if(    bb(s3.substring(m,s3.length()),a,b)==false){
                          
                            sb.setCharAt(m,s3.charAt(all));
                            sb.setCharAt(all,s3.charAt(m));
                            s3=sb.toString();
                            k=1;
                            s3=s3.substring(0, m)+"*"+s3.substring(m,s3.length());
                            count=0;
                            m=m-Math.min(a, b)+1;
                          
                        }
                    count=0;
              
              
                }
                    else{
                        s3=s3.substring(0, m)+"*"+s3.substring(m,s3.length());
                        count=0;
                        m=m-Math.min(a, b)+1;
                        count=0;
                    }
                    count=0;
                  
            }
        count=0;  
    }
  
}      
      

  
sf[i]=s3;


}
for(String s:sf){
  

System.out.println(s);}
  
    }
    static Boolean bb(String sd,int a,int b){
        Boolean bb=true;
        for(int m=sd.length()-1;m>=0;m--){
          
            if(sd.charAt(m)=='a'){
                int count=0;
                for(int l=1;l<=a;l++){
                    if((m-l)>-1){
                    if(sd.charAt(m-l)==sd.charAt(m)){
                        count++;
                      
                    }}}
                if(count==a){
                bb=false;
                    break;
                }
            }
            if(sd.charAt(m)=='b'){
              
                int count=0;
                for(int l=1;l<=b;l++){
                    if((m-l)>-1){
                    if(sd.charAt(m-l)==sd.charAt(m)){
                        count++;
                      
                    }}}
                if(count==b){
                bb=false;
              
                    break;
                }
            }
      
        }
      
        return bb;      
    }
}

Monday 9 October 2017

CodeChef October Chellange 2017 : Max Mex

PROBLEM:  You are given a multi-set S of N integers, and an integer K. You want to find the maximum value of minimal excluded non-negative integer (MEX) of the multi-set given that you are allowed to add at most any K integers to the multi-set. Find the maximum value of MEX that you can obtain.
Few examples of finding MEX of a multi-set are as follows. MEX of multi-set {0} is 1, {1} is 0, {0, 1, 3} is 2, {0, 1, 2, 3, 5, 6} is 4.

Input

The first line of the input contains an integer T denoting the number of testcases.
The first line of each test case contains two space seperated integers N and K denoting the size of the multi-set and the maximum number of extra integers that you can add in the multi-set respectively.
The second line contains N space separated integers denoting the multi-set S: S1, S2 ,.... SN.

Output

For each testcase, output the answer in a single line.

Constraints

  • 1T10
  • 1N105
  • 0K105
  • 0Si2 * 105

Subtasks

  • Subtask #1 (15 points): K=0.
  • Subtask #2 (85 points): Original Constraints.

Example

Input:
4
3 0
1 0 2
3 1
1 0 2
4 3
2 5 4 9
2 0
3 4

Output:
3
4
6
0

Explanation

Example case 1. As K = 0, so we can't add any element to the multi-set. Elements of the set are {1, 0, 2}. The MEX value of this set is 3.
Example case 2. As K = 1, you are allowed to add at most 1 element to the multi-set. The multi-set are {1, 0, 2}. You can add element 3 to the multi-set, and it becomes {1, 0, 2, 3}. The MEX value of this multi-set is 4. There is no other way to have higher value of MEX of the set by adding at most one element to the multi-set.



SOLUTION :   IN JAVA  

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Collections;
import java.util.ArrayList;

 class MaxMex {
  
public static void main(String []args){
try{

    ArrayList<Integer> al=new ArrayList<Integer>();
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(isr,10);
    String []line0=br.readLine().split(" ");
    int t=Integer.parseInt(line0[0]);
  
    for(int i=0;i<t;i++){
      
        BufferedReader br1=new BufferedReader(isr,100000);
        String []line1=br1.readLine().split(" ");
        int n=Integer.parseInt(line1[0]);
        int ad=Integer.parseInt(line1[1]);
        //ar1=new int[ad];
         ArrayList<Integer> ar=new ArrayList<Integer>();
         BufferedReader br2=new BufferedReader(isr,2*100000);
            String []line2=br2.readLine().split(" ");
        for(int j=0;j<n;j++){
          
            ar.add(Integer.parseInt(line2[j]));
        }
        //int max=0;
        // Iterator itr=ar.iterator();
         // while(itr.hasNext()){
            //  if(max<(int)itr.next()){
                // max=(int)itr.next();
             // }
          //}
        //  System.out.println(max);
    int max=Collections.max(ar);
        for(int j=1;j<=ad+1;){
          
        for(int k=0;k<=max+ad+1;k++){
            int count=0;
            Iterator itr1=ar.iterator();
            while(itr1.hasNext()){
            if(k==(int)itr1.next()){
                count+=1;
            }
            }
            if(count==0){
              
                if(j<ad+1){
                ar.add(k);}
                if(j==ad+1){
                    al.add(k);
                }
                j++;
            }
        }
    }
      
  
    }
    Iterator i=al.iterator();
    while(i.hasNext())
    System.out.println(i.next());
}
catch(IOException e){
  
}
    }

}

Friday 6 October 2017

CodeVita Season IV Round 1 : Accico Equi Pairs

Problem : Accico Equi Pairs

Ron Wesley has been bit by a three headed snake and Harry Potter is searching for a potion. The Witch promises to tell the ingredients of the medicine if Harry can find equi pair of an array. Listen to the conversation between Harry The witch to know more about equi pairs.
Conversation:-
The Witch : To find the equi pair, you must know how to find the slices first.
Harry         : What is a slice?
The Witch : If Z is an array with N elements, a slice of indices (X, Y) is Z[X] + Z[X+1]...Z[Y]
Harry         : How can I use it to find equi pair?
The Witch : (a, b) is an equi pair if slice of (0, a-1) = slice of (a+1, b-1) = slice of (b+1, N-1) and b>a+1 and size of array > 4
Input Format:
An array of N integers delimited by white space
Output Format:
Print equi pair in first line in the format { a,b }
Print slices in the format { 0,a-1 }, { a+1,b-1 }, { b+1,N-1 }

OR

Print "Array does not contain any equi pair" if there are no equi pairs in the array
Constraints:
Zi >= 0 and 1<= i <=N
size of array (N) > 4
b > (a+1) 
Sample Input and Output

SNo.InputOutput

1

8 3 5 2 10 6 7 9 5 2

Indices which form equi pair { 3,6 }
Slices are { 0,2 },{ 4,5 },{ 7,9 }

2

6 2 6 2 3 3 1 9

Array does not contain any equi pair
Note:
Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc
Note:
For C and C++, return type of main() function should be int.
 
 
SOLUTION :  IN JAVA
 
 
 import java.util.*;
public class AccicoPeris {
    public static void main(String []args){
    int n,count=0;String s,s1,s2;char[] ch;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
int []a=new int[n];
if(n<=4){
    System.out.println("invalid input");
}
else
{
for(int m=1;m<=n;m++){
    a[m-1]=sc.nextInt();
    if(a[m-1]<=0){
        System.out.println("invalid input");  
    }


for(int i=1;i<=n;i++){
    for(int j=(i+2);j<=n;j++){
        int sum1=0,sum2=0,sum3=0,count1=0,count2=0,count3=0;
  
      
        for(int k=0;k<i;k++){
             count1++;
             sum1+=a[k];
        }
        for(int k=i+1;k<=j-1;k++){
             count2++;
             sum2+=a[k];
        }
        for(int k=j+1;k<=n-1;k++){
            count3++;
             sum3+=a[k];
        }
        if(sum1==sum2&&sum2==sum3&&count1>1&&count2>1&&count3>1){
            System.out.println("Indices which form an equi pair { "+i+","+j+" } Slices are {0, "+(i-1)+"} , {"+(i+1)+","+(j-1)+"} , {"+(j+1)+","+(n-1)+"}");
            count++;
        }
        }  
}
}}
if(count==0){
    System.out.println("Array does not contain any equi pair.");
}
}
}
 
 

Tuesday 3 October 2017

TCS CodeVita 2015 Round 2 : Alpha Numeric Sorting

Given text comprising of words and numbers, sort them both in ascending order and print them in a manner that a word is followed by a number. Words can be in upper or lower case. You have to convert them into lowercase and then sort and print them.
Input Format:
First line contains total number of test cases, denoted by N
Next N lines, each contains a text in which words are at odd position and numbers are at even position and are delimited by space
Output Format:
Words and numbers sorted in ascending order and printed in a manner that a word is followed by a number.
Constraints:
1. Text starts with a word
2. Count of words and numbers are the same.
3. Duplicate elements are not allowed 
4. Words should be printed in lower case. 
5. No special characters allowed in text.
Solution in Java

import java.util.*;


public class StringAndNumeric {
public static void main(String []args){
   
    Scanner sc=new Scanner(System.in);
    String str=sc.nextLine();
    String [] str2=str.split(" ") ;
    int n=str2.length;
    String []str3=new String[n/2+1];
    String []str4=new String[n/2];
    int i,j=0,k=0;
for(i=0;i<n;i++){
    if(i%2==0){
    str3[j]=str2[i];
    j++;}
    else{
       
    str4[k]=str2[i];
    k++;}

}
Arrays.sort(str3);
Arrays.sort(str4);

k=0;int p=0;
for( int l=0;l<n;l++){
    if(l%2==0){
    str2[l]=str3[k];
    k++;}
    else{
        str2[l]=str4[p];p++;   
}}
for( i=0;i<n;i++)
System.out.println(str2[i]);
}}
Input: gghgh 67 asasa 34
Output:

asasa

34

gghgh

67

TCS CodeVita 2016 Round1 Question: Consecutive Prime Sum

Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example

5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.

Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.
Input Format:

First line contains a number N
Output Format:

Print the total number of all such prime numbers which are less than or equal to N.
 
SOLUTION IN JAVA
import java.util.*;
public class primesum {
public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt(),i,k,sum,count;
       
    for(i=3;i<=n;i++){
         sum=0;count=0;
        if(checkPrime(i)){
            for(k=2;k<i;k++){
                if(checkPrime(k)){
                sum+=k;
                count++;
                if(sum==i){
                    System.out.println(count+"\t"+i);
                break;   
                }
                }
            }
        }
       
    }}
    public static boolean checkPrime(int i){
        int j,count=0;
        boolean bo;
        int m=i/2;
        for( j=2;j<i;j++){
            if(i%j==0) {
                count++;
            break;
            }
        }
        if(count>0)
        {
            bo=false;
        }
        else bo=true;
        return bo;
    }
}

INPUT: 20
OUTPUT: 
2   5
4   17

TCS CodeVita 2016 Round1 Question: Min Product Array

The task is to find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2.

Note- the product sum is Summation (A[i]*B[i]) for all i from 1 to n where n is the size of both arrays
Input Format: 
  1. First line of the input contains n and k delimited by whitespace
  2. Second line contains the Array A (modifiable array) with its values delimited by spaces
  3. Third line contains the Array B (non-modifiable array) with its values delimited by spaces

Output Format:

Output the minimum sum of products of the two arrays
Constraints:
  1. 1 ≤ N ≤ 10^5
  2. 0 ≤ |A[i]|, |B[i]| ≤ 10^5
  3. 0 ≤ K ≤ 10^9




ANSWER:     SOLUTION IN JAVA.

 import java.util.*;
public class codevita {
    public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt(),i,j,k,r,min=0,min1=0,sum,sum1;
    System.out.print("\t");
     k=sc.nextInt();
     System.out.println();
int []a=new int[n];
int []b=new int[n];
for( i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println();
for( i=0;i<n;i++)
b[i]=sc.nextInt();

for(i=0;i<n;i++){
    sum=0;
    a[i]-=2*k;
    for(j=0;j<n;j++){
           
sum+=a[j]*b[j];

    }
    if(i==0)
        min=sum;
    a[i]+=2*k;
    if(min>=sum){
        min=sum;
    }
}
for(i=0;i<n;i++){
    sum1=0;
    a[i]+=2*k;
    for(j=0;j<n;j++){
           
sum1+=a[j]*b[j];

    }
    if(i==0)
        min1=sum1;
    a[i]-=2*k;
    if(min1>=sum1){
        min1=sum1;
    }
}
if(min<min1)
    System.out.println(min);
else System.out.println(min1);

sc.close();
}
}


INPUT:
3 4
3 4 5
5 6 7

OUTPUT: 18