Accenture Coding Question | Electrostatic Field | CodeWindow

Electrostatic

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:
Input 1: Integer array denoting the magnitude of each charge.
Input 2: String denoting nature of each charge ith represents a sign of charge at ithentry represents a sign of charge at ith location in input1
Input 3: No of charges it holds (length of input1)

Output Specification:
Return the next maximum electrostatic field possible in the rod.

Example 1:
Input 1: {4,3,5}
Input 2: PNP
Input 3: 3

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 2:
Input 1: {2,3}
Input 2: PN
Input 3: 2

Output : 100
Explanation:
The maximum possible electric charge on the section of the rod is 2-3=-1 unit.

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

c=list(map(int, input().split()))
e=input()
l=int(input())
e=list(e)
#print(c)
#print(e)
count=0
for i in range(l):
    if e[i]=='P':
        count=count+c[i]
    else:
        count=count-c[i]
        
print(abs(count*100))

# Telegram @codewindow

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100];
    gets(a);
    char s[100];
    gets(s);
    int n;
    scanf("%d",&n);
    int charge[n];
    int count = 0;
    for(int i=0;i<strlen(a);i++){
        if(a[i] != ' ') {
            charge[count] = a[i]-48;
            count++;
        }
    }
    int sum = 0;
    for(int i=0;i<n;i++) {
        if(s[i] == 'P')
            sum += charge[i];
        else
            sum -= charge[i];
    }
    if(sum > 0) {
        printf("%d",sum*100);
    }
    else {
        printf("%d",-sum*100);
    }
}

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    char a[100];
    cin.getline(a,100);
    char s[100];
    cin.getline(s,100);
    int n;
    cin >> n;
    int charge[n];
    int count = 0;
    for(int i=0;i<strlen(a);i++){
        if(a[i] != ' ') {
            charge[count] = a[i]-48;
            count++;
        }
    }
    int sum = 0;
    for(int i=0;i<n;i++) {
        if(s[i] == 'P')
            sum += charge[i];
        else
            sum -= charge[i];
    }
    if(sum > 0) {
        cout << sum*100;
    }
    else {
        cout << -sum*100;
    }
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;

class CodeWindow {
    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.println(sum*100);
        }
        else {
            System.out.println(-sum*100);
        }
    }
}

Output:

4 3 5
PNP
3
600
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *