image_2021-10-23_16-51-13.png
28.5 KB
#N1512. Number of Good Pairs
problem link=>https://leetcode.com/problems/number-of-good-pairs/
#solution
problem link=>https://leetcode.com/problems/number-of-good-pairs/
#solution
class Solution {
public int numIdenticalPairs(int[] nums) {
int good=0;
for(int i=0; i<nums.length-1; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]==nums[j])
good++;
}
}
return good;
}
}image_2021-10-23_16-53-56.png
38.9 KB
#N1365. How Many Numbers Are Smaller Than the Current Number
problem link=>https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
#solution
problem link=>https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
#solution
class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {
int[] ans=new int[nums.length];
int count=0;
for(int i=0; i<nums.length; i++){
for(int j=0; j<nums.length; j++){
if(nums[i]>nums[j]&&j!=i){
count++;
}
}
ans[i]=count;
count=0;
}
return ans;
}
}image_2021-10-25_15-28-15.png
49.6 KB
#medium
#N1395. Count Number of Teams
problem link=>https://leetcode.com/problems/count-number-of-teams/
#solution
#N1395. Count Number of Teams
problem link=>https://leetcode.com/problems/count-number-of-teams/
#solution
class Solution {
public int numTeams(int[] rating) {
int res = 0;
for (int i = 1; i < rating.length - 1; ++i) {
int less[] = new int[2], greater[] = new int[2];
for (int j = 0; j < rating.length; ++j) {
if (rating[i] < rating[j])
++less[j > i ? 1 : 0];
if (rating[i] > rating[j])
++greater[j > i ? 1 : 0];
}
res += less[0] * greater[1] + greater[0] * less[1];
}
return res;
}
}image_2021-10-25_15-37-39.png
29.8 KB
#N1961. Check If String Is a Prefix of Array
problem link=>https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/
#solution
problem link=>https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/
#solution
class Solution {
public boolean isPrefixString(String s, String[] words) {
String allWords="";
for(int i=0; i<words.length; i++){
allWords+=words[i];
if(allWords.equals(s))
return true;
}
return false;
}
}image_2021-10-26_02-13-29.png
38.2 KB
#N1389. Create Target Array in the Given Order
problem link=>https://leetcode.com/problems/create-target-array-in-the-given-order/
#solution
problem link=>https://leetcode.com/problems/create-target-array-in-the-given-order/
#solution
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
int[] target = new int[nums.length];
int i = 0, k = 0;
while (i < index.length) {
for (k = target.length - 1; k > index[i]; k--)
target[k] = target[k - 1];
target[index[i]] = nums[i];
i++;
}
return target;
}
}
‼️P.s. Without ArrayLists. It would be simple and no-logic with ArrayListsimage_2021-10-26_12-57-51.png
34.9 KB
#N1528. Shuffle String
problem link=>https://leetcode.com/problems/shuffle-string/
#solution
problem link=>https://leetcode.com/problems/shuffle-string/
#solution
class Solution {
public String restoreString(String s, int[] indices) {
char[] ch=new char[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
char[] newch=new char[ch.length];
for(int i=0; i<indices.length;i++){
newch[indices[i]]=ch[i];
}
return new String(newch);
}
}image_2021-10-26_12-58-50.png
26.5 KB
#N1720. Decode XORed Array
problem link=>https://leetcode.com/problems/decode-xored-array/
#solution
problem link=>https://leetcode.com/problems/decode-xored-array/
#solution
class Solution {
public int[] decode(int[] encoded, int first) {
int[] arr=new int[encoded.length+1];
arr[0]=first;
for(int i=0; i<encoded.length; i++){
arr[i+1]=arr[i]^encoded[i];
}
return arr;
}
}image_2021-10-26_17-48-37.png
26.6 KB
#N961. N-Repeated Element in Size 2N Array
problem link=>https://leetcode.com/problems/n-repeated-element-in-size-2n-array/
#solution
problem link=>https://leetcode.com/problems/n-repeated-element-in-size-2n-array/
#solution
class Solution {
public int repeatedNTimes(int[] nums) {
int arr[]=new int[10000];
for(int i=0; i<nums.length; i++){
if(++arr[nums[i]]==2)
return nums[i];
}
return 0;
}
}image_2021-10-26_18-02-16.png
36.4 KB
#N942. DI String Match
problem link=>https://leetcode.com/problems/di-string-match/
#solution
problem link=>https://leetcode.com/problems/di-string-match/
#solution
class Solution {
public int[] diStringMatch(String s) {
char[] ch=s.toCharArray();
int[] arr=new int[ch.length+1];
int I0=0, Dn=ch.length;
for(int i=0; i<ch.length; i++){
if(ch[i]=='I')
arr[i]=I0++;
if(ch[i]=='D')
arr[i]=Dn--;
}
arr[arr.length-1]=I0;
return arr;
}
}image_2021-10-27_11-17-19.png
36.7 KB
#N1748. Sum of Unique Elements
problem link=>https://leetcode.com/problems/sum-of-unique-elements/
#solution
problem link=>https://leetcode.com/problems/sum-of-unique-elements/
#solution
class Solution {
public int sumOfUnique(int[] nums) {
int countFrequency[]=new int[101];
for(int num:nums){
++countFrequency[num];
}
int sum=0;
for(int i=0; i<countFrequency.length; i++){
if(countFrequency[i]==1)
sum+=i;
}
return sum;
}
}image_2021-10-27_15-23-19.png
35.2 KB
#N9. Palindrome Number
problem link=>https://leetcode.com/problems/palindrome-number/
#solution
problem link=>https://leetcode.com/problems/palindrome-number/
#solution
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int num=x;
int palindrome=0;
int temp=0;
while(true){
temp=x%10;
x/=10;
palindrome=palindrome*10+temp;
if(x==0)
break;
}
return num==palindrome;
}
}image_2021-10-27_17-05-48.png
56.5 KB
#N13. Roman to Integer
problem link=>https://leetcode.com/problems/roman-to-integer/
#solution
problem link=>https://leetcode.com/problems/roman-to-integer/
#solution
class Solution {
public int romanToInt(String s) {
int nums[]=new int[s.length()];
int temp=0;
int num=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='I') nums[i]=1;
if(s.charAt(i)=='V') nums[i]=5;
if(s.charAt(i)=='X') nums[i]=10;
if(s.charAt(i)=='L') nums[i]=50;
if(s.charAt(i)=='C') nums[i]=100;
if(s.charAt(i)=='D') nums[i]=500;
if(s.charAt(i)=='M') nums[i]=1000;
}
for(int i=0; i<nums.length-1; i++){
if(nums[i]<nums[i+1]){
num-=nums[i];
}else
num+=nums[i];
}
num+=nums[nums.length-1];
return num;
}
}image_2021-10-28_01-48-53.png
41.1 KB
#N67. Add Binary
problem link=>https://leetcode.com/problems/add-binary/
#solution
problem link=>https://leetcode.com/problems/add-binary/
#solution
class Solution {
public String addBinary(String a, String b) {
int lena = a.length();
int lenb = b.length();
int i =0, qarz = 0;
String res = "";
while(i<lena || i<lenb || qarz!=0){
int x = (i<lena) ? Integer.parseInt(""+a.charAt(lena - 1 - i)) : 0;
int y = (i<lenb) ? Integer.parseInt(""+b.charAt(lenb - 1 - i)) : 0;
res = (x + y + qarz)%2 + res;
qarz = (x + y + qarz)/2;
i++;
}
return res;
}
}❤1
image_2021-10-28_02-50-24.png
35.6 KB
#N69. Sqrt(x)
problem link=>https://leetcode.com/problems/sqrtx/
#solution
problem link=>https://leetcode.com/problems/sqrtx/
#solution
class Solution {
public int mySqrt(int x) {
if (x == 0) return 0;
int start = 1, end = x;
while (start < end) {
int mid = (start + end) / 2;
if (mid <= x / mid && (mid + 1) > x / (mid + 1))
return mid;
else if (mid > x / mid)
end = mid;
else
start = mid + 1;
}
return start;
}
}image_2021-10-28_03-22-49.png
26 KB
#N70. Climbing Stairs
problem link=>https://leetcode.com/problems/climbing-stairs/
#solution
problem link=>https://leetcode.com/problems/climbing-stairs/
#solution
class Solution {
public int climbStairs(int n) {
int fib=0, x=1, y=1;
for(int i=1; i<n; i++){
fib=y;
y=x+y;
x=fib;
}
return y;
}
}image_2021-10-29_01-38-15.png
30.7 KB
#N2037. Minimum Number of Moves to Seat Everyone
problem link=>https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
#solution
problem link=>https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
#solution
class Solution {
public int minMovesToSeat(int[] seats, int[] students) {
Arrays.sort(seats);
Arrays.sort(students);
int balance=0;
for(int i=0; i<seats.length; i++){
balance+=Math.abs(students[i]-seats[i]);
}
return balance;
}
}image_2021-10-29_01-40-43.png
37.5 KB
#N905. Sort Array By Parity
problem link=>https://leetcode.com/problems/sort-array-by-parity/
#solution
problem link=>https://leetcode.com/problems/sort-array-by-parity/
#solution
class Solution {
public int[] sortArrayByParity(int[] nums) {
int temp=0;
for(int i=0; i<nums.length-1; i++){
for(int j=0; j<nums.length-i-1; j++){
if(nums[j]%2==1&&nums[j+1]%2==0){
temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
return nums;
}
}image_2021-10-29_01-46-19.png
24.2 KB
#N561. Array Partition I
problem link=>https://leetcode.com/problems/array-partition-i/
#solution
problem link=>https://leetcode.com/problems/array-partition-i/
#solution
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum=0;
for(int i=0; i<nums.length; i=i+2){
sum+=nums[i];
}
return sum;
}
}image_2021-10-29_14-53-36.png
39.9 KB
#N704. Binary Search
problem link=>https://leetcode.com/problems/binary-search/
#solution
problem link=>https://leetcode.com/problems/binary-search/
#solution
class Solution {
public int search(int[] nums, int target) {
int left=0, right=nums.length-1, mid;
while(left<=right){
mid=(left+right)/2;
if(nums[mid]<target){
left=mid+1;
}
if(nums[mid]>target){
right=mid-1;
}
if(nums[mid]==target){
return mid;
}
}
return -1;
}
}
‼️Implementation of Bineary Search to the problemsimage_2021-10-29_15-02-21.png
33.8 KB
#N1346. Check If N and Its Double Exist
problem link=>https://leetcode.com/problems/check-if-n-and-its-double-exist/
#solution
problem link=>https://leetcode.com/problems/check-if-n-and-its-double-exist/
#solution
class Solution {
public boolean checkIfExist(int[] arr) {
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr.length; j++){
if(arr[i]==2*arr[j]&&i!=j)
return true;
}
}
return false;
}
}