Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Question

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Dear students, this is C++ array and structure coding challenge.

Array Problem
1. Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3

Output: [2,3,5,4,1,7]

Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is
[2,3,5,4,1,7].

2. here is a programming language with only four operations and one variable X:
● ++X and X++ increments the value of the variable X by 1.
● --X and X-- decrements the value of the variable X by 1.

Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of
X after performing all the operations.

Example 1:

Input: operations = ["--X","X++","X++"]

Output: 1

Explanation: The operations are performed as follows:

Initially, X = 0.

--X: X is decremented by 1, X = 0 - 1 = -1.

X++: X is incremented by 1, X = -1 + 1 = 0.

X++: X is incremented by 1, X = 0 + 1 = 1.
3. Given an array nums. We define a running sum of an array as runningSum[i] =
sum(nums[0]…nums[i]).

Return the running sum of nums.

Example 1:

Input: nums = [1,2,3,4]

Output: [1,3,6,10]

Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

4. You are given an m x n integer grid accounts where accounts[i][j] is the amount of
money the i​​th​​customer has in the j​​th​​bank. Return the wealth that the richest
customer has. A customer's wealth is the amount of money they have in all their
bank accounts. The richest customer is the customer that has the maximum wealth.

Example 1:

Input: accounts = [[1,2,3],[3,2,1]]

Output: 6

Explanation:

1st customer has wealth = 1 + 2 + 3 = 6

2nd customer has wealth = 3 + 2 + 1 = 6

Both customers are considered the richest with a wealth of 6 each, so return 6.

5. A sentence is a list of words that are separated by a single space with no leading or
trailing spaces.

You are given an array of strings sentences, where each sentences[i] represents a
single sentence.

Return the maximum number of words that appear in a single sentence.

Example 1:

Input: sentences = ["alice and bob love leetcode", "i think so too", "this is great
thanks very much"]

Output: 6
Explanation:

- The first sentence, "alice and bob love leetcode", has 5 words in total.

- The second sentence, "i think so too", has 4 words in total.

- The third sentence, "this is great thanks very much", has 6 words in total.

Thus, the maximum number of words in a single sentence comes from the third
sentence, which has 6 words.

6. There are n kids with candies. You are given an integer array candies, where each
candies[i] represents the number of candies the ith kid has, and an integer
extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith
kid all the extraCandies, they will have the greatest number of candies among all the
kids, or false otherwise.

Note that multiple kids can have the greatest number of candies.

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3

Output: [true,true,true,false,true]

Explanation: If you give all extraCandies to:

- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.

- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.

- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.

- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

7. You are given a positive integer array nums.


● The element sum is the sum of all the elements in nums.
● The digit sum is the sum of all the digits (not necessarily distinct) that appear in
nums.

Return the absolute difference between the element sum and digit sum of nums.
Note that the absolute difference between two integers x and y is defined as |x - y|

Example 1:

Input: nums = [1,15,6,3]

Output: 9

Explanation:

The element sum of nums is 1 + 15 + 6 + 3 = 25.

The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16.

The absolute difference between the element sum and digit sum is |25 - 16| = 9.

8. Given the array nums, for each nums[i] find out how many numbers in the array
are smaller than it. That is, for each nums[i] you have to count the number of valid
j's such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]

Output: [4,0,1,1,3]

Explanation:

For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

For nums[1]=1 does not exist any smaller number than it.

For nums[2]=2 there exist one smaller number than it (1).

For nums[3]=2 there exist one smaller number than it (1).

For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

9. You are given an array items, where each items[i] = [typei, colori, namei] describes
the type, color, and name of the ith item. You are also given a rule represented by
two strings, ruleKey and ruleValue.

The ith item is said to match the rule if one of the following is true:

● ruleKey == "type" and ruleValue == typei.


● ruleKey == "color" and ruleValue == colori.
● ruleKey == "name" and ruleValue == namei.

Return the number of items that match the given rule.

Example 1:

Input: items =
[["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iph
one"]], ruleKey = "color", ruleValue = "silver"

Output: 1

Explanation: There is only one item matching the given rule, which is
["computer","silver","lenovo"].

Structure problem
1. Write a structure to store the roll no., name, age (between 11 to 14) and
address of students (more than 10). Store the information of the
students.

1 - Write a function to print the names of all the students having age 14.

2 - Write another function to print the names of all the students having
even roll no.

3 - Write another function to display the details of the student whose roll
no is given (i.e. roll no. entered by the user).

2. Write a program to compare two dates entered by user. Make a structure


named Date to store the elements day, month and year to store the dates.
If the dates are equal, display "Dates are equal" otherwise display "Dates
are not equal".

You might also like