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 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.
Using : Java / C / C++ / Python / JavaScript
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)
Question 6 Solution »
// bubble shorting
public class BubbleShort {
public static void bubbleShort(int[] arr) {
// outer loop
for(int i=0; i<arr.length; i++) {
// inner loop
for(int j=i+1; j<arr.length; j++) {
if(arr[i] > arr[j]) {
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
}
}
public static void main(String[] arg) {
int[] array = {5, 4, 1, 3, 2};
bubbleShort(array);
for(int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
Question 6 Solution »
// bubble short
#include <iostream>
using namespace std;
void bubble(int array[], int N) {
// outer loop
for(int i=0; i<N-1; i++) {
// inner loop
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 arr[5] = {5, 4, 1, 3, 2};
int N = 5;
bubble(arr, N);
for(int i=0; i<N; i++) {
cout << arr[i] << " ";
}
return 0;
}
👍2❤1
// identifying odd or even number
import java.util.Scanner;
public class IsOddEven {
public static void isOddEven(int num) {
if((num & 1) == 1) {
System.out.print(num + " is odd number");
} else {
System.out.print(num + " is even number");
}
}
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int number = sc.nextInt();
isOddEven(number);
}
}
❤1
# check number given by user is even or odd
def is_odd_even(num):
if (num & 1) == 1:
print(f"{num} is a odd number.")
else:
print(f"{num} is an even number.")
print(is_odd_even(int(input("Enter a number: "))))
❤1
// Recursion logic : Factorial of N numbers...
public static int fact(int num) {
if(num == 1) {
return 1;
} else {
return num * fact(num - 1);
}
}
👍1
// Removing duplicate characters from string...
public class DuplicateString {
public static void removeDuplicate(String str, int idx, StringBuilder newStr, boolean[] map) {
if(idx == str.length()) {
System.out.print(newStr);
return;
}
char chr = str.charAt(idx);
if (map[chr - 'a'] == true) {
removeDuplicate(str, idx+1, newStr, map);
} else {
map[chr - 'a'] = true;
removeDuplicate(str, idx+1, newStr.append(chr), map);
}
}
public static void main(String[] args) {
String str = "hello";
removeDuplicate(str, 0, new StringBuilder(""), new boolean[26]);
}
}
👍1
» Question 7..
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
int* res = (int*)malloc(2 * sizeof(int));
*returnSize = 2;
for(int i=0; i<numsSize-1; i++) {
for(int j=i+1; j<numsSize; j++) {
if(nums[i] + nums[j] == target) {
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
};
Question 7..
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::vector<int> res;
for(int i=0; i<nums.size()-1; i++) {
for(int j=i+1; j<nums.size(); j++) {
if(nums[i] + nums[j] == target) {
res.push_back(i);
res.push_back(j);
return res;
}
}
}
return res;
}
};