Tuesday, September 28, 2021

compare triplets

 Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.

The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).

The task is to find their comparison points by comparing a[0] with b[0]a[1] with b[1], and a[2] with b[2].

  • If a[i] > b[i], then Alice is awarded 1 point.
  • If a[i] < b[i], then Bob is awarded 1 point.
  • If a[i] = b[i], then neither person receives a point.

Comparison points is the total points a person earned.

Given a and b, determine their respective comparison points.

Example

a = [1, 2, 3]
b = [3, 2, 1]

  • For elements *0*, Bob is awarded a point because a[0] .
  • For the equal elements a[1] and b[1], no points are earned.
  • Finally, for elements 2a[2] > b[2] so Alice receives a point.

The return array is [1, 1] with Alice's score first and Bob's second.

Function Description

Complete the function compareTriplets in the editor below.

compareTriplets has the following parameter(s):

  • int a[3]: Alice's challenge rating
  • int b[3]: Bob's challenge rating

Return

  • int[2]: Alice's score is in the first position, and Bob's score is in the second.

Input Format

The first line contains 3 space-separated integers, a[0]a[1], and a[2], the respective values in triplet a.
The second line contains 3 space-separated integers, b[0]b[1], and b[2], the respective values in triplet b.

Constraints

  • 1 ≤ a[i] ≤ 100
  • 1 ≤ b[i] ≤ 100

Sample Input 0

5 6 7
3 6 10

Sample Output 0

1 1

Explanation 0

In this example:

Now, let's compare each individual score:

  • , so Alice receives  point.
  • , so nobody receives a point.
  • , so Bob receives  point.

Alice's comparison score is , and Bob's comparison score is . Thus, we return the array .


Answer:

vector<int> compareTriplets(vector<int> a, vector<int> b) {
vector<int> count={0,0};
for(int i=0;i<a.size();i++)
{
    if(a[i]>b[i])
    {
        count[0]++;
       // count.push_back(counta);
    }
    else if(a[i]<b[i])
    {
        count[1]++;
        //count.push_back(countb);
    }
    else {
        
        count[0] = count[0];
        count[1] = count[1];
        //break;
    }
    
}

return count;
}

Monday, September 27, 2021

min max sum

 Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

The minimum sum is  and the maximum sum is . The function prints

Output:

16   24
Answer:
void miniMaxSum(vector<int> arr) {
  long summax=0,summin=0;
   int n = arr.size();
   sort(arr.begin(),arr.end());
   long min=arr[0];
   long max= arr[n-1];
   for(int i=0;i<n;i++)
   {
       summax = summax+arr[i];
   }
   min=summax-min;
   max=summax-max;
   cout<<max<<" "<<min;
}

Friday, September 24, 2021

Q find sum of diagonal elements in a matrix and then find the absolute difference of the sum and return it.

eg 

2  3  5

1  0  2

4  1  2

left diag sum = 2+0+2=4

right diag sum = 5+0+4=9

absol. diff. 9-4 =5 ans

     


 for(int j = 0; j < numInputs; j++){

    for(int k = 0; k < numInputs; k++){
        cin >> curInput;
        if(j == k){
            leftD += curInput;
        }
        if(j+k == (numInputs-1)){
            rightD += curInput;
        }
    }
}
ans = abs(leftD-rightD);

In java
public static int diagonalDifference(List<List<Integer>> arr) {
    int leftdiagonal = 0, rightdiagonal = 0;
for(int i = 0, j = arr.get(0).size()-1; i < arr.get(0).size(); i++, j--){
        leftdiagonal = leftdiagonal + arr.get(i).get(i);
        rightdiagonal = rightdiagonal + arr.get(i).get(j);
    }
    return Math.abs(leftdiagonal - rightdiagonal);
    }

Sunday, August 1, 2021

reverse a string

You are given a string s. You need to reverse the string.

Example 1:

Input:
s = Geeks
Output: skeeG


 #include<bits/stdc++.h>

using namespace std;



string reverseWord(string str);



int main() {

int t;

cin>>t;

while(t--)

{

string s;

cin >> s;

cout << reverseWord(s) << endl;

}

return 0;

}


// } Driver Code Ends



//User function Template for C++


string reverseWord(string str){

    int len = str.length()-1;

    int i=0;

    while(i<len)

    {

        char c = str[i];

        str[i] = str[len];

        str[len] = c;

        i++;

        len--;

    }

    

  return str;

}

find min max

 Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.

 

Answer:

#include <stdio.h>


struct pair {

    long long int min;

    long long int max;

};


struct pair getMinMax(long long int arr[], long long int n) ;


int main() {

    long long int t, n, a[100002], i;

    struct pair minmax;


    scanf("%lld", &t);

    while (t--) {

        scanf("%lld", &n);


        for (i = 0; i < n; i++) scanf("%lld", &a[i]);

        minmax = getMinMax(a, n);

        printf("%lld %lld\n", minmax.min, minmax.max);

    }

    return 0;

}// } Driver Code Ends



// User function Template for C


struct pair getMinMax(long long int arr[], long long int n) {

    struct pair minmax;

    if(n==1)  //if size of array is 1 them min and max will be same

    {

        minmax.min=arr[0];

        minmax.max=arr[0];

        

        return minmax;

    }

    //if more than one element present

    

    if(arr[0]>arr[1])

    {

        minmax.max=arr[0];

        minmax.min=arr[1];

    }

    else

    {

        minmax.max=arr[1];

        minmax.min=arr[0];

    }

    

    

    for(int i=2;i<n;i++)

    {

        if(arr[i]>minmax.max)

        {

            minmax.max=arr[i];

        }

    }

    for(int i=2;i<n;i++)

    {

        if(arr[i]<minmax.min)

        {

            minmax.min=arr[i];

        }

    }

    

    return minmax;

    //printf("%d",min);

    //printf("%d",max);


    

}

Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...