Electrostatic

 Coding Problem Keys 

 Electrostatics 

 Problem Statement 

Doug is fond of change, every now and then he tries to do new things. This time, he caught up with a rod comprising of negative (N) and positive (P) charges. He is asked to calculate the maximum net electrostatic field possible in the region due to the rod.

Note: Assume, Electrostatic Field = Total Charge * 100.

 Input Specification 

Input1: integer array denoting the magnitude of each charge
Input2: String denoting the nature of each charge, i-th entry represents a sign of charge at the i-th loccation in input1.
Input3: No. of charges it holds (Length of input1).

 Output Specification 

Return the maximum electrostatic field possible in the rod.

 Example Input/Output 1 

 Input 

Input1: {4,3,5}
Input2: PNP
Input3: 3

 Output 

Output: 600

 Explanation 

The maximum electric charge on the rod is 4 - 3 + 5 = 6 units, so the magnitude of the electric field would be 6 * 100 = 600.

 Example Input/Output 2 

 Input 

Input1: {2,3}
Input2: PN
Input3: 2

 Output 

Output: 100

 Explanation 

The maximum electric charge on the rod is 2 - 3 = 1 unit, so the magnitude of the electric field would be 1 * 100 = 100.

 Solution 

 Programming Language: JAVA Language 

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String s = sc.nextLine();
        int n = sc.nextInt();
        int charge[] = new int[n];
        int count = 0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)!=' '){
                charge[count] = str.charAt(i)-48;
                count++;
            }
        }
        int sum = 0;
        for(int i=0;i<n;i++){
            if(s.charAt(i)=='P')
                sum+=charge[i];
            else
                sum-=charge[i];
        }
        if(sum>0){
            System.out.print(sum*100);
        }else{
            System.out.print(-sum*100);
        }
    }
}

// Published By PKJCODERS

 Programming Language: Python 3 Language 

def electrostatic(a,b,n):
    for i in range(n):
        if(b[i])=='N':
            a[i]=a[i]*-1
    return abs(sum(a))*100
a=list(map(int,input().split()))
b=list(input().strip())
n=int(input())
print(electrostatic(a,b,n))

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Maximum Goods Transported

Swap and Reverse the String

Words Starting with Upper Case

Desktop Products

Matrix Print Excluding Border Elements

Last K Factors of N