CᴏᴅɪɴɢNᴇʀᴅ 💸🐾
99 subscribers
118 photos
3 videos
15 files
40 links
༗ हरे कृष्णा ༗
"Lost in my own cosmos 💫 | Coding Need Patience 🥀🐾"

❝Face the failure, Until the Failure fails to face you.❞

— Dreamer

» ChikuX69.netlify.app «

DM ? " @ChikuXBot " : 404;
Download Telegram
Output of both codes is »

1234
123
12
1


Find Out the Difference between both codes
🔥1
def diamond(n):
for i in range(1, n+1):
for j in range(1, n-i+1):
print(" ", end='')
for j in range(1, i+i):
print("* ", end='')
print('')
for i in range(n-1, 0, -1):
for j in range(1, n-i+1):
print(" ", end='')
for j in range(1, i+i):
print("* ", end='')
print('')


diamond(4)
Question 1: Java / C / C++ / Python / JavaScript
Question 2: Using Java Only..
You have given an array
{2, 3, 9, 6, 4, 5}


Find the total pairs can be formed using this array and also print that all.
Print numbers of total pair exist in this array too.


Question 3: Java / C / C++ / Python / JavaScript.

**Note : For Python Treat this array as list then solve it.
>> Write a program to print only next prime number. Example - input is 5 output is 7.


import math

def isPrime(n):
if n == 2:
return True
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
return False
return True

def main():
num = int(input("Enter a number: "))

while True:
num += 1
if isPrime(num) == True:
print(num)
break

main()
image_2024-01-20_15-34-44.png
129.8 KB
Question 3 : Java / C / C++ / Python / JavaScript.


Height = {4, 2, 0, 6, 3, 2, 5}
Question 3 >> Solution!


import java.math.*;
public class TrappingRainwater {
public static int trappedRainwater(int arr[]) {
int trappedWater = 0;
int[] leftMax = new int[arr.length];
int[] rightMax = new int[arr.length];

// Auxiliary array
// left max
leftMax[0] = arr[0];
for(int i=1; i<arr.length; i++) {
leftMax[i] = Math.max(arr[i], leftMax[i-1]);
}
// right max
rightMax[arr.length-1] = arr[arr.length-1];
for(int i=arr.length-2; i>=0; i--) {
rightMax[i] = Math.max(rightMax[i+1], arr[i]);
}
// calculating rainwater
for(int i=0; i<arr.length; i++) {
int waterLevel = Math.min(leftMax[i], rightMax[i]);
trappedWater += Math.max(0, waterLevel-arr[i]);
}
return trappedWater;
}

public static void main(String[] arg) {
int[] array = {4, 2, 0, 6, 3, 2, 5};
int res = trappedRainwater(array);
System.out.print("Total trapped rainwater for\ngiven array is " + res);
}
}
Question 3 >> Solution!

# Calculate the rain water trapped by the gaps between the buildings
height = [4, 2, 0, 6, 3, 2, 5]


def trapping_rainwater(arr):
trapped_water = 0
right_max = [0] * len(arr)
left_max = [0] * len(arr)

# Auxiliary array
# left max boundary calculation.
left_max[0] = arr[0]
for i in range(1, len(arr)):
left_max[i] = max(arr[i], left_max[i-1])

# right max boundary calculation.
right_max[len(arr)-1] = arr[len(arr)-1]
for i in range(len(arr)-2, -1, -1):
right_max[i] = max(right_max[i+1], arr[i])

# Calculate the trapped water.
for i in range(len(arr)-1):
water_level = min(left_max[i], right_max[i])
trapped_water += max(0, water_level - arr[i])
print(trapped_water)


trapping_rainwater(height)
Question 4 : Buy & Sell Stocks

You are given an array prices where prices[i] is the price of a given stock
on the i'th day. You want to maximize your profit by choosing a single day to
buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you
cannot achieve any profit, return 0.

price = {7, 1, 5, 3, 6, 4}

Using : Java / C / C++ / Python / JavaScript
Question 4 solution >>

public class Main {
public static void stocks(int arr[]) {
int maxProfit = 0;
int buyingPrice = Integer.MAX_VALUE;

// max profit
for(int i=0; i<arr.length; i++) {
if(buyingPrice < arr[i]) {
int profit = arr[i] - buyingPrice;
maxProfit = Math.max(maxProfit, profit);
} else {
buyingPrice = arr[i];
}
}
System.out.println("The Max Profit is " + maxProfit);
}
public static void main(String[] args) {
int arr[] = {7, 1, 5, 3, 6, 4};
stocks(arr);
}
}
Question 5 : Given an integer array nums, return true if any value appears at least twice in the
array, and return false if every element is distinct.

Example 1:
Input: nums = [1, 2, 3, 1]
Output: true

Example 2:
Input: nums = [1, 2, 3, 4]
Output: false

Using : Java / C / C++ / Python / JavaScript
Question 5 Solution »

public class DoubleElement {
public static boolean doubleElement(int[] arr) {
boolean res = false;
// outer loop
for(int i=0; i<arr.length; i++) {
for(int j=i+1; j<arr.length; j++) {
if(arr[i] == arr[j]) {
res = true;
break;
}
}
}
return res;
}
public static void main(String[] arg) {
int[] array = {1, 2, 3, 4};
int[] array2 = {1, 2, 3, 1};
System.out.println(doubleElement(array));
System.out.println(doubleElement(array2));
}
}
Question 5 Solution »

array = [1, 2, 3, 1]
array2 = [1, 2, 3, 4]
array3 = [1, 2, 3, 4, 5, 3]

def double_element(arr):
res = False
# outer loop
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] == arr[j]:
res = True
return res


print(double_element(array))
print(double_element(array2))
print(double_element(array3))
Question 6: Short an array in ascending order using bubble shorting algorithm.

array = {5, 4, 3, 2, 1}

Using : Java / C / C++ / Python / JavaScript
Question 6 Solution »

// shorting an array using bubble shorting algorithm.
// array = {5, 4, 3, 2, 1}
#include <stdio.h>

void bubbleShort(int array[], int N) {
for(int i=0; i<N-1; i++) {
for(int j=i+1; j<N; j++) {
if(array[i] > array[j]) {
array[i] = array[i] + array[j];
array[j] = array[i] - array[j];
array[i] = array[i] - array[j];
}
}
}
}
int main() {
int array[5] = {5, 4, 3, 2, 1};
int N = 5;
bubbleShort(array, N);
for(int i=0; i<N; i++) {
printf("%d ", array[i]);
}
}
Question 6 Solution »

// Bubble shorting
let array = [5, 4, 1, 3, 2];
console.log(array);
for(let i=0; i<array.length; i++) {
for(let j=i+1; j<array.length; j++) {
if (array[i] > array[j]) {
array[i] = array[i] + array[j];
array[j] = array[i] - array[j];
array[i] = array[i] - array[j];
}
}
}

console.log(array);
Question 6 Solution »

# bubble short
array = [5, 4, 1, 3, 2]

def bubble_short(arr):
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i] = arr[i] + arr[j]
arr[j] = arr[i] - arr[j]
arr[i] = arr[i] - arr[j]



bubble_short(array)
print(array)