Skip to main content

Posts

Showing posts with the label microsoft

Wave Array | Convert an array to a wave array

 You have given a sorted array a[ ] of distinct integers and you have to sort the array in a wave-like pattern. It will be arranged like a[0]>=a[1]<=a[2]>=a[3]<=a[4]... This question has been asked in Google, Microsoft, Adobe, Amazon, Goldman Sachs, Paytm , and FactSet . Input: n=5 a[ ] = {1, 2, 3, 4, 5} Output: 2 1 4 3 5 So how can we solve this problem? If our array is already sorted then we have to just swap the array at the alternate elements. Like for a[ ] = {1, 2, 3, 4, 5} Loop1: i=0, a[ ] = {2, 1, 3, 4, 5} Loop2: i=2, a[ ] = {2, 1, 4, 3, 5} for Loop 3: i=4 which is not less than n-1 i.e. 4. So the loop ends here. The array generated is forming a wave pattern: {2, 1, 4, 3, 5}. Cpp code: #include   <bits/stdc++.h> using   namespace   std ; int   main () {      int  n;     cin  >>  n;      int  a[n];      for  ( int  i  ...

How to add 2 numbers without using an arithmetic operator?

 Guess you have to add two integers or numbers but without using the arithmetic operator. You cannot use any arithmetic operator like +, -, ++, --, ... etc. How will you do that? This question was asked in Microsoft Online Assessment. For solving this problem we have to go back to our Digital Electronics concepts where for adding two bits, we can do the XOR(^) of those bits. if they have a carry, we can find that out by applying AND(&) of them. So, this is the simple concept of Half Adder  which is used to add 2 single bits.  Input: 3 5 Output: 8 C++ code for this problem: #include   <bits/stdc++.h> using   namespace   std ; int   main () {      int  x, y, carry;     cin  >>  x  >>  y;      while  (y  !=   0 )     {         carry  =  x  &  ...