New Question!!
Q) WAP to merge two array (each of size 10 numbers.)
RESTRICTIONS:-
•Not to use more than 2 loops
•Maximum variables you can use:-4 variables
NOTE:-
•Try to give shortest code possible.
• Spammers stay away
Answers
from array import array
arr_1, arr_2 = array("i", range(1, 11)), array("i", range(11, 21))
arr_3 = arr_1 + arr_2
print("arr_3", arr_3)
Required Answer:-
Question:
- Write a program to merge two arrays (each of 10 elements)
Restrictions Given:
- Not to use more than 2 loops.
- Maximum variables that can be used - 4.
Solution:
It's a good question. Read this post to get your answer.
Here is the code written in Java.
import java.util.*;
public class Challenge4me {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[10],b[]=new int[10], c[]=new int[20];
for(int i=0;i<40;i++) {
if(i<10) {
System.out.print("Element of array a at index "+i+": ");
a[i]=sc.nextInt();
c[i]=a[i];
}
else if(i<20) {
System.out.print("Elements of array b at index "+(i-10)+": ");
b[i-10]=sc.nextInt();
c[i]=b[i-10];
}
}
System.out.println(Arrays.toString(c));
}
}
Here is the code written in Python.
print("Enter elements in lists a and b providing spaces between each numbers..")
c=[]
print("Enter elements of a..")
a = list(map(int,input("Enter the number: ").strip().split()))[:10]
print("Enter elements of b...")
b=list(map(int,input("Enter the number: ").strip().split()))[:10]
c=[*a,*b]
print(c)
Output is attached. Logic is same but syntax differ in different languages.
Restrictions followed:
- I have used 1 loop in Java code and no loop in python code.
- Maximum variables I have used in Java code - 4, in Python code - 3
- (Hence Solved)