• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

CodeChef’ RRCOPY

July 26, 2014 by Dhaval Dave

Def: 
You had an array of integer numbers. You also had a beautiful operations called “Copy-Paste” which allowed you to copy any contiguous sub-sequence of your array and paste it in any position of your array. For example, if you have array [1, 2, 3, 4, 5] and copy it’s sub-sequence from the second to the fourth element and paste it after the third one, then you will get [1, 2, 3, 2, 3, 4, 4, 5] array. You remember that you have done a finite(probably zero) number of such operations over your initial array and got an array A as a result. Unfortunately you don’t remember the initial array itself, so you would like to know what could it be. You are interested by the smallest such array. So the task is to find the minimal size(length) of the array that A can be obtained from by using “Copy-Paste” operations.
PS : This is CodeChef’s problemInput

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of elements in obtained array A. The second line contains N space-separated integers A1, A2, …, AN denoting the array.

Example :
5 , 1 1 1 1 1
5 , 1 2 3 1 2

Output:
1
3

Explanation :
What we want here is to get exactly the count of unique number.
One way is to do with Two loops, for each number check whole string.
Second way is to create hash for each individual number and keep count.
Third and simple way is to keep a hash kind of array, for each single occurrence of a number w’ll just mark it True. and keep the counter.

Thanks to Dhaval for suggesting this approach and Code

Code :


#include <stdio.h>
#include <stdbool.h>

int rrcopy(int n, int a[]){
 int i=0,ans=0;
 bool c[100001]={0}; ans=0;
 for(i=0;i<n;i++){
  if(!c[a[i]]) {c[a[i]]=1;ans++; }
 }
 return ans;
}
int main(void) {
 int n;
 //int a[]={1,1,1}; //op 1
 int a[]={1,2,3,2,3,4,4,5}; //op 5
 //int a[]={1,2,3,2,3,5}; // op 4

 n=sizeof(a)/sizeof(int);
 int l=rrcopy(n,a);
 printf("min length %d",l);
 return l;
}

You can find it working at http://ideone.com/e.js/xuHuWk 

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array, codechef

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Sort an array according to the order defined by another array

Binary Tree Isomorphic to each other

VMWare SDEII Interview

Trie Dictionary

Generate largest number arranging a no. of given non negative integer numbers

Find next greater number with same set of digits

ADOBE Aptitude C Language Test

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Maximum of all subarrays of size k

Given a float number convert it into the string WITHOUT using any inbuilt Function

Password Predictor

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

How strtok() Works

1014 Practice Question of New GRE – Princeton

Find Pythagorean Triplets in an array in O(N)

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Amazon Interview Experience – SDE Chennai

Flipkart SDET Interview Experience

Sequence Finder Dynamic Programming

The Magic HackerEarth Nirvana solutions Hiring Challenge

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Check if an array has duplicate numbers in O(n) time and O(1) space

LeetCode: Container With Most Water

Find the kth number with prime factors 3, 5 and 7

Possible sizes of bus to carry n groups of friends

Count number of ways to reach a given score in a game

Difference between a LinkedList and a Binary Search Tree BST

Max Sum in circularly situated Values

Printing Longest Common Subsequence

Copyright © 2025 · Genesis Framework · WordPress · Log in