Difference between revisions of "Neet Code Problems"

From ZCubes Wiki
Jump to navigation Jump to search
Line 158: Line 158:
  
 
This approach checks for lengths greater than 1 byrow, and cumuluate with OR over the entire array.
 
This approach checks for lengths greater than 1 byrow, and cumuluate with OR over the entire array.
 +
 +
Another solution:
 +
n=[1,2,3,1];
 +
∏((n⍣)#1n)>1
 +
 +
Answer: true
 +
 +
∏ Product
 +
n⍣ gives FREQUENCY of each element of n
 +
#1n extracts all elements from column 1 (note use of 1n for column n)
 +
>1 Checking if the product is greater than 1
  
 
=Other Interesting Solutions=
 
=Other Interesting Solutions=

Revision as of 11:54, 12 September 2024

Neet Code Solutions

Thinking in z^3

This is a quick introduction to the z^3 language (pronounced as "zcubes language").

ZCubes Language (z^3) is an easy to write, natural to read, omni-functional language.

Learn z^3 Language in detail by clicking this link.

Introduction

Following are z^3 Solutions to YouTube Code Report Channel Videos. To learn how to code in ZCubes language, please visit [https://coding.zcubes.com]. ZCubes Web Platform can be started in Code Mode at [https://code.zcubes.com].

Problems and Solutions

Product of Array Except Self

Video: https://www.youtube.com/watch?v=bNvIQI2wAjk [Product of Array Except Self - Leetcode 238 - Python]

Problem Description:

Given an integer array nums, return an array output where output[i] is the product of all the elements of nums except nums[i].

Each product is guaranteed to fit in a 32-bit integer.

Follow-up: Could you solve it in O ( n ) O(n) time without using the division operation?

Example 1:

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

Output: [48,24,12,8] Example 2:

Input: nums = [-1,0,1,2,3]

Output: [0,-6,0,0,0]

z^3 Solution

n=[1,2,4,6];
n⪥n≡∏

Gives answer

24
12
48
24


n=[-1,1,0,-3,3];
n⪥n≡∏

Gives answer:


0
0
0
0
0


Notes: ⪥ is the .around operator on an array, which takes one or more indices from the second argument to give you an array except that index. ≡∏ Product (∏) conducted by row (≡)

n⪥n can be written as n.around(n) also.


Duplicate Integer

Video: https://www.youtube.com/watch?v=3OamzN90kPg [Contains Duplicate - Leetcode 217 - Python]

Problem Description:

Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.

Example 1:

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

Output: true Example 2:

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

Output: false

z^3 Solution

n=[1,2,3,1];
(n∪).$(x=>n⒤x)

Gives answer

0 3
1
2


Notes: n∪ Uniques of n array .$ Apply function to each element x=>n⒤x Function x, defined as n find indices that contain x ≡ can be used to loop to check greater than 1 etc

n=[1,2,3,1];
((n∪).$(x=>n⒤x)≡(x=>x#>1))▣OR

This approach checks for lengths greater than 1 byrow, and cumuluate with OR over the entire array.

Another solution: n=[1,2,3,1]; ∏((n⍣)#1n)>1

Answer: true

∏ Product n⍣ gives FREQUENCY of each element of n

  1. 1n extracts all elements from column 1 (note use of 1n for column n)

>1 Checking if the product is greater than 1

Other Interesting Solutions

See Related