Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | Sum of Uncommon Elements | CodeWindow

Sum of Uncommon Elements

The function accepts two integer arrays ‘arr’ and ‘arr2’ of sizes n and m respectively as its argument. Implement the function to find and return the sum of all uncommon elements in two arrays (elements which are present in only one of the array).
Note:
Return -1 if both arrays are null (None in the case of Python).
If one of the arrays is null then return the sum of all elements of the other array.

Example 1:
Input 1: 9 -4 3 2 -5
Input 2: 2 -5 7 9

Output: 6
Explanation:
Uncommon elements of two arrays are -4,3 and 7. Sum of uncommon elements = -4 + 3 + 7 = 6. Thus, output is 6.

Solution in Python 3:

#https://codewindow.in
# Join our Telegram Channel

arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
a=[]
for i in arr1:
    if i in arr2:
        pass
    else:
        a.append(i)

for j in arr2:
    if j in arr1:
        pass
    else:
        a.append(j)
print(sum(a))

# Telegram @codewindow

Solution in JAVA :

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

import java.io.*;

class CodeWindow {
    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");
        int a[] = new int[str.length];
        for(int i=0;i<str.length;i++)
            a[i] = Integer.parseInt(str[i]);
        
        str = br.readLine().split(" ");
        int b[] = new int[str.length];
        for(int i=0;i<str.length;i++)
            b[i] = Integer.parseInt(str[i]);
        int sum = 0;
        for(int i=0;i<a.length;i++) {
            int flag = 1;
            for(int j=0;j<b.length;j++) {
                if(a[i] == b[j]) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 1)
                sum+=a[i];
        }
        for(int i=0;i<b.length;i++) {
            int flag = 1;
            for(int j=0;j<a.length;j++) {
                if(b[i] == a[j]) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 1)
                sum+=b[i];
        }
        System.out.println(sum);
    }
}

Solution in C++ :

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

#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    vector<int> a;
    vector<int> b;
    string s;
    getline(cin,s);
    for(int i=0;i<s.length();i++)
        if(s[i] != ' ')
            a.push_back(s[i]-'0');
    getline(cin,s);
    for(int i=0;i<s.length();i++)
        if(s[i] != ' ')
            b.push_back(s[i]-'0');
    int sum=0;
    for(int i=0;i<a.size();i++) {
        int flag = 1;
        for(int j=0;j<b.size();j++) {
            if(a[i] == b[j]) {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
            sum += a[i];
    }
    for(int i=0;i<b.size();i++) {
        int flag = 1;
        for(int j=0;j<b.size();j++) {
            if(b[i] == a[j]) {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
            sum += b[i];
    }
    cout << sum;
    return 0;
}

Output:

4 5 9 8 7
8 5 6 2
28
9 -4 3 2 -5
2 -5 7 9
6
Recent Posts
Pages