Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1: Given a number n , print triangular pattern. * * * * * * * * * * * * * * *

Question 1:

Given a number n , print triangular pattern.

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

Complete the below two methods. You can print the pattern using any time complexity in PrintPatternAnyComplexity(int n) where as in PrintPatternLinearComplexity(int n) the time complexity should be linear i.e O(n) .

public static void PrintPatternAnyComplexity(int n)

public static void PrintPatternLinearComplexity(int n)

Question 2:

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Input: [1,3,5,4,7]

Output: 3

Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.

Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

Input: [2,2,2,2,2]

Output: 1

Explanation: The longest continuous increasing subsequence is [2], its length is 1.

Note: Algorithms runtime complexity must be O(n).

public int LongestSubSeq(int[] nums)

Question 3:

Given an array of integers greater than zero, find if it is possible to split it in two subarrays (without reordering the elements), such that the sum of the two subarrays is the same. Print the two subarrays.

Input : Array = [ 1 , 2 , 3 , 4 , 5 , 5 ]

Output : [ 1, 2 ,3, 4 ]

[ 5 , 5 ]

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

Output : [4, 1]

[2, 3]

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

Output : False

Note: Algorithms runtime complexity must be O(n).

public static void PrintTwoParts(int[] arr1)

Question 4:

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]

Output: [0,1,9,16,100]

Example 2:

Input: [-7,-3,2,3,11]

Output: [4,9,9,49,121]

Note:

  • 1 <= A.length <= 10000
  • -10000 <= A[i] <= 10000
  • A is sorted in non-decreasing order.
  • The time complexity of the method should not exceed O(n)

public static int[] SortedSquares(int[] A)

Question 5:

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [4,2,2,4], nums2 = [2,2]

Output: [2,2]

Example 2:

Input: nums1 = [3,6,2], nums2 = [6,3,6,7,3]

Output: [3,6]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Note: Algorithms runtime complexity must be in the order of O(n).

Hint : You can make use of a dictionary to obtain the required time complexity.

public static int[] Intersect(int[] nums1, int[] nums2)

Question 6:

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

Example 1:

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

Output: true

Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

Input: arr = [1,2]

Output: false

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]

Output: true

Note: The time complexity of the method should not exceed O(n)

Hint: Make use of dictionaries to attain the time complexity.

public bool UniqueOccurrences(int[] arr)

Question 7:

Given a non-decreasing integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.

Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,

Output: ["2", "4->49", "51->74", "76->99"]

Note: Handle all the corner cases. Example: return empty array if given array contains all the elements from the ranges or return ["0->99"] if array is empty.

public List Ranges(int[] nums, int lower, int upper)

Question 8:

Given an array of strings names of size n. You will create output string array which stores folder names.

Since two files cannot have the same name, if you enter a folder name which is previously used, the system will have a suffix addition to its name in the form of (k) where, k is the smallest positive integer such that the obtained name remains unique.

Return an array of strings of length n where the names of the files are according to the assignments by the system. Carefully look at the below examples to understand the problem statement.

Example 1:

Input: names = ["pes","fifa","gta","pes(2019)"]

Output: ["pes","fifa","gta","pes(2019)"]

Explanation: Let's see how the file system creates folder names:

"pes" --> not assigned before, remains "pes"

"fifa" --> not assigned before, remains "fifa"

"gta" --> not assigned before, remains "gta"

"pes(2019)" --> not assigned before, remains "pes(2019)"

Example 2:

Input: names = ["gta","gta(1)","gta","avalon"]

Output: ["gta","gta(1)","gta(2)","avalon"]

Explanation: Let's see how the file system creates folder names:

"gta" --> not assigned before, remains "gta"

"gta(1)" --> not assigned before, remains "gta(1)"

"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"

"avalon" --> not assigned before, remains "avalon"

Example 3:

Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]

Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]

Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".

Example 4:

Input: names = ["wano","wano","wano","wano"]

Output: ["wano","wano(1)","wano(2)","wano(3)"]

Explanation: Just increase the value of k each time you create folder "wano".

public string[] UniqFolderNames(string[] names)

Hint: Make use of dictionaries.

Program.cs

using System;
using System.Collections.Generic;
 
namespace Assignment2_Fall2020
{
 class Program
 {
 
 
 static void Main(string[] args)
 {
 Console.WriteLine("Question 1");
 int n = 7;
 PrintPatternAnyComplexity(n);
 PrintPatternLinearComplexity(n);
 
 
 Console.WriteLine("Question 2");
 int[] array1 = new int[] { 1, 3, 5, 4, 7 };
 int result = LongestSubSeq(array1);
 Console.WriteLine(result);
 
 Console.WriteLine("Question 3");
 int[] array2 = new int[] { 1, 2, 3, 4, 5, 5 };
 PrintTwoParts(array2);
 
 
 Console.WriteLine("Question 4");
 int[] array3 = new int[] { -4, -1, 0, 3, 10 };
 int[] result2 = SortedSquares(array3);
 //Write code to print the result array here
 
 Console.WriteLine("Question 5");
 int[] nums1 = { 4, 2, 2, 4 };
 int[] nums2 = { 2, 2 };
 int[] intersect1 = Intersect(nums1, nums2);
 //Write code to print the result array here
 
 
 Console.WriteLine("Question 6");
 int[] arr = new int[] { 1, 2, 2, 1, 1, 3 };
 Console.WriteLine(UniqueOccurrences(arr));
 
 Console.WriteLine("Question 7");
 int[] numbers = { 0, 1, 3, 50, 75 };
 int lower = 0;
 int upper = 99;
 List ResultList = Ranges(numbers, lower, upper);
 //Write code to print list here
 
 Console.WriteLine("Question 8");
 string[] names = new string[] { "pes", "fifa", "gta", "pes(2019)" };
 string[] namesResult = UniqFolderNames(names);
 //Write code to print your result here
 
 
 }
 
 public static void PrintPatternAnyComplexity(int n)
 
 {
 try
 {
 //Write your code here;
 }
 catch (Exception)
 {
 
 throw;
 }
 
 }
 public static void PrintPatternLinearComplexity(int n)
 
 {
 try
 {
 //Write your code here;
 }
 catch (Exception)
 {
 
 throw;
 }
 
 }
 
 
 public static int LongestSubSeq(int[] nums)
 {
 try
 {
 //write your code here 
 }
 catch (Exception)
 {
 
 throw;
 }
 return 0;
 }
 public static void PrintTwoParts(int[] array2)
 {
 try
 {
 //Write your code here;
 }
 catch (Exception)
 {
 
 throw;
 }
 
 }
 
 public static int[] SortedSquares(int[] A)
 {
 try
 {
 //Write Your Code Here
 }
 catch (Exception)
 {
 
 throw;
 }
 return null;
 }
 public static int[] Intersect(int[] nums1, int[] nums2)
 {
 try
 {
 // Write your code here
 }
 catch
 {
 throw;
 }
 
 return new int[] { };
 }
 
 
 public static bool UniqueOccurrences(int[] arr)
 {
 try
 {
 //Write your code here;
 }
 catch (Exception)
 {
 
 throw;
 }
 return default;
 }
 public List Ranges(int[] numbers, int lower, int upper)
 {
 try
 {
 //Write Your Code Here
 }
 catch (Exception)
 {
 
 throw;
 }
 return default;
 }
 public static string[] UniqFolderNames(string[] names)
 {
 try
 {
 //Write Your Code Here
 }
 catch (Exception)
 {
 
 throw;
 }
 return default;
 }
 
 }
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions

Question

What lessons in intervention design, does this case represent?

Answered: 1 week ago